mybatis枚举映射成tinyint
第一步:定义顶级枚举接口
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;}@Overridepublic Integer getCode() {return code;}@Overridepublic 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.");}@Overridepublic 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());}@Overridepublic 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());}@Overridepublic E getNullableResult(ResultSet rs, String columnName) throws SQLException {// 根据数据库存储类型决定获取类型Integer i = rs.getInt(columnName);if (rs.wasNull()) {return null;} else {// 根据数据库中的code值,定位enum子类return locateEnumStatus(i);}}@Overridepublic E getNullableResult(ResultSet rs, int columnIndex) throws SQLException {// 根据数据库存储类型决定获取类型Integer i = rs.getInt(columnIndex);if (rs.wasNull()) {return null;} else {// 根据数据库中的code值,定位enum子类return locateEnumStatus(i);}}@Overridepublic E getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {// 根据数据库存储类型决定获取类型Integer i = cs.getInt(columnIndex);if (cs.wasNull()) {return null;} else {// 根据数据库中的code值,定位enum子类return locateEnumStatus(i);}}@Overridepublic E getResult(ResultSet rs, String columnName) throws SQLException {Integer i = rs.getInt(columnName);if (rs.wasNull()) {return null;} else {// 根据数据库中的code值,定位enum子类return locateEnumStatus(i);}}@Overridepublic E getResult(ResultSet rs, int columnIndex) throws SQLException {Integer i = rs.getInt(columnIndex);if (rs.wasNull()) {return null;} else {// 根据数据库中的code值,定位enum子类return locateEnumStatus(i);}}@Overridepublic 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的更多相关文章
- springboot~mybatis枚举映射
在mybatis和mybatis plus里,如果你的实体字段是一个枚举类型,而在数据表里是整型,这时在存储时需要进行处理,默认情况下,会把枚举的元素名称拼接到SQL语句里,而由于数据表是int类型, ...
- MyBatis 查询映射自定义枚举
背景 MyBatis查询若想映射枚举类型,则需要从 EnumTypeHandler 或者 EnumOrdinalTypeHandler 中选一个来使用 ...
- Mybatis 枚举类处理
目录 类型处理器(TypeHandler) 内置的枚举处理器 EnumTypeHandler源码 自定义枚举类处理 通用枚举处理器 Git 类型处理器(TypeHandler) 无论是 MyBatis ...
- MyBatis XML 映射配置文件
配置文件的基本结构 configuration —— 根元素 properties —— 定义配置外在化 settings —— 一些全局性的配置 typeAliases —— 为一些类定义别名 ty ...
- 六 mybatis高级映射(一对一,一对多,多对多)
1 订单商品数据模型 以订单商品数据为模型,来对mybaits高级关系映射进行学习.
- 【JAVA - SSM】之MyBatis输出映射
MyBatis中的输出映射有两种:resultType和resultMap. 1.resultType 使用resultType进行结果映射时,只有当查询结果中有至少一列的名称和resultType指 ...
- MyBatis Generator自动生成MyBatis的映射代码
MyBatis Generator大大简化了MyBatis的数据库的代码编写,有了一个配置文件,就可以直接根据表映射成实体类.Dao类和xml映射.资源地址:MyBatis项目地址:http://my ...
- Mybatis sql映射文件浅析 Mybatis简介(三)
简介 除了配置相关之外,另一个核心就是SQL映射,MyBatis 的真正强大也在于它的映射语句. Mybatis创建了一套规则以XML为载体映射SQL 之前提到过,各项配置信息将Mybatis应用的整 ...
- Mybatis sql映射文件浅析 Mybatis简介(三) 简介
Mybatis sql映射文件浅析 Mybatis简介(三) 简介 除了配置相关之外,另一个核心就是SQL映射,MyBatis 的真正强大也在于它的映射语句. Mybatis创建了一套规则以XML ...
随机推荐
- k-临近算法学习
本章主要内容: k-临近算法是通过对象本身的特征将对象划分到某一类型中去,比如电影中的题材类型的分类是,可能就会考虑到电影中出现的镜头出现的次数的多少将电影划分为动作电影,爱情电影等等,本次的随笔参考 ...
- shell之 printf 输出语句
总结: (1)printf 使用引用文本或空格分隔的参数,外面可以在printf中使用格式化字符串,还可以制定字符串的宽度.左右对齐方式等.默认printf不会像 echo 自动添加换行符,我们可以手 ...
- python基础教程(七)
本章介绍如何将语句组织成函数,这样,可以告诉计算机如何做事. 下面编写一小段代码计算婓波那契数列(前两个数的和是第三个数) fibs = [0,1] # 定义一个列表,初始内容是0,1 for i ...
- [2012-06-18]awk利用关联数组合并记录
问题源起:http://bbs.chinaunix.net/thread-3753784-1-1.html 代码如下 {% capture text %} $awk '{if(!a[$1]){a[$1 ...
- TP5.0实现无限极回复功能
最近做项目的时候用到了评论回复,使用ThinkPHP5.0框架做回复碰到了一些问题,简单总结一下.(李昌辉) 1.首先是数据表的设计: create table zy_huifu ( code int ...
- 新建github项目,邀请成员
创建一个项目(repository) 进入项目,点击 SETTINGS 点击左侧导航的 Collaborators 在 Teams 里有个下拉菜单,里面你可以给你的 team 选择 write(写)权 ...
- CSS 中的 initial、inherit、unset、revert
在css中,initial(初始).inherit(继承).unset(未设置).revert(还原)这四个关键字可以应用于所有的CSS属性. initial - 初始默认值.IE不支持 inheri ...
- jvm 常用内存分析命令
详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt121 // 打印出内存占用情况 jstat -gcutil 12564 10 ...
- sqlserver与mysql中vachar(n)中遇到的坑
前两天在做将mysql的数据表导入到sqlserver当中. 本人比较愚笨,操作方法 是先将mysql的数据表到处为insert脚本,再在sqlserver中执行sql脚本 在网上看了一下那些方法 , ...
- java中synchronized的使用
synchronized是Java中的关键字,是一种同步锁. synchronized分对象锁和类的锁两种. (一)通常synchronized 方法和synchronized(this){}都是属于 ...