`

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已经帮我们做了parameternull判断
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文件中的使用:
在使用枚举的地方指定typeHandler,如下:
<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映射

 

分享到:
评论

相关推荐

    springboot--mybatis枚举自动转换实现.rar

    现在,当你在Mapper接口中使用`Gender`枚举类型时,MyBatis会自动使用我们定义的`GenderTypeHandler`进行转换。例如: ```java @Mapper public interface UserMapper { @Select("SELECT * FROM user WHERE gender...

    mybatis入门实战之枚举类型

    本文将深入探讨在MyBatis中如何使用枚举类型,并通过实际的项目"mybatis入门实战之枚举类型"进行讲解。这个项目提供了一个简单的demo,非常适合初学者了解并实践MyBatis的TypeHandler机制。 首先,我们要明白枚举...

    mybatis中实现枚举自动转换方法详解

    在MyBatis中,枚举类型的自动转换是一个常见的需求,特别是在处理数据库字段与Java枚举类型之间转换时。本文将详细介绍如何在MyBatis中实现枚举的自动转换,以便在查询结果返回时能直接得到对应的枚举实例,无需额外...

    1.springbood+mybatis项目demo2.mybatis自定义枚举类型的转换器以及各种使用场景

    本项目示例"1.springboot+mybatis项目demo2.mybatis自定义枚举类型的转换器以及各种使用场景"着重展示了如何在Spring Boot应用中集成MyBatis,并利用MyBatis处理枚举类型的数据。 首先,我们来了解Spring Boot。...

    mybatis处理枚举类的简单方法

    MyBatis处理枚举类的简单方法 MyBatis是一个流行的持久层框架,提供了很多便捷的方法来处理数据库交互,然而,在处理枚举类时,MyBatis提供的两个枚举处理类:`EnumOrdinalTypeHandler`和`EnumTypeHandler`,却存在...

    spring boot mybatis枚举映射示例代码

    现在,当我们在实体类中使用`YesOrNo`枚举类型,并映射到整型字段时,MyBatis Plus会自动调用我们自定义的枚举处理器,实现枚举与数据库字段之间的转换,从而避免了因数据类型不匹配导致的异常。这种方法提高了代码...

    mybatis-plus使用@EnumValue处理枚举类型的示例代码

    在MyBatis-Plus 3.1.0版本之后,如果你不需要使用原生的枚举类型,可以通过配置默认的枚举类型处理器来避免扫描所有的枚举。默认枚举类型处理器的配置包括两个步骤: 1. **配置文件配置**:在`mybatis-plus`的配置...

    MyBatis中如何优雅的使用枚举详解

    总结总结通过以上步骤,我们已经成功地在MyBatis中优雅地使用了枚举类型。这种方式不仅保持了代码的可读性,同时也方便了数据库与枚举之间的数据交互。在实际项目中,我们可以创建一个枚举基类,让所有枚举继承该...

    Mybatis类型转换介绍 TypeHandler

    在实际项目中,我们可能会遇到需要处理复杂类型转换的情况,比如枚举类型、自定义集合类型等。这时,自定义TypeHandler可以有效地解决这些问题,提高代码的可读性和可维护性。 总结来说,Mybatis的TypeHandler是...

    Mybatis-Plus通用枚举的使用详解

    在处理枚举类型时,Mybatis-Plus提供了通用枚举(Generic Enum)的功能,使得枚举在数据库交互中的使用更加优雅。 在数据库设计中,我们常常会遇到一些具有固定选项的字段,如性别、状态等,这些字段通常可以使用...

    boot+mybatis、mybatis中枚举转换器、前后端分离项目统一返回格式、boot集成quraz框架实现定时任务

    在MyBatis中,枚举类型与数据库字段之间的转换可能会遇到问题,因为枚举的存储和读取方式需要自定义。为解决此问题,我们可以创建一个自定义的TypeHandler,实现`org.apache.ibatis.type.TypeHandler`接口。这个...

    Mybatis plus无介绍快使用,枚举变量的使用(七)

    在Mybatis Plus中,我们可以直接使用枚举类型的`code`作为数据库字段的值,而在业务逻辑中使用枚举的`desc`进行展示。这样做的好处是,即使数据库中的数值发生变化,只要保持枚举类的对应关系,代码的逻辑就不会受到...

    mybatis入门之typeHanlder的三种使用案例

    在Java开发中,MyBatis是一个非常流行的持久层框架,它简化了数据库操作,并提供了强大的映射机制。...正确选择和使用TypeHandler,尤其是针对枚举类型,能够有效避免潜在的维护问题,提高代码的可读性和可维护性。

    MyBatis3.2.2中文官方文档

    MyBatis对枚举类型提供了一定的支持,可以通过配置文件自定义枚举类型的映射方式。 #### 对象工厂(objectFactory) 对象工厂用于创建结果对象的实例。MyBatis在设置结果集属性时,会调用对象工厂来创建对象。 ##...

    ibatis 支持枚举类型

    现在,我们可以在Mapper接口和XML配置中使用枚举类型了。假设有一个用户实体类`User`,其中包含`status`字段为`Status`类型,那么Mapper接口可以这样定义: ```java public interface UserMapper { @Select(...

    Mybatis中自定义TypeHandler处理枚举详解

    在Mybatis中,枚举类型的数据处理是通过TypeHandler实现的。默认情况下,Mybatis提供了两种内置的枚举TypeHandler: 1. `EnumTypeHandler`: 这个TypeHandler将枚举对象转换为它们的字符串表示,即枚举的名称。在...

    自定义mybatis流程分析.rar

    1. 自定义TypeHandler:当MyBatis内置的TypeHandler无法满足需求时,可以编写自定义TypeHandler,覆盖默认的类型转换逻辑,如处理自定义枚举类型、复杂对象等。 2. 自定义Executor:Executor是MyBatis的执行器,...

    MyBatis(ibatis)学习文档.docx

    MyBatis的枚举类型处理器,用于处理Java枚举类型与数据库表之间的映射关系。 object, MyBatis的对象是指Java对象,用于表示业务逻辑中的实体对象。MyBatis的对象可以与数据库表之间建立映射关系,从而实现数据的...

    jeecg-mybatis-framework

    提供数据字典管理功能,便于维护常用枚举类型;同时支持多语言切换,满足全球化应用需求。 9. **快速开发平台**: Jeecg-Mybatis-Framework不仅仅是一个代码生成工具,更是一个完整的快速开发平台,包含了丰富的...

Global site tag (gtag.js) - Google Analytics