mybatis 枚举类型使用
一、首先定义接口,提供获取数据库存取的值得方法,如下:
public interface BaseEnum {
int getCode();
}
二、定义mybatis的typeHandler扩展类,如下:
package com.camelot.assetcenter.sdk.orm.mybatis; import com.camelot.assetcenter.sdk.common.BaseEnum;
import com.camelot.openplatform.common.log.Log;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.slf4j.Logger; import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException; public class EnumTypeHandler extends BaseTypeHandler<BaseEnum> { private final static Logger logger = Log.get(EnumTypeHandler.class);
private Class<BaseEnum> type; private final BaseEnum[] enums; /**
* 设置配置文件设置的转换类以及枚举类内容,供其他方法更便捷高效的实现
* @param type 配置文件中设置的转换类
*/
public EnumTypeHandler(Class<BaseEnum> 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 BaseEnum getNullableResult(ResultSet rs, String columnName) throws SQLException {
// 根据数据库存储类型决定获取类型,本例子中数据库中存放INT类型
logger.debug(columnName);
int i = rs.getInt(columnName); if (rs.wasNull()) {
return null;
} else {
// 根据数据库中的code值,定位EnumStatus子类
return locateEnumStatus(i);
}
} @Override
public BaseEnum getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
// 根据数据库存储类型决定获取类型,本例子中数据库中存放INT类型
logger.debug(columnIndex+"");
int i = rs.getInt(columnIndex);
if (rs.wasNull()) {
return null;
} else {
// 根据数据库中的code值,定位EnumStatus子类
return locateEnumStatus(i);
}
} @Override
public BaseEnum getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
// 根据数据库存储类型决定获取类型,本例子中数据库中存放INT类型
logger.debug(columnIndex+"");
int i = cs.getInt(columnIndex);
if (cs.wasNull()) {
return null;
} else {
// 根据数据库中的code值,定位EnumStatus子类
return locateEnumStatus(i);
}
} @Override
public void setNonNullParameter(PreparedStatement ps, int i, BaseEnum parameter, JdbcType jdbcType)
throws SQLException {
// baseTypeHandler已经帮我们做了parameter的null判断
logger.debug(parameter.getCode()+"=================================");
ps.setInt(i, parameter.getCode()); } /**
* 枚举类型转换,由于构造函数获取了枚举的子类enums,让遍历更加高效快捷
* @param code 数据库中存储的自定义code属性
* @return code对应的枚举类
*/
private BaseEnum locateEnumStatus(int code) {
for(BaseEnum status : enums) {
if(status.getCode() ==code ) {
return status;
}
}
throw new IllegalArgumentException("未知的枚举类型:" + code + ",请核对" + type.getSimpleName());
} }
三、在mapper文件中的使用:
<select id="queryOverdueDetail" resultMap="assetUserIntegralDetailMap">
<include refid="selectAllColumns" />
where 1=1
<if test="entity!=null">
<if test="entity.type != null and entity.type !=''">
<![CDATA[ and asset_user_integral_detail_.type = #{entity.type,typeHandler=com.camelot.assetcenter.sdk.orm.mybatis.EnumTypeHandler} ]]>
</if> <if test="entity.status != null and entity.status !=''">
<![CDATA[ and asset_user_integral_detail_.status = #{entity.status,typeHandler=com.camelot.assetcenter.sdk.orm.mybatis.EnumTypeHandler} ]]>
</if>
<if test="entity.createTime != null ">
<![CDATA[ and asset_user_integral_detail_.create_time < #{overdueTime} ]]>
</if>
</if>
<if test="limitCount !=null and limitCount != '' ">
limit #{limitCount}
</if>
</select>
<resultMap id="assetUserIntegralDetailMap" type="assetUserIntegralDetail">
<result property="userIntegralDetailId" column="user_integral_detail_id" />
<result property="userIntegralId" column="user_integral_id" />
<result property="integral" column="integral" />
<result property="balanceIntegral" column="balance_integral" />
<result property="type" column="type" typeHandler="com.camelot.assetcenter.sdk.orm.mybatis.EnumTypeHandler"/>
<result property="businessType" column="business_type" typeHandler="com.camelot.assetcenter.sdk.orm.mybatis.EnumTypeHandler"/>
<result property="businessId" column="business_id" />
<result property="source" column="source" typeHandler="com.camelot.assetcenter.sdk.orm.mybatis.EnumTypeHandler"/>
<result property="shopId" column="shop_id" />
<result property="status" column="status" typeHandler="com.camelot.assetcenter.sdk.orm.mybatis.EnumTypeHandler"/>
<result property="createTime" column="create_time" />
<result property="description" column="description" />
</resultMap>
<if
test="entity.type != null and entity.type !=''">
<![CDATA[ and asset_user_integral_detail_.type = #{entity.type,typeHandler=com.camelot.assetcenter.sdk.orm.mybatis.EnumTypeHandler} ]]>
</if>
<if
test="entity.businessType != null and entity.businessType !=''">
<![CDATA[ and asset_user_integral_detail_.business_type = #{entity.businessType,typeHandler=com.camelot.assetcenter.sdk.orm.mybatis.EnumTypeHandler} ]]>
</if>
按照以上方式既可以实现枚举类型和mybatis映射
mybatis 枚举类型使用的更多相关文章
- mybatis枚举类型处理器
1. 定义枚举值的接口 public abstract interface ValuedEnum { int getValue(); } 所有要被mybatis处理的枚举类继承该接口 2. 定义枚举类 ...
- MyBatis里字段到枚举类型的转换/映射
一.简介 我们在用MyBatis里,很多时间有这样一个需求:bean里有个属性是枚举,在DB存储时我们想存的枚举的代号,从DB拿出来时想直接映射成目标枚举类型,也即代号字段与Java枚举类的相互类型转 ...
- MyBatis对于Java对象里的枚举类型处理
平时咱们写程序实体类内或多或少都会有枚举类型属性,方便嘛.但是mybatis里怎么处理他们的增删改查呢? 要求: 插入的时候,会用枚举的定义插入数据库,我们希望在数据库中看到的是数字或者其他东西: 查 ...
- MyBatis(八):Mybatis Java API枚举类型转化的用法
最近工作中用到了mybatis的Java API方式进行开发,顺便也整理下该功能的用法,接下来会针对基本部分进行学习: 1)Java API处理一对多.多对一的用法: 2)增.删.改.查的用法: 3) ...
- mybatis 处理枚举类型
MyBatis支持持久化enum类型属性.假设t_user表中有一列gender(性别)类型为 varchar2(10),存储 MALE 或者 FEMALE 两种值.并且,User对象有一个enum类 ...
- mybatis枚举映射成tinyint
第一步:定义顶级枚举接口 public interface BaseEnum<E extends Enum<?>, T> { public T getCode(); publi ...
- mybatis-枚举类型的typeHandler&自定义枚举类型typeHandler
MyBatis内部提供了两个转化枚举类型的typeHandler给我们使用. org.apache.ibatis.type.EnumTypeHandler 是使用枚举字符串名称作为参数传递的 org. ...
- mybatis枚举自动转换(通用转换处理器实现)
https://blog.csdn.net/fighterandknight/article/details/51520595 https://blog.csdn.net/fighterandknig ...
- 解决:oracle+myBatis ResultMap 类型为 map 时,表字段类型有 Long/Blob/Clob 时报错
前言:最近在做一个通用查询单表的组件,所以 sql 的写法就是 select *,然后 resultType="map" .如果数据库中的表里有字段类型为 Long 等类型时,my ...
随机推荐
- Shader Optimization Tips
Author : http://www.cnblogs.com/open-coder/p/3982999.html During the last few months, I have been wo ...
- CF1066D Boxes Packing(二分答案)
题意描述: 你有$n$个物品,每个物品大小为$a_i$,$m$个盒子,每个盒子的容积为$k$.$Maksim$先生想把物品装入盒子中.对于每个物品,如果能被放入当前的盒子中,则放入当前盒子,否则换一个 ...
- datatable去掉表头默认排序
禁用排序:"ordering":false 某一列禁用排序:"orderable":false 以某一列排序:"order":[[x,&qu ...
- js 中~~是什么意思?
其实是一种利用符号进行的类型转换,转换成数字类型 ~~true == 1~~false == 0~~"" == 0~~[] == 0 ~~undefined ==0~~!undef ...
- python中的super怎么用?
面向对象有这个强大特点和作用, 著名的三大特点:封装, 继承, 多态 这篇博客写的是super()的简单理解和使用 今天在读restframework的源码的时候, 发现源码中使用了super, 依以 ...
- Apache Spark on K8s的安全性和性能优化
前言 Apache Spark是目前最为流行的大数据计算框架,与Hadoop相比,它是替换MapReduce组件的不二选择,越来越多的企业正在从传统的MapReduce作业调度迁移到Spark上来,S ...
- Hive--关联表(join)
在hive中,关联有4种方式: 内关联:join on 左外关联:left join on 右外关联:right join on 全外关联:full join on 另外还有一种可实现hive笛卡儿积 ...
- B/S与C/S架构简介
概念: C/S结构,即Client/Server(客户机/服务器)结构,是大家熟知的软件系统体系结构,通过将任务合理分配到Client端和Server端,降低了系统的通讯开销,可以充分利用两端硬件环境 ...
- Java学习笔记十六:Java中的构造方法
Java中的构造方法 1.使用new+构造方法 创建一个新的对象: 2.构造方法是定义在Java类中的一个用来初始化对象的方法: 3.构造方法与类同名且没有返回值: 4.语法格式: public 构造 ...
- VXLAN简介(摘抄)
VXLAN简介 VXLAN:Virtual eXtensible Local Area Network的缩写,虚拟扩展局域网,现代数据中心的的一种网络虚拟化技术,即在传统的三层IP网络上虚拟出一张二层 ...