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 ...
随机推荐
- app后端api设计【转】
博客:https://blog.csdn.net/newjueqi/article/details/44037011 app和后端的交互,一般都是通过后端提供的api实现.api的设计,估计很多刚进入 ...
- Java中BigDecimal的一个除法异常
java.lang.ArithmeticException: Non-terminating decimal expansion; no exact representable decimal res ...
- shell基础知识---与监听服务器长连接端口状态
从未写过脚本我的最近接了俩脚本的需求,就在这分享一下我的我学到基础知识主要就四部分内容 一.变量 变量的定义 string='字符串' string="字符串" num=808st ...
- HTML5中的拖拽与拖放(drag&&drop)
1.drag 当拖动某个元素时,将会依次触发下列事件: 1)dragstart:按下鼠标键并开始移动鼠标时,会触发该事件 2)drag:dragstart触发后,随即便触发drag事件,而且在元素被拖 ...
- thinkphp5配置讲解
一.thinkphp配置类型有哪些? 1.在thinkphp中,有6种配置.即惯例配置,应用配置.扩展配置.模块配置.场景配置.动态配置. 2.惯例配置就是系统默认的配置. 3.应用配置就是我们自己开 ...
- hadoop生态搭建(3节点)-10.spark配置
# https://www.scala-lang.org/download/2.12.4.html# ================================================= ...
- Zookeeper -- 关于Zookeeper
Zookeeper是什么? 分布式协调框架 Zookeeper中文件呈树形结构,树形结构下包含多个节点,称为Znode:zk中节点存储数据不超过1M,指得是Znode中存储数据不超过1M Zookee ...
- rails应用使用carrierwave和mini_magick上传用户头像
1. 在Gemfile添加 gem 'carrierwave' gem 'mini_magick' 执行 bundle install 2. 生成uploader rails generate upl ...
- go基础语法-函数
1.基础定义 golang的函数很'纯粹',只有可变参数列表的概念,没有默认参数.可选参数.函数重载.操作符重载这些难以把控的概念 语法:'func'声明,而后函数名在前,中间的括号内定义参数,返回值 ...
- HTML5 tricks for mobile
iOS 7.1的Safari为meta标签新增minimal-ui属性,在网页加载时隐藏地址栏与导航栏 01. Creating a fullscreen experience On Android ...