第一步:定义顶级枚举接口

public interface BaseEnum<E extends Enum<?>, T> {
public T getCode();
public String getValue();
}
第二步:实现枚举接口
public enum AccountTypeEnum implements BaseEnum<AccountTypeEnum,Integer>{
PERSONAL(1,"PERSONAL"),
ORGANIZATION(2,"ORGANIZATION");
private Integer code;
private String value;
static Map<Integer,AccountTypeEnum> enumMap= new HashMap<>();
static {
for(AccountTypeEnum accountTypeEnum:AccountTypeEnum.values()) {
enumMap.put(accountTypeEnum.getCode(),accountTypeEnum);
}
}

private AccountTypeEnum(Integer code,String value) {
this.code = code;
this.value = value;
}

public void setCode(Integer code) {
this.code = code;
}

public void setValue(String value) {
this.value = value;
}

@Override
public Integer getCode() {
return code;
}

@Override
public String getValue() {
return value;
}
public static AccountTypeEnum getEnum(Integer code) {
return enumMap.get(code);
}
}
第三步:定义mybatis全局枚举处理器
public class UniversalEnumHandler<E extends BaseEnum> extends BaseTypeHandler<E> {
private Class<E> type;
private E [] enums;

/**
* 设置配置文件设置的转换类以及枚举类内容,供其他方法更便捷高效的实现
* @param type 配置文件中设置的转换类
*/
public UniversalEnumHandler(Class<E> type) {
if (type == null)
throw new IllegalArgumentException("Type argument cannot be null");
this.type = type;
this.enums = type.getEnumConstants();
if (this.enums == null)
throw new IllegalArgumentException(type.getSimpleName() + " does not represent an enum type.");
}

@Override
public void setNonNullParameter(PreparedStatement ps, int i, E parameter, JdbcType jdbcType)throws SQLException {
//BaseTypeHandler已经做了parameter的null判断
// ps.setObject(i,(Integer)parameter.getState(), jdbcType.TINYINT.TYPE_CODE);
ps.setInt(i,(Integer)parameter.getCode());
}

@Override
public void setParameter(PreparedStatement ps, int i, E parameter, JdbcType jdbcType) throwsSQLException {
// ps.setObject(i,(Integer)parameter.getState(), jdbcType.TINYINT.TYPE_CODE);
ps.setInt(i,(Integer)parameter.getCode());
}

@Override
public E getNullableResult(ResultSet rs, String columnName) throws SQLException {
// 根据数据库存储类型决定获取类型
Integer i = rs.getInt(columnName);
if (rs.wasNull()) {
return null;
} else {
// 根据数据库中的code值,定位enum子类
return locateEnumStatus(i);
}
}

@Override
public E getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
// 根据数据库存储类型决定获取类型
Integer i = rs.getInt(columnIndex);
if (rs.wasNull()) {
return null;
} else {
// 根据数据库中的code值,定位enum子类
return locateEnumStatus(i);
}
}

@Override
public E getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
// 根据数据库存储类型决定获取类型
Integer i = cs.getInt(columnIndex);
if (cs.wasNull()) {
return null;
} else {
// 根据数据库中的code值,定位enum子类
return locateEnumStatus(i);
}
}

@Override
public E getResult(ResultSet rs, String columnName) throws SQLException {
Integer i = rs.getInt(columnName);
if (rs.wasNull()) {
return null;
} else {
// 根据数据库中的code值,定位enum子类
return locateEnumStatus(i);
}
}

@Override
public E getResult(ResultSet rs, int columnIndex) throws SQLException {
Integer i = rs.getInt(columnIndex);
if (rs.wasNull()) {
return null;
} else {
// 根据数据库中的code值,定位enum子类
return locateEnumStatus(i);
}
}

@Override
public E getResult(CallableStatement cs, int columnIndex) throws SQLException {
Integer i = cs.getInt(columnIndex);
if (cs.wasNull()) {
return null;
} else {
// 根据数据库中的code值,定位enum子类
return locateEnumStatus(i);
}
}

/**
* 枚举类型转换,由于构造函数获取了枚举的子类enums,让遍历更加高效快捷
* @param value 数据库中存储的自定义value属性
* @return value对应的枚举类
*/
private E locateEnumStatus(Integer value) {
for(E e : enums) {
if(e.getCode().equals(value)) {
return e;
}
}
throw new IllegalArgumentException("未知的枚举类型:" + value + ",请核对" + type.getSimpleName());
}
}
第四步:在mybatis配置文件中添加typeHandler标签
<configuration>
<settings>
<!-- 开启驼峰自动映射 -->
<setting name="mapUnderscoreToCamelCase" value="true" />
<!-- 二级缓存的总开关 -->
<setting name="cacheEnabled" value="false" />
</settings>
<typeHandlers>
<typeHandler handler="moc.service.infrastructure.mybatis.UniversalEnumHandler"
javaType="moc.commons.enums.AccountTypeEnum"/>
</typeHandlers>
<plugins>
<!-- 分页插件:com.github.pagehelper为PageHelper类所在包名 -->
<plugin interceptor="com.github.pagehelper.PageHelper">
<!-- 方言 -->
<property name="dialect" value="mysql" />
<!-- 该参数默认为false -->
<!-- 设置为true时,使用RowBounds分页会进行count查询,查询数据总条数 -->
<property name="rowBoundsWithCount" value="true" />
</plugin>

<!-- 通用Mapper插件 -->
<plugin interceptor="com.github.abel533.mapperhelper.MapperInterceptor">
<!-- 主键自增回写方法,默认值MYSQL,详细说明请看文档 -->
<property name="IDENTITY" value="MYSQL" />
<!--通用Mapper接口,多个通用接口用逗号隔开 -->
<property name="mappers" value="moc.persistence.mapper.base.SysMapper" />
</plugin>
</plugins>
</configuration>
第五步:定义数据持久对象
@Table(name = "t_user")
public class UserPO implements Serializable {
private static final long serialVersionUID = -7508885998192627398L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY, generator = "Mysql")
private Integer id;
private String userName;
private String nickName;
private String userPwd;
private AccountTypeEnum accountType;
private RoleTypeEnum userRole;
private String idCard;
private String phone;
private String email;
private String contacts;
private String contactsPhone;
private String address;
private Timestamp updateTime;
private Timestamp createTime;
private Boolean isDel;
}

mybatis枚举映射成tinyint的更多相关文章

  1. springboot~mybatis枚举映射

    在mybatis和mybatis plus里,如果你的实体字段是一个枚举类型,而在数据表里是整型,这时在存储时需要进行处理,默认情况下,会把枚举的元素名称拼接到SQL语句里,而由于数据表是int类型, ...

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

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

  3. Mybatis 枚举类处理

    目录 类型处理器(TypeHandler) 内置的枚举处理器 EnumTypeHandler源码 自定义枚举类处理 通用枚举处理器 Git 类型处理器(TypeHandler) 无论是 MyBatis ...

  4. MyBatis XML 映射配置文件

    配置文件的基本结构 configuration —— 根元素 properties —— 定义配置外在化 settings —— 一些全局性的配置 typeAliases —— 为一些类定义别名 ty ...

  5. 六 mybatis高级映射(一对一,一对多,多对多)

    1  订单商品数据模型 以订单商品数据为模型,来对mybaits高级关系映射进行学习.

  6. 【JAVA - SSM】之MyBatis输出映射

    MyBatis中的输出映射有两种:resultType和resultMap. 1.resultType 使用resultType进行结果映射时,只有当查询结果中有至少一列的名称和resultType指 ...

  7. MyBatis Generator自动生成MyBatis的映射代码

    MyBatis Generator大大简化了MyBatis的数据库的代码编写,有了一个配置文件,就可以直接根据表映射成实体类.Dao类和xml映射.资源地址:MyBatis项目地址:http://my ...

  8. Mybatis sql映射文件浅析 Mybatis简介(三)

    简介 除了配置相关之外,另一个核心就是SQL映射,MyBatis 的真正强大也在于它的映射语句. Mybatis创建了一套规则以XML为载体映射SQL 之前提到过,各项配置信息将Mybatis应用的整 ...

  9. Mybatis sql映射文件浅析 Mybatis简介(三) 简介

    Mybatis sql映射文件浅析 Mybatis简介(三)   简介 除了配置相关之外,另一个核心就是SQL映射,MyBatis 的真正强大也在于它的映射语句. Mybatis创建了一套规则以XML ...

随机推荐

  1. 转每天一个linux命令(4):mkdir命令

    linux mkdir 命令用来创建指定的名称的目录,要求创建目录的用户在当前目录中具有写权限,并且指定的目录名不能是当前目录中已有的目录. 1.命令格式: mkdir [选项] 目录... 2.命令 ...

  2. C4.5算法(摘抄)

    1. C4.5算法简介 C4.5是一系列用在机器学习和数据挖掘的分类问题中的算法.它的目标是监督学习:给定一个数据集,其中的每一个元组都能用一组属性值来描述,每一个元组属于一个互斥的类别中的某一类.C ...

  3. exjs上传图片异常:com.jspsmart.upload.SmartUploadException: File can't be saved (1120).

    错误: 文件名格式不对:未命??.jpg SmartUpload mySmartUpload = new SmartUpload(); com.jspsmart.upload.File myFile ...

  4. Mybatis映射文件完整模板参照

    Mybatis映射文件完整模板参照 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE map ...

  5. vue中引入swiper(vue中的滑块组件vue-awesome-swiper)

    第一步安装 npm install vue-awesome-swiper --save 第二部在main.js中引入 import VueAwesomeSwiper from 'vue-awesome ...

  6. hdu 3722 二分图 最优完备匹配 KM算法

    这题只要想到是最优完备匹配就行了: 题意:给出n个字符串,若两两相连,将前一个反置添加到后一个后面,相连的值为两个字串从头开始相等的字符个数: 问如何匹配得出最大值: 思路:建图,套模板. 代码: # ...

  7. [转载]浏览器事件window.onload、onfocus、onblur、ons

    原文地址:浏览器事件window.onload.onfocus.onblur.onscroll和resize作者:lilyxiao <html> <head> <titl ...

  8. Beta版本测试报告以及Beta版本发布说明

    Beta版本测试报告 请根据团队项目中软件的需求文档.功能说明.系统设计和Beta阶段的计划安排,写出软件的测试过程和测试结果,并回答下述问题. 在测试过程中总共发现了多少bug?每个类别的bug分别 ...

  9. 团队作业4--第一次项目冲刺(Alpha版本)7

    一.Daily Scrum Meeting照片 二.燃尽图 三.项目进展 1.完成全部基础功能 2.完成一些小改进与优化 四.困难与问题 软件基本是可以运行并且正常使用,但还没有实战过,遇到的问题与困 ...

  10. 【Alpha】Daily Scrum Meeting——Day5

    站立式会议照片 1.本次会议为第五次Meeting会议: 2.本次会议在上午大课间09:40,在陆大楼召开,本次会议为30分钟讨论昨天的任务完成情况以及接下来的任务安排. 燃尽图 每个人的工作分配 成 ...