spring boot mybatis枚举映射示例代码
作者:张占岭 时间:2023-03-01 11:45:51
前言
在mybatis和mybatis plus里,如果你的实体字段是一个枚举类型,而在数据表里是整型,这时在存储时需要进行处理,默认情况下,会把枚举的元素名称拼接到SQL语句里,而由于数据表是int类型,所以在插入等操作时,就会出现异常!
添加枚举处理器
MappedTypes(value = {YesOrNo.class})
public class UniversalEnumHandler<E extends Enum<E> & BaseEnum> extends BaseTypeHandler<E> {
private final Class<E> type;
/**
* construct with parameter.
*/
public UniversalEnumHandler(Class<E> type) {
if (type == null) {
throw new IllegalArgumentException("Type argument cannot be null");
}
this.type = type;
}
@Override
public void setNonNullParameter(PreparedStatement ps, int i, E parameter, JdbcType jdbcType)
throws SQLException {
ps.setInt(i, parameter.getCode());
}
@Override
public E getNullableResult(ResultSet rs, String columnName) throws SQLException {
int code = rs.getInt(columnName);
return rs.wasNull() ? null : EnumUtils.codeOf(this.type, code);
}
@Override
public E getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
int code = rs.getInt(columnIndex);
return rs.wasNull() ? null : EnumUtils.codeOf(this.type, code);
}
@Override
public E getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
int code = cs.getInt(columnIndex);
return cs.wasNull() ? null : EnumUtils.codeOf(this.type, code);
}
}
在配置文件指定处理器
mybatis-plus:
typeHandlersPackage: cn.pilipa.account.cerebrum.client.enums #处理器所在包,我是把枚举处理器放在枚举包里
定义代表枚举键值的接口
public interface BaseEnum<E extends Enum<?>, T> {
public Integer getCode();
public String getText();
}
定义一下枚举
public enum YesOrNo implements BaseEnum {
Yes(1, "是"),
No(0, "否");
private Integer code;
private String text;
YesOrNo(Integer code, String text) {
this.code = code;
this.text = text;
}
@JsonCreator
public static YesOrNo jsonCreate(Integer code) {
return EnumUtils.codeOf(YesOrNo.class, code);
}
@Override
public Integer getCode() {
return this.code;
}
@Override
public String getText() {
return this.text;
}
@JsonValue
public Integer getCodeStr() {
return this.code;
}
}
在实体中定义枚举类型字段
/**
* 是否为国民.
*/
private YesOrNo naturalBorn;
生成的SQL语句
==> Preparing: INSERT INTO employee_info ( id, name, credit_number, status, first_time, tax_code, natural_born ) VALUES ( ?, ?, ?, ?, ?, ?, ? )
2019-09-05 16:56:38.991 DEBUG [accounting-client,,,] 92833 --- [ main] c.p.a.c.c.m.EmployeeInfoMapper.insert :
==> Parameters: 1169534796253630466(Long), 段会涛(String), 130523199011111219(String), 1(Integer), 2019-09-05(Date), 130523199011111219(String), 0(Integer)
从上面结果中看到,我们的natural_born对应的值已经是int类型了,表示处理器成功了!
来源:https://www.cnblogs.com/lori/p/11468268.html
标签:springboot,mybatis,枚举
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
教你开发脚手架集成Spring Boot Actuator监控的详细过程
2021-11-19 03:27:39
C#使用base64对字符串进行编码和解码的测试
2022-09-21 23:08:59
![](https://img.aspxhome.com/file/2023/2/78112_0s.png)
Spring事务注解@Transactional失效的八种场景分析
2022-03-09 13:36:13
利用C#实现分割GIF图片
2021-06-15 16:16:41
![](https://img.aspxhome.com/file/2023/7/113807_0s.png)
详解C#中的委托
2022-05-10 06:01:43
![](https://img.aspxhome.com/file/2023/0/108740_0s.png)
C#中winform实现自动触发鼠标、键盘事件的方法
2022-02-23 22:50:58
Java常见面试题之多线程和高并发详解
2023-07-24 09:33:51
![](https://img.aspxhome.com/file/2023/4/108174_0s.jpg)
SpringBoot整合ElasticSearch实践
2023-02-05 06:02:08
![](https://img.aspxhome.com/file/2023/1/78281_0s.png)
解决使用mybatis-plus时,生成的SQL大写变小写加下划线问题
2022-10-20 09:24:47
C#中怎样从指定字符串中查找并替换字符串?
2023-09-30 14:26:24
![](https://img.aspxhome.com/file/2023/6/83236_0s.jpg)
C# API中模型与它们的接口设计详解
2023-11-03 19:49:01
idea创建JAVA Class时自动生成头部文档注释的方法
2023-07-10 18:53:07
![](https://img.aspxhome.com/file/2023/5/69715_0s.png)
Android 中RecyclerView顶部刷新实现详解
2022-08-20 17:22:39
SpringBoot整合WebService的实现示例
2023-05-25 12:37:55
![](https://img.aspxhome.com/file/2023/4/60754_0s.png)
springboot结合vue实现增删改查及分页查询
2023-11-24 15:53:44
![](https://img.aspxhome.com/file/2023/3/59783_0s.jpg)
代理角色java设计模式之静态代理详细介绍
2022-10-14 16:48:03
教你怎么用Java数组和链表实现栈
2023-10-29 08:13:57
![](https://img.aspxhome.com/file/2023/0/58720_0s.png)
spring MVC实现简单登录功能
2023-11-13 05:20:48
![](https://img.aspxhome.com/file/2023/3/59563_0s.jpg)
Java实战之用hutool-db实现多数据源配置
2023-11-28 19:37:10
![](https://img.aspxhome.com/file/2023/0/60250_0s.png)
C#中的HttpWebRequest类用法详解
2022-11-07 11:41:17