最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

MyBatis中TypeHandler的使用教程詳解

 更新時間:2024年12月01日 11:40:56   作者:不想做咸魚的王富貴  
在我們平常開發(fā)操作數(shù)據(jù)庫時,查詢、插入數(shù)據(jù)等操作行為,有時會報數(shù)據(jù)類型不匹配異常,就可以得知數(shù)據(jù)的類型是不唯一的必然是多種不同的數(shù)據(jù)類型,本文給大家介紹了MyBatis中TypeHandler的使用教程,需要的朋友可以參考下

一.TypeHandler作用及其使用場景

在我們平常開發(fā)操作數(shù)據(jù)庫時,查詢、插入數(shù)據(jù)等操作行為,有時會報數(shù)據(jù)類型不匹配異常,就可以得知數(shù)據(jù)的類型是不唯一的必然是多種不同的數(shù)據(jù)類型。并且我們必須要明確的一點就是java作為一門編程語言有自己的數(shù)據(jù)類型,數(shù)據(jù)庫也是有自己的數(shù)據(jù)類型的。

jdbc數(shù)據(jù)類型:org.apache.ibatis.type.JdbcType 此枚舉就是所有的數(shù)據(jù)庫支持類型

java數(shù)據(jù)類型:int、long、string、…

一定要分清,例如java重的date數(shù)據(jù)插入到數(shù)據(jù)庫中,應該是已經轉換成了數(shù)據(jù)庫的某種類型,必然跟java已經沒有關系了。中間有一些我們看不見的操作做了數(shù)據(jù)處理。

假設此時的java類型與數(shù)據(jù)庫數(shù)據(jù)類型是一樣的,哪么其他語言中的日期數(shù)據(jù)插入數(shù)據(jù)庫時又該怎么解釋,例如C#操作數(shù)據(jù)庫存入時間類型,C#與java肯定沒有關系吧。所以每種語言與數(shù)據(jù)庫之間有種數(shù)據(jù)類型關系對應。

思考:

因為java與數(shù)據(jù)庫各自有數(shù)據(jù)類型,所以在將java數(shù)據(jù)存入數(shù)據(jù)庫前中間是否有其他操作,是我們看不見的,不然java數(shù)據(jù)怎么知道自己與哪個jdbc數(shù)據(jù)類型匹配?

答:mybatis框架為每種數(shù)據(jù)類型做了默認的關系對應,BaseTypeHandler的所有實現(xiàn)類,就是來做這些處理的。

例如:java中的date插入數(shù)據(jù)庫時是jdbc哪種類型,怎么就是這種類型? 中間具體有什么操作?

答:DateTypeHandler就是來解決date數(shù)據(jù)類型的處理。

二.TypeHandler使用

我們想要自定義去處理Java和JDBC的數(shù)據(jù)類型轉換時,需要實現(xiàn)TypeHandler接口,該接口源碼如下:

//此接口作用是用于指定jdbc與java的數(shù)據(jù)類型間對應關系處理。
public interface TypeHandler<T> {
  // 保存操作,數(shù)據(jù)入庫之前時數(shù)據(jù)處理
  void setParameter(PreparedStatement ps, int i, T parameter, JdbcType jdbcType) throws SQLException;
  //下面三個則是,從數(shù)據(jù)庫加載數(shù)據(jù)后,vo對象封裝前的數(shù)據(jù)處理
  T getResult(ResultSet rs, String columnName) throws SQLException;
  T getResult(ResultSet rs, int columnIndex) throws SQLException;
  T getResult(CallableStatement cs, int columnIndex) throws SQLException;
}

JsonIntegerTypeHandler 實現(xiàn)Integer和字符串互轉

public class JsonIntegerTypeHandler extends BaseTypeHandler<Integer> {
    private static final ObjectMapper mapper = new ObjectMapper();

    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, Integer parameter, JdbcType jdbcType) throws SQLException {
        ps.setString(i, toJson(parameter));
    }

    @Override
    public Integer getNullableResult(ResultSet rs, String columnName) throws SQLException {
        return this.toObject(rs.getString(columnName));
    }

    @Override
    public Integer getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        return this.toObject(rs.getString(columnIndex));
    }

    @Override
    public Integer getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        return this.toObject(cs.getString(columnIndex));
    }

    private String toJson(Integer params) {
        try {
            String copyObject = IotDbUtils.copyObject(params);
            return copyObject;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    private Integer toObject(String content) {
        if (content != null && !content.isEmpty()) {
            try {
                System.out.println("1111111111111"+content);
                return (Integer) mapper.readValue(content, Integer.class);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        } else {
            return null;
        }
    }
}

Mapper.xml

查詢

查詢的時候 你轉的那個字段就配置哪個字段

    <resultMap id="DmpDeviceReportResult" type="com.chinaunicom.iotdb.domain.DmpDeviceReportInformation" >
        <result column="Time" property="timestamp" />

        <result column="type" jdbcType="INTEGER"
                property="type" typeHandler="com.chinaunicom.iotdb.handler.JsonIntegerTypeHandler"/>
        

    </resultMap>

  <select id="listDeviceReportInformation" resultMap="DmpDeviceReportResult">
        select trace_id, imei, topic, information, interaction_time,type
        from dmp_device_report_information
        where imei = #{serialNumber}
        <if test="beginTime != null and endTime != null">
            and interaction_time between #{beginTime} and #{endTime}
        </if>
        <if test="traceId != null and traceId != ''">
            and trace_id = #{traceId}
        </if>
        limit #{pageSize}  offset #{pageNum}
    </select>

新增

<insert id="add" parameterType="com.chinaunicom.iotdb.domain.DmpDeviceReportInformation">
        INSERT INTO root.device.dmp_device_report_information
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="null != type ">
                type,
            </if>
            <if test="null != traceId and '' != traceId">
                trace_id,
            </if>
            <if test="null != imei and '' != imei">
                imei,
            </if>
            <if test="null != topic and '' != topic">
                topic,
            </if>
            <if test="null != information and '' != information">
                information,
            </if>
            <if test="null != interactionTime  and '' != interactionTime ">
                interaction_time,
            </if>
            <if test="null != organizationId ">
                organization_id
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="null != type ">
                #{type,typeHandler=com.chinaunicom.iotdb.handler.JsonIntegerTypeHandler },
            </if>
            <if test="null != traceId and '' != traceId">
                #{traceId,typeHandler=com.chinaunicom.iotdb.handler.JsonIntegerTypeHandler },
            </if>
            <if test="null != imei and '' != imei">
                #{imei,typeHandler=com.chinaunicom.iotdb.handler.JsonIntegerTypeHandler },
            </if>
            <if test="null != topic and '' != topic">
                #{topic,typeHandler=com.chinaunicom.iotdb.handler.JsonIntegerTypeHandler },
            </if>
            <if test="null != information and '' != information">
                #{information,typeHandler=com.chinaunicom.iotdb.handler.JsonIntegerTypeHandler },
            </if>
            <if test="null != interactionTime and '' != interactionTime ">
                #{interactionTime,typeHandler=com.chinaunicom.iotdb.handler.JsonIntegerTypeHandler },
            </if>
            <if test="null != organizationId ">
                #{organizationId,typeHandler=com.chinaunicom.iotdb.handler.JsonIntegerTypeHandler },
            </if>
        </trim>
    </insert>

mybatisPlus中使用

類注解 @TableName(autoResultMap = true)
參數(shù)注解 @TableField(typeHandler = JsonIntegerTypeHandler.class)

@Data
@TableName(autoResultMap = true)
public class DmpDeviceReportInformation implements Serializable
{
    private static final long serialVersionUID = 1L;

    private Long id;

    /**
     * 消息類型1:消息上報 2:消息下發(fā)
     */
 @TableField(typeHandler = JsonIntegerTypeHandler.class)
    private Integer type;
    }

注意: 如果使用自己的mapper查詢 要選擇mybaits的形式

要想全局生效的話

mybatis-plus:
   type-handlers-package: com.chinaunicom.iotdb.handler

到此這篇關于MyBatis中TypeHandler的使用教程詳解的文章就介紹到這了,更多相關MyBatis TypeHandler使用內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • Java中的RMI使用方法詳解

    Java中的RMI使用方法詳解

    這篇文章主要介紹了Java中的RMI使用方法,RMI是Java提供的一個完善的簡單易用的遠程方法調用框架,采用客戶服務器通信方式,在服務器上部署了提供各種服務的遠程對象,下面我們來詳細講解
    2023-10-10
  • 詳解Spring-Boot中如何使用多線程處理任務

    詳解Spring-Boot中如何使用多線程處理任務

    本篇文章主要介紹了詳解Spring-Boot中如何使用多線程處理任務,具有一定的參考價值,感興趣的小伙伴們可以參考一下。
    2017-03-03
  • Spring整合Mybatis框架方法剖析

    Spring整合Mybatis框架方法剖析

    這篇文章主要為大家介紹了Spring整合Mybatis框架方法剖析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-07-07
  • Java消息隊列JMS實現(xiàn)原理解析

    Java消息隊列JMS實現(xiàn)原理解析

    這篇文章主要介紹了Java消息隊列JMS實現(xiàn)原理解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-03-03
  • maven安裝、使用、配置本地倉庫、idea配置maven以及解決plugins報錯問題

    maven安裝、使用、配置本地倉庫、idea配置maven以及解決plugins報錯問題

    本地倉庫是遠程倉庫的一個緩沖和子集,當你構建Maven項目時首先會從本地倉庫查找資源,如果沒有那么Maven會從遠程倉庫下載到你本地倉庫,這篇文章主要給大家介紹了關于maven安裝、使用、配置本地倉庫、idea配置maven以及解決plugins報錯問題的相關資料,需要的朋友可以參考下
    2024-01-01
  • 手寫Java?LockSupport的示例代碼

    手寫Java?LockSupport的示例代碼

    LockSupport給我們提供了一個非常強大的功能,它是線程阻塞最基本的元語,他可以將一個線程阻塞也可以將一個線程喚醒,因此經常在并發(fā)的場景下進行使用。本文將用60行代碼實現(xiàn)手寫LockSupport,需要的可以參考一下
    2022-08-08
  • MyBatis?核心組件Configuration實例詳解

    MyBatis?核心組件Configuration實例詳解

    Configuration用于描述 MyBatis 的主配置信息,其他組件需要獲取配置信息時,直接通過 Configuration 對象獲取,這篇文章主要介紹了MyBatis核心組件Configuration,需要的朋友可以參考下
    2023-08-08
  • 詳解Java中的敏感信息處理

    詳解Java中的敏感信息處理

    平時開發(fā)中常常會遇到像用戶的手機號、姓名、身份證等敏感信息需要處理,這篇文章主要為大家整理了一些常用的方法,希望對大家有所幫助
    2025-01-01
  • java二叉樹面試題詳解

    java二叉樹面試題詳解

    下面小編就為大家?guī)硪黄猨ava二叉樹的幾道面試題詳解。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2021-07-07
  • Spring?Cloud?Hystrix原理與注意事項小結

    Spring?Cloud?Hystrix原理與注意事項小結

    本文介紹了Hystrix的基本概念、工作原理以及其在實際開發(fā)中的應用方式,通過對Hystrix的深入學習,開發(fā)者可以在分布式系統(tǒng)中實現(xiàn)精細的錯誤處理機制,并能夠及時響應系統(tǒng)中的異常,避免服務的連鎖崩潰,感興趣的朋友一起看看吧
    2025-03-03

最新評論

舟山市| 阿拉尔市| 扎鲁特旗| 凌源市| 兴化市| 滨海县| 长海县| 霍林郭勒市| 阿克| 阳春市| 平陆县| 长沙县| 衡阳市| 会昌县| 土默特右旗| 水富县| 靖边县| 安顺市| 平定县| 昭觉县| 桃江县| 台中县| 吴堡县| 阿坝县| 襄垣县| 红原县| 平潭县| 墨竹工卡县| 蒙自县| 治多县| 莱芜市| 奈曼旗| 乐亭县| 阳江市| 孝感市| 疏勒县| 金华市| 吉安县| 庄河市| 洪洞县| 多伦县|