文章目录
- 前言
- 自定义 TypeHandler
- 1. 创建 TypeHandler 类
- 2. 注册自定义 TypeHandler
- 2.1. 在 MyBatis XML 配置文件中注册:
- 2.2. 在 Spring Boot 中注册:
- 2.3. 在 Spring Boot配置文件注册:
- 3. 使用 TypeHandler
- 3.1 在实体类属性上使用
- 3.2 在 XML 映射文件中使用
- 3.3 在Wrapper 查询中使用(`MyBatis-Plus 3.5.3.2 版本开始`)
- 总结
- 官方文档地址
前言
在 MyBatis-Plus 中,TypeHandler 是用来处理 Java 类型与数据库类型之间转换的组件。它们用于在执行 SQL 语句时,将 Java 对象的值设置到 PreparedStatement 中,或者从 ResultSet 或 CallableStatement 中取出值。它可以帮助 MyBatis-Plus 将数据库中的数据转换成 Java 对象,或者将 Java 对象转换成数据库可接受的数据类型。而MyBatis-Plus 给大家提供了提供了一些内置的类型处理器,可以通过 TableField 注解快速注入到 MyBatis 容器中,从而简化开发过程。
自定义 TypeHandler
有时候,我们需要处理一些特殊的数据类型,或者需要自定义数据类型与数据库类型的映射关系。这时就需要自定义 TypeHandler。
1. 创建 TypeHandler 类
要自定义一个 TypeHandler,需要实现 MyBatis 的 TypeHandler<T>
接口或者继承 BaseTypeHandler<T>
抽象类(推荐后者)。通常,继承 BaseTypeHandler<T>
更为方便,因为它已经提供了对 null 值的处理。
// 字段类型
@MappedTypes(MyType.class)
// 数据库字段类型
@MappedJdbcTypes(JdbcType.VARCHAR)
public class MyTypeHandler extends BaseTypeHandler<MyType> {@Overridepublic void setNonNullParameter(PreparedStatement ps, int i, MyType parameter, JdbcType jdbcType) throws SQLException {// 将 Java 类型参数 parameter 转换成数据库需要的类型,并设置到 PreparedStatement 中ps.setString(i, parameter.toString()); // 示例:假设 MyType 转换成字符串存储}@Overridepublic MyType getNullableResult(ResultSet rs, String columnName) throws SQLException {// 从 ResultSet 中获取 columnName 列的值,并转换成 Java 类型 MyTypereturn MyType.parse(rs.getString(columnName)); // 示例:假设从字符串解析成 MyType}@Overridepublic MyType getNullableResult(ResultSet rs, int columnIndex) throws SQLException {// 从 ResultSet 中获取 columnIndex 列的值,并转换成 Java 类型 MyTypereturn MyType.parse(rs.getString(columnIndex)); // 示例:假设从字符串解析成 MyType}@Overridepublic MyType getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {// 从 CallableStatement 中获取 columnIndex 列的值,并转换成 Java 类型 MyTypereturn MyType.parse(cs.getString(columnIndex)); // 示例:假设从字符串解析成 MyType}
}
2. 注册自定义 TypeHandler
注册自定义的 TypeHandler 通常需要在 MyBatis 的配置文件中或者 Spring Boot 的配置类中进行配置,具体方式取决于你的项目结构和配置方式。
2.1. 在 MyBatis XML 配置文件中注册:
<typeHandlers><typeHandler handler="com.example.MyTypeHandler" javaType="com.example.MyType"/>
</typeHandlers>
2.2. 在 Spring Boot 中注册:
@Configuration
public class MyBatisConfig {@Beanpublic ConfigurationCustomizer mybatisConfigurationCustomizer() {return configuration -> {// 注册自定义 TypeHandlerconfiguration.getTypeHandlerRegistry().register(com.example.MyType.class, com.example.MyTypeHandler.class);};}
}
2.3. 在 Spring Boot配置文件注册:
# Mybatis-Plus配置
mybatis-plus:# TypeHandler扫描路径type-handlers-package: com.example.MyTypeHandler
3. 使用 TypeHandler
自定义 TypeHandler 注册后,MyBatis-Plus 将会自动识别并使用它。在实体类的属性上可以使用 @TableField
注解指定 TypeHandler,或者在查询方法的 SQL 中使用 #{property, typeHandler=com.example.MyTypeHandler}
明确指定,亦或者在Wrapper 查询中直接使用 TypeHandler。
3.1 在实体类属性上使用
@Data
@Accessors(chain = true)
@TableName(autoResultMap = true)
public class User {private Long id;.../*** 必须开启映射注解** @TableName(autoResultMap = true)** 选择对应的 JSON 处理器,并确保存在对应的 JSON 解析依赖包*/@TableField(typeHandler = MyTypeHandler.class)// 或者使用 FastjsonTypeHandler// @TableField(typeHandler = FastjsonTypeHandler.class)private OtherInfo otherInfo;
}
3.2 在 XML 映射文件中使用
<!-- 单个字段的类型处理器配置 -->
<result column="other_info" jdbcType="VARCHAR" property="otherInfo" typeHandler="com.example.MyTypeHandler" /><!-- 多个字段中某个字段的类型处理器配置 -->
<resultMap id="departmentResultMap" type="com.example...DepartmentVO"><result property="director" column="director" typeHandler="com.example.MyTypeHandler" />
</resultMap>
<select id="selectPageVO" resultMap="departmentResultMap">select id,name,director from department ...
</select>
3.3 在Wrapper 查询中使用(MyBatis-Plus 3.5.3.2 版本开始
)
Wrappers.<H2User>lambdaQuery().apply("name={0,typeHandler=" + H2userNameJsonTypeHandler.class.getCanonicalName() + "}", "{\"id\":101,\"name\":\"Tomcat\"}"))
总结
-
通过自定义 TypeHandler,可以灵活处理各种复杂的数据类型映射需求,帮助 MyBatis-Plus 在数据库操作中进行数据类型的正确转换,提升系统的稳定性和可维护性。
-
欢迎大家提出建议以及批评,有任何问题可以私信。
官方文档地址
- Mybatis-Plus字段类型处理器