mybatis-plus postgresql 組合時對JSONB的字段處理。
以下是必須加的
@TableName(value = "solar_user",autoResultMap = true)
先定義entity
@SuppressWarnings("serial")
@Data
@Accessors(chain = true)
@TableName(value = "solar_user",autoResultMap = true)
public class User extends Model<User> {
private Long id;
private String userName;
private String password;
private Integer sex;
private String email;
private String mobile;
// 只對保存操作有效
@TableField(typeHandler = JsonbTypeHandler.class)
private Profile profile;
}
json處理類
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.baomidou.mybatisplus.extension.handlers.AbstractJsonTypeHandler;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;
import org.postgresql.util.PGobject;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
@MappedTypes({Object.class})
@MappedJdbcTypes({JdbcType.VARCHAR})
public class JsonbTypeHandler extends AbstractJsonTypeHandler<Object> {
private static final PGobject jsonObject = new PGobject();
private final Class<?> type;
public JsonbTypeHandler(Class<?> type) {
this.type = type;
}
@Override
public void setNonNullParameter(PreparedStatement ps, int i, Object parameter, JdbcType jdbcType) throws SQLException {
if (ps != null) {
jsonObject.setType("jsonb");
jsonObject.setValue(JSON.toJSONString(parameter));
ps.setObject(i, jsonObject);
}
}
@Override
public Object getNullableResult(ResultSet rs, String columnName) throws SQLException {
Object v=rs.getObject(columnName);
return toFill(v);
}
@Override
public Object getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
Object v=rs.getObject(columnIndex);
return toFill(v);
}
@Override
public Object getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
Object v=cs.getObject(columnIndex);
return toFill(v);
}
@Override
protected Object parse(String json) {
return JSON.parseObject(json, this.type);
}
//必須將v轉成 PGObject處理
private Object toFill(Object v){
if(v!=null && v instanceof PGobject){
PGobject p=(PGobject)v;
String pv=p.getValue();
if(StringUtils.isNotEmpty(pv)&&(p.getType().equals("jsonb")||p.getType().equals("json"))) {
return parse(p.getValue());
}
}
return v;
}
@Override
protected String toJson(Object obj) {
return JSON.toJSONString(obj, new SerializerFeature[]{SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullListAsEmpty, SerializerFeature.WriteNullStringAsEmpty});
}
}
entity的@TableField 配置的等同如下的
userMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="net.loyin.ucenter.mapper.UserMapper">
<resultMap id="userResultMap" type="net.loyin.ucenter.entity.User">
<id property="id" column="id"/>
<result column="profile" property="profile" jdbcType="OTHER" typeHandler="net.loyin.common.db.JsonbTypeHandler"/>
</resultMap>
<select id="selectUser" parameterType="java.lang.Long" resultMap="userResultMap">
select id,profile from solar_user where id = #{id}
</select>
</mapper>
UserMapper.java
public interface UserMapper extends BaseMapper<User> {
public User selectUser(@Param("id") Long id);
}