步骤1:添加依赖
首先,确保你的项目中已经添加了 MyBatis Plus 和 Hutool 的依赖。如果你使用的是 Maven,可以在 pom.xml 文件中添加如下依赖:
<dependencies>
<!-- MyBatis Plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.3</version>
</dependency>
<!-- Hutool -->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.11</version>
</dependency>
</dependencies>
步骤 2: 创建自定义 TypeHandler
接下来,创建一个自定义的 TypeHandler,用于处理 JSONObject 和 String 之间的转换。
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
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;
/**
* 实现实体类和数据库表字段的映射(JSONObject to String)
*/
public class JSONOjectTypeHandler extends BaseTypeHandler<JSONObject> {
@Override
public void setNonNullParameter(PreparedStatement preparedStatement, int i, JSONObject parameter, JdbcType jdbcType) throws SQLException {
preparedStatement.setString(i, parameter.toString());
}
@Override
public JSONObject getNullableResult(ResultSet resultSet, String columnName) throws SQLException {
String jsonString = resultSet.getString(columnName);
return jsonString == null ? null : JSONUtil.parseObj(jsonString);
}
@Override
public JSONObject getNullableResult(ResultSet resultSet, int columnIndex) throws SQLException {
String jsonString = resultSet.getString(columnIndex);
return jsonString == null ? null : JSONUtil.parseObj(jsonString);
}
@Override
public JSONObject getNullableResult(CallableStatement callableStatement, int columnIndex) throws SQLException {
String jsonString = callableStatement.getString(columnIndex);
return jsonString == null ? null : JSONUtil.parseObj(jsonString);
}
}
步骤 3: 在实体类中使用 TypeHandler
在实体类中使用自定义的 TypeHandler,将其应用到相应的字段上。
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import cn.hutool.json.JSONObject;
@TableName("user")
public class User {
private String id;
private String name;
@TableField(typeHandler = JSONObjectTypeHandler.class)
private JSONObject attributes;
// getters and setters
}
步骤 4: 配置 MyBatis Plus(选做)
如果需要全局注册这个 TypeHandler,可以在 MyBatis Plus 的配置类中进行配置。
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;
@Configuration
@MapperScan("com.example.mapper") // 替换为你的 Mapper 所在的包路径
public class MyBatisPlusConfig {
@Bean
public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
MybatisSqlSessionFactoryBean sessionFactory = new MybatisSqlSessionFactoryBean();
sessionFactory.setDataSource(dataSource);
// 注册自定义的 TypeHandler
sessionFactory.setTypeHandlers(new JSONObjectTypeHandler());
return sessionFactory.getObject();
}
}