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 ...
随机推荐
- 使用第三方库iOS-ECharts做柱状图的心得
最近的项目里面用到了饼图和条形统计图,饼图用的是PNChart来做的,这个库感觉用起来也简单,但是做条形统计图的时候就特别蛋疼(不知道是不是我姿势没对),反正就是各种问题,然后就想到换一种框架,最后选 ...
- OC - 时间日期类NSDate
OC - 时间日期类NSDate //NSDate 时间日期类 NSDate 二进制数据流 { //1.获取当前时间 零时区的时间 //显示的是格林尼治的时间: 年-月-日 时:分:秒:+时区 NSD ...
- 使用js函数格式化xml字符串带缩进
遇到了一个做soap的API的操作,中途需要说明xml的组装模式等, 如上图,组装产生的mxl代码药格式化并展示.由于是在前端做的,所以需要将字符串将xml进行格式化并输出,找到别人写的算法稍加更改并 ...
- 【Django笔记四】Django2.0中的表单
一.环境版本信息: 操作系统:windows10 Django版本:2.0.5 Python版本:3.6.4 Mysql版本: 5.5.53 安装mysql 二.基础信息 1.App中的模型mod ...
- BZOJ2037: [Sdoi2008]Sue的小球(区间DP)
Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 869 Solved: 483[Submit][Status][Discuss] Description ...
- 复习宝典之Spring
查看更多宝典,请点击<金三银四,你的专属面试宝典> 第六章:Spring Spring容器是Spring的核心,一切Spring bean都存储在Spring容器内,并由其通过IoC技术管 ...
- yii学习笔记(5),视图操作
在控制器调用$this->render()方法来输出视图 function actionLogin(){ $name = "admin"; // 加载视图 return $t ...
- laravel4.2 Redis 使用
laravel4.2 Redis 使用 配置文件,app/config/database.php 'redis' => array( 'cluster' => false, 'defaul ...
- scala 获取当前时间的两种方式
在编写程序时,有时需要获取当前时间,这在记录异常信息.获取程序运行耗时很有用处 方式一: val time1=System.currentTimeMillis() 这种方式获取的是程序运行到此的毫秒数 ...
- 第5天 Java基础语法
第5天 Java基础语法 今日内容介绍 方法 方法 方法概述 在我们的日常生活中,方法可以理解为要做某件事情,而采取的解决办法. 如:小明同学在路边准备坐车来学校学习.这就面临着一件事情(坐车到学校这 ...