欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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ù)庫中,應(yīng)該是已經(jīng)轉(zhuǎn)換成了數(shù)據(jù)庫的某種類型,必然跟java已經(jīng)沒有關(guān)系了。中間有一些我們看不見的操作做了數(shù)據(jù)處理。

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

思考:

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

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

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

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

二.TypeHandler使用

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

//此接口作用是用于指定jdbc與java的數(shù)據(jù)類型間對應(yīng)關(guān)系處理。
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和字符串互轉(zhuǎn)

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

查詢

查詢的時候 你轉(zhuǎn)的那個字段就配置哪個字段

    <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

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

相關(guān)文章

  • Java中不可變集合的實現(xiàn)方式

    Java中不可變集合的實現(xiàn)方式

    在 Java 編程中,不可變集合是指其內(nèi)容在創(chuàng)建后不能被修改的集合,不可變集合在多線程編程中具有重要作用,因為它可以確保數(shù)據(jù)的一致性和安全性,在本文中,我們將深入探討 Java 中如何實現(xiàn)不可變集合,具體的實現(xiàn)方式,需要的朋友可以參考下
    2025-05-05
  • Java中的?stop?the?world是什么呢

    Java中的?stop?the?world是什么呢

    這篇文章主要介紹了Java中的stop?the?world是什么呢,從字面上講,就是停止這個世界,看到這個字眼,就覺得這是可怕的事情,那到底什么是stop-the-world,本文給大家詳細講解,感興趣的朋友跟隨小編一起看看吧
    2023-05-05
  • Java集合Map常見問題_動力節(jié)點Java學(xué)院整理

    Java集合Map常見問題_動力節(jié)點Java學(xué)院整理

    這篇文章主要為大家詳細整理了Java集合Map常見問題,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-05-05
  • 你肯定能看懂的Java IO相關(guān)知識總結(jié)

    你肯定能看懂的Java IO相關(guān)知識總結(jié)

    群里有大佬說想讓我寫一篇NIO,一直也沒寫,但是和同事聊天也說對Java的IO不是很清晰,因此今天就寫下Java的io,先打個基礎(chǔ),下次寫NIO,需要的朋友可以參考下
    2021-05-05
  • Java 中解決Unsupported major.minor version 51.0的問題

    Java 中解決Unsupported major.minor version 51.0的問題

    本文主要介紹解決Unsupported major.minor version 51.0的問題, 這里給大家整理了詳細資料,有需要的小伙伴可以參考下
    2016-08-08
  • 詳解Java單例模式中的餓漢和懶漢模式

    詳解Java單例模式中的餓漢和懶漢模式

    這篇文章主要介紹了詳解Java單例模式中的餓漢和懶漢模式,單例模式中有兩種模式一種是餓漢模式,一種是懶漢模式,那么他們有什么區(qū)別呢,需要的朋友可以參考下本文
    2023-04-04
  • idea使用pagehelper實現(xiàn)后端分頁功能的步驟詳解

    idea使用pagehelper實現(xiàn)后端分頁功能的步驟詳解

    這篇文章主要介紹了idea使用pagehelper實現(xiàn)后端分頁功能的步驟,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-09-09
  • SpringBoot定時任務(wù)動態(tài)擴展ScheduledTaskRegistrar詳解

    SpringBoot定時任務(wù)動態(tài)擴展ScheduledTaskRegistrar詳解

    這篇文章主要為大家介紹了SpringBoot定時任務(wù)動態(tài)擴展ScheduledTaskRegistrar類示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-01-01
  • Java中ArrayList與順序表的概念與使用實例

    Java中ArrayList與順序表的概念與使用實例

    順序表是指用一組地址連續(xù)的存儲單元依次存儲各個元素,使得在邏輯結(jié)構(gòu)上相鄰的數(shù)據(jù)元素存儲在相鄰的物理存儲單元中的線性表,下面這篇文章主要介紹了Java?ArrayList與順序表的相關(guān)資料,需要的朋友可以參考下
    2022-01-01
  • Java?Bean轉(zhuǎn)Map的那些踩坑實戰(zhàn)

    Java?Bean轉(zhuǎn)Map的那些踩坑實戰(zhàn)

    項目中有時會遇到Map轉(zhuǎn)Bean,Bean轉(zhuǎn)Map的情況,下面這篇文章主要給大家介紹了關(guān)于Java?Bean轉(zhuǎn)Map那些踩坑的相關(guān)資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下
    2022-07-07

最新評論