【mybatis】mybatis自定义类型处理器,自定义TypeHandler接口

1.测试数据库表

CREATE TABLE test_tbl_department(
	id varchar(32) NOT NULL,
	name varchar(32) NOT NULL,
	tel varchar(18) DEFAULT NULL,
	PRIMARY KEY(id)
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4;

CREATE TABLE test_tbl_user(
	id varchar(32) NOT NULL,
	version int(11) NOT NULL DEFAULT '0',
	name varchar(32) NOT NULL,
	age int(3) DEFAULT NULL,
	birthday datetime DEFAULT NULL,
	department_id varchar(32) NOT NULL,
	sorder int(11) NOT NULL DEFAULT '1',
	deleted tinyint(1) NOT NULL DEFAULT '0',
	jsonUser varchar(100) DEFAULT NULL
	primary key(id)
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4;

2.测试实体类

jsonUser存储User的json类型的信息,通过自定义类型处理器,将JSONObject对象转为字符串类型,或将VARCHAR类型转为JSONObject类型。

建表时,user的department_id为非空字段,但是User实体类中并没有给出。通过自定义类型处理器,将department_id自动转为Department对象,或将Department自动转为department_id字符串。

@Data
public class User{
   
	private String id;
	private String name;
	private Integer age;
	private Date birthday;
	private Department department;
	private JSONObject jsonUser;
}

@Data
public class Department {
   
	private String id;
	private String name;
	private String tel;
	private Set<User> users;
}

3.测试接口

public interface UserDao {
   
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MyBatis中,你可以使用自定义类型处理器TypeHandler)来处理自定义的Map类型类型处理器用于在Java对象和数据库字段之间进行转换。 以下是一种实现自定义类型处理器处理自定义的Map的示例: 1. 定义一个自定义的Map类型,例如`CustomMap`: ```java public class CustomMap extends HashMap<String, Object> { // 添加自定义的方法或属性 } ``` 2. 实现一个自定义类型处理器,继承自`org.apache.ibatis.type.BaseTypeHandler`类,并实现对应的方法。 ```java import org.apache.ibatis.type.BaseTypeHandler; import org.apache.ibatis.type.JdbcType; import java.sql.CallableStatement; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class CustomMapTypeHandler extends BaseTypeHandler<CustomMap> { @Override public void setNonNullParameter(PreparedStatement ps, int i, CustomMap parameter, JdbcType jdbcType) throws SQLException { // 将CustomMap转换为需要的数据类型,并设置到PreparedStatement中 // ps.setXXX(i, convertedValue); } @Override public CustomMap getNullableResult(ResultSet rs, String columnName) throws SQLException { // 从ResultSet中获取指定列名的值,并将其转换为CustomMap类型 // Object columnValue = rs.getXXX(columnName); // CustomMap map = convertToCustomMap(columnValue); // return map; return null; } @Override public CustomMap getNullableResult(ResultSet rs, int columnIndex) throws SQLException { // 与上面类似,只是根据列索引获取值 return null; } @Override public CustomMap getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { // 与上面类似,只是在CallableStatement中获取值 return null; } } ``` 在上述示例中,我们继承了`BaseTypeHandler`类,并重写了父类的方法,在这些方法中进行了自定义类型的转换逻辑。 3. 在MyBatis配置文件中,注册自定义类型处理器。 ```xml <typeHandlers> <typeHandler handler="com.example.CustomMapTypeHandler"/> </typeHandlers> ``` 通过以上步骤,你就可以使用自定义的Map类型,并通过自定义类型处理器来处理该类型的转换逻辑。在数据库操作时,MyBatis会自动调用类型处理器来进行转换。你可以根据实际需求,在类型处理器中编写相应的转换逻辑,将自定义的Map类型数据库字段进行转换。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值