当前位置:   article > 正文

Mybatis_plus 类型装换 UUID JSON_uuid typehandler

uuid typehandler

实体类

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;


}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68

JSON 类型转换器

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;
    }

}


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57

UUID类型转换器

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;


}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68

mybatis-plus 配置文件

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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

mybatis 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="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>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号