赞
踩
package demo.gridvo.sensor.sensorzhaoq.pojo; import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.annotation.JSONField; import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; import java.io.Serializable; import java.util.Date; import java.util.UUID; import demo.gridvo.sensor.sensorzhaoq.util.ObjectJsonHandler; import demo.gridvo.sensor.sensorzhaoq.util.SensorTypeEnum; import demo.gridvo.sensor.sensorzhaoq.util.UUIDTypeHandler; import lombok.Data; /** * * @TableName sensor */ //开启注解,否则添加没问题,查询会为null @TableName(autoResultMap = true) @Data public class Sensor implements Serializable { /** * 主键 */ @TableId @TableField(typeHandler = UUIDTypeHandler.class) private UUID id; /** * 名称 */ private String name; /** * 创建时间 */ @JSONField(format = "yyyy-MM-dd HH:mm:ss") private Date createTime; /** * 是否可用 */ private Boolean isDisabled; /** * 扩展信息 */ @TableField(typeHandler = ObjectJsonHandler.class) private JSONObject additionalInfo; /** * 类型 */ @TableField("type") private SensorTypeEnum type; /**固件 * */ private Object firmware; }
package demo.gridvo.sensor.sensorzhaoq.util; import com.alibaba.fastjson.JSONObject; import org.apache.ibatis.type.BaseTypeHandler; import org.apache.ibatis.type.JdbcType; 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(JSONObject.class) public class ObjectJsonHandler extends BaseTypeHandler<JSONObject> { //引入PGSQL提供的工具类PGobject private static final PGobject jsonObject = new PGobject(); @Override public void setNonNullParameter(PreparedStatement ps, int i, JSONObject param, JdbcType jdbcType) throws SQLException { //转换的操作在这里!!! jsonObject.setType("json"); jsonObject.setValue(param.toString()); ps.setObject(i, jsonObject); } @Override public JSONObject getNullableResult(ResultSet rs, String columnName) throws SQLException { String sqlJson = rs.getString(columnName); if (null != sqlJson) { return JSONObject.parseObject(sqlJson); } return null; } //根据列索引,获取可以为空的结果 @Override public JSONObject getNullableResult(ResultSet rs, int columnIndex) throws SQLException { String sqlJson = rs.getString(columnIndex); if (null != sqlJson) { return JSONObject.parseObject(sqlJson); } return null; } @Override public JSONObject getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { String sqlJson = cs.getString(columnIndex); if (null != sqlJson) { return JSONObject.parseObject(sqlJson); } return null; } }
package demo.gridvo.sensor.sensorzhaoq.pojo; import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.annotation.JSONField; import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; import java.io.Serializable; import java.util.Date; import java.util.UUID; import demo.gridvo.sensor.sensorzhaoq.util.ObjectJsonHandler; import demo.gridvo.sensor.sensorzhaoq.util.SensorTypeEnum; import demo.gridvo.sensor.sensorzhaoq.util.UUIDTypeHandler; import lombok.Data; /** * * @TableName sensor */ //开启注解,否则添加没问题,查询会为null @TableName(autoResultMap = true) @Data public class Sensor implements Serializable { /** * 主键 */ @TableId @TableField(typeHandler = UUIDTypeHandler.class) private UUID id; /** * 名称 */ private String name; /** * 创建时间 */ @JSONField(format = "yyyy-MM-dd HH:mm:ss") private Date createTime; /** * 是否可用 */ private Boolean isDisabled; /** * 扩展信息 */ @TableField(typeHandler = ObjectJsonHandler.class) private JSONObject additionalInfo; /** * 类型 */ @TableField("type") private SensorTypeEnum type; /**固件 * */ private Object firmware; }
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #开启sql日志
mapper-locations: classpath*:mapper/*Mapper.xml
type-aliases-package: demo.gridvo.sensor.sensorzhaoqdao
configuration:
map-underscore-to-camel-case: true
type-handlers-package: demo.gridvo.sensor.sensorzhaoq.convert
type-enums-package: demo.gridvo.sensor.sensorzhaoq.convert
<?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="demo.gridvo.sensor.sensorzhaoq.mapper.SensorMapper"> <resultMap id="BaseResultMap" type="demo.gridvo.sensor.sensorzhaoq.pojo.Sensor"> <id property="id" column="id" jdbcType="OTHER"/> <result property="name" column="name" jdbcType="VARCHAR"/> <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/> <result property="isDisabled" column="is_disabled" jdbcType="BOOLEAN"/> <result property="additionalInfo" column="additional_info" jdbcType="OTHER" typeHandler="demo.gridvo.sensor.sensorzhaoq.convert.ObjectJsonHandler"/> <result property="type" column="type" jdbcType="INTEGER"/> <result property="firmware" column="firmware" jdbcType="OTHER"/> </resultMap> <sql id="Base_Column_List"> id,name,create_time, is_disabled,additional_info,type, firmware </sql> </mapper>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。