在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类型了,表示处理器成功了!

springboot~mybatis枚举映射的更多相关文章

  1. mybatis枚举映射成tinyint

    第一步:定义顶级枚举接口 public interface BaseEnum<E extends Enum<?>, T> { public T getCode(); publi ...

  2. MyBatis 查询映射自定义枚举

    背景                  MyBatis查询若想映射枚举类型,则需要从 EnumTypeHandler 或者 EnumOrdinalTypeHandler 中选一个来使用         ...

  3. springboot + mybatis

    这两天启动了一个新项目因为项目组成员一直都使用的是mybatis,虽然个人比较喜欢jpa这种极简的模式,但是为了项目保持统一性技术选型还是定了 mybatis.到网上找了一下关于spring boot ...

  4. SpringBoot+MyBatis+MySQL读写分离(实例)

    ​ 1. 引言 读写分离要做的事情就是对于一条SQL该选择哪个数据库去执行,至于谁来做选择数据库这件事儿,无非两个,要么中间件帮我们做,要么程序自己做.因此,一般来讲,读写分离有两种实现方式.第一种是 ...

  5. SpringBoot+Mybatis+MybatisPlus整合实现基本的CRUD操作

    SpringBoot+Mybatis+MybatisPlus整合实现基本的CRUD操作 1> 数据准备 -- 创建测试表 CREATE TABLE `tb_user` ( `id` ) NOT ...

  6. 从0开始完成SpringBoot+Mybatis实现增删改查

    1.准备知识: 1)需要掌握的知识: Java基础,JavaWeb开发基础,Spring基础(没有Spring的基础也可以,接触过Spring最好),ajax,Jquery,Mybatis. 2)项目 ...

  7. 基于Maven的Springboot+Mybatis+Druid+Swagger2+mybatis-generator框架环境搭建

    基于Maven的Springboot+Mybatis+Druid+Swagger2+mybatis-generator框架环境搭建 前言 最近做回后台开发,重新抓起以前学过的SSM(Spring+Sp ...

  8. 基于SpringBoot + Mybatis实现SpringMVC Web项目

    一.热身 一个现实的场景是:当我们开发一个Web工程时,架构师和开发工程师可能更关心项目技术结构上的设计.而几乎所有结构良好的软件(项目)都使用了分层设计.分层设计是将项目按技术职能分为几个内聚的部分 ...

  9. Springboot & Mybatis 构建restful 服务三

    Springboot & Mybatis 构建restful 服务三 1 前置条件 成功执行完Springboot & Mybatis 构建restful 服务二 2 restful ...

随机推荐

  1. gulp+webpack+angular1的一点小经验(第一部分gulp与webpack的整合)

    时间匆匆如流水继上周熟悉了gulp的初步安装与环境配置以后,我的项目又进入了新的阶段! 这篇文章将把我这一周遇到的一些问题,以及解决的方式做一个小小的总结,不一定记的完整,但都是个人的一点经验,分享给 ...

  2. C#DateTime格式转换全介绍

    DateTime与字符串转换: DateTime()与转换为字符串主要依靠DateTime().ToString(string format) 函数,以我的理解,参数format大体分为单个字母和多个 ...

  3. eclipse m2eclipse 从Maven的本地库中读取依赖库

    在Mac pro的终端中执行命令 mvn package 后,已经把该工程所需要的依赖库(dependancies)下载到本地库,但在把该工程 import 到 eclipse中时,发现m2eclip ...

  4. 1010 Radix (25 分),PTA

    题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805507225665536 题意:给定n1.n2两个数,求可以是两 ...

  5. HDU-1274

    在纺织CAD系统开发过程中,经常会遇到纱线排列的问题.  该问题的描述是这样的:常用纱线的品种一般不会超过25种,所以分别可以用小写字母表示不同的纱线,例如:abc表示三根纱线的排列:重复可以用数字和 ...

  6. CodeForces999A-Mishka and Contest

    A. Mishka and Contest time limit per test 1 second memory limit per test 256 megabytes input standar ...

  7. Python3 函数实践之简易购物系统

    函数实践之简易购物系统 项目主要需求: 用户可以自行选择功能 该购物系统具有注册/登录/购物/购物车/退出登录功能 用户在登录后才能使用购物/购物车/退出登录功能 ''' 注册 登录 购物 购物车 退 ...

  8. 【Web技术】337- 秒懂 Web 缓存

    点击上方"前端自习课"关注,学习起来~ 最近把前端缓存重新整理了一下,从整体的层面上把前端所有能用的缓存方案梳理了一遍.同时,对于http缓存,使用了表格的方案,使得原先晦涩难记的 ...

  9. get请求被浏览器跨域的同源策略请求机制拦截,但是get请求是否请求到了服务器呢

    浏览器会拦截跨域请求,但是只是拦截返回结果,请求还是会被发送到服务器. 请求因为跨域被拦截后,会改成 OPTIONS 请求送达服务器,这样服务器就可以知道有人在请求.

  10. Linux内核构建过程

    构建内核 # shell 执行如下指令make zImage 全局变量 srctree    := $(if $(KBUILD_SRC),$(KBUILD_SRC),$(CURDIR))objtree ...