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

MyBatis新增數(shù)據(jù)時自增id的兩種寫法小結(jié)

 更新時間:2024年09月27日 11:38:31   作者:惑邊  
本文介紹了在MyBatis中配置自增ID的兩種方法:一種是通過在Mapper文件中設(shè)置useGeneratedKeys和keyProperty,另一種是使用selectKey標(biāo)簽,批量插入時,同樣采用useGeneratedKeys標(biāo)簽,感興趣的可以了解一下

一、單個插入

  • 接口方法:
    public interface PlayerDao {
        int insertOnePlayer(Player player);
        int insertOnePlayer2(Player player);
    }

1.1 方式一

   public void testInsertGenerateId1() throws IOException {
           // 2.獲取sqlSession
           SqlSession sqlSession = sqlSessionFactory.openSession();
           // 3.獲取對應(yīng)mapper
           PlayerDao mapper = sqlSession.getMapper(PlayerDao.class);
           // 4.執(zhí)行查詢語句并返回結(jié)果
           Player player = new Player();
           player.setPlayName("Allen Iverson");
           player.setPlayNo(3);
           player.setTeam("76ers");
           player.setHeight(1.83F);
           mapper.insertOnePlayer(player);
           sqlSession.commit();
           System.out.println(player.getId());
       }
  • Mapper文件:
      <insert id="insertOnePlayer" parameterType="Player" useGeneratedKeys="true" keyProperty="id">
     		insert into tb_player (id, playName, playNo,team, height)
     		values (
                 #{id,jdbcType=INTEGER},
                 #{playName,jdbcType=VARCHAR},
                 #{playNo,jdbcType=INTEGER},
                 #{team,jdbcType=VARCHAR},
                 #{height,jdbcType=DECIMAL}
     		)
     	</insert>
  • 方式一配置:useGeneratedKeys=“true” keyProperty=“id” 即可

1.2 方式二

    public void testInsertGenerateId2() throws IOException {
            // 2.獲取sqlSession
            SqlSession sqlSession = sqlSessionFactory.openSession();
            // 3.獲取對應(yīng)mapper
            PlayerDao mapper = sqlSession.getMapper(PlayerDao.class);
            // 4.執(zhí)行查詢語句并返回結(jié)果
            Player player = new Player();
            player.setPlayName("Tony Parker");
            player.setPlayNo(9);
            player.setTeam("spurs");
            player.setHeight(1.88F);
            mapper.insertOnePlayer2(player);
            sqlSession.commit();
            System.out.println(player.getId());
        }
    
    
    Mapper文件:
   <insert id="insertOnePlayer2" parameterType="Player">
           <selectKey  keyProperty="id" order="AFTER" resultType="int">
               select LAST_INSERT_ID()
           </selectKey>
           insert into tb_player (id, playName, playNo,team, height)
           values (
           #{id,jdbcType=INTEGER},
           #{playName,jdbcType=VARCHAR},
           #{playNo,jdbcType=INTEGER},
           #{team,jdbcType=VARCHAR},
           #{height,jdbcType=DECIMAL}
           )
       </insert>
  • 方式二通過 selectKey 標(biāo)簽完成 ,selectKey 更加靈活,支持一定程度的自定義

二、批量插入

  • Java文件省略了,這里直接給出Mapper文件, Mapper 文件如下,其實就是:useGeneratedKeys=“true” keyProperty=“id”,其中id是JavaBean的主鍵id
  <insert id="insertBatch" parameterType="java.util.List" useGeneratedKeys="true" keyProperty="id">
   INSERT INTO partition_info (id, node_ip_id, init_schema_info_id,
   prefix_table_index, partition_num, start_time,
   end_time, create_time, is_delete
   )
   values
   <foreach collection="list" item="item" index="index" separator=",">
     (#{item.id,jdbcType=INTEGER}, #{item.nodeIpId,jdbcType=INTEGER}, #{item.initSchemaInfoId,jdbcType=INTEGER},
     #{item.prefixTableIndex,jdbcType=VARCHAR}, #{item.partitionNum,jdbcType=VARCHAR}, #{item.startTime,jdbcType=TIMESTAMP},
     #{item.endTime,jdbcType=TIMESTAMP}, #{item.createTime,jdbcType=TIMESTAMP}, #{item.isDelete,jdbcType=TINYINT}
     )
   </foreach>
 </insert>
  • Java代碼
        System.out.println("before insert ...");
        for (PartitionInfo p: list) {
            System.out.println(p);
        }

        PartitionInfoMapper mapper = sqlSession.getMapper(PartitionInfoMapper.class);
        int i = mapper.insertBatch(list);
        System.out.println("The rows be affected :" + i);

        System.out.println("after insert ...");
        for (PartitionInfo p: list) {
            System.out.println(p);
        }
  • 輸出

before insert ...
PartitionInfo(id=null, nodeIpId=1, initSchemaInfoId=1, prefixTableIndex=1, partitionNum=null, startTime=null, endTime=null, createTime=Fri Dec 13 18:26:46 CST 2019, isDelete=null)
PartitionInfo(id=null, nodeIpId=2, initSchemaInfoId=2, prefixTableIndex=2, partitionNum=null, startTime=null, endTime=null, createTime=Fri Dec 13 18:26:46 CST 2019, isDelete=null)
PartitionInfo(id=null, nodeIpId=3, initSchemaInfoId=3, prefixTableIndex=3, partitionNum=null, startTime=null, endTime=null, createTime=Fri Dec 13 18:26:46 CST 2019, isDelete=null)
The rows be affected :3
after insert ...
PartitionInfo(id=701, nodeIpId=1, initSchemaInfoId=1, prefixTableIndex=1, partitionNum=null, startTime=null, endTime=null, createTime=Fri Dec 13 18:26:46 CST 2019, isDelete=null)
PartitionInfo(id=702, nodeIpId=2, initSchemaInfoId=2, prefixTableIndex=2, partitionNum=null, startTime=null, endTime=null, createTime=Fri Dec 13 18:26:46 CST 2019, isDelete=null)
PartitionInfo(id=703, nodeIpId=3, initSchemaInfoId=3, prefixTableIndex=3, partitionNum=null, startTime=null, endTime=null, createTime=Fri Dec 13 18:26:46 CST 2019, isDelete=null) 

  • 這里其他的代碼都省略了,基本上就是: useGeneratedKeys=“true” keyProperty=“id” 這兩個標(biāo)簽起作用
  • 另外我用的mybatis版本是 3.4.1

三、注意

  • 注意Mapper文件中的 insert into tb_player (id, playName, playNo,team, height),這里不要多了一個逗號,之前height后面還有一個逗號導(dǎo)致一直空指針的錯誤。

 到此這篇關(guān)于MyBatis新增數(shù)據(jù)時自增id的兩種寫法小結(jié)的文章就介紹到這了,更多相關(guān)MyBatis新增數(shù)據(jù)時自增id內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Spring?Cloud?Gateway?服務(wù)網(wǎng)關(guān)的部署與使用詳細講解

    Spring?Cloud?Gateway?服務(wù)網(wǎng)關(guān)的部署與使用詳細講解

    這篇文章主要介紹了Spring?Cloud?Gateway?服務(wù)網(wǎng)關(guān)的部署與使用詳細介紹,本文給大家講解的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-04-04
  • Java代碼性能測試實戰(zhàn)之ContiPerf安裝使用

    Java代碼性能測試實戰(zhàn)之ContiPerf安裝使用

    這篇文章主要為大家介紹了Java代碼性能測試實戰(zhàn)之ContiPerf安裝使用,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-06-06
  • 基于Java Gradle復(fù)制項目模塊過程圖解

    基于Java Gradle復(fù)制項目模塊過程圖解

    這篇文章主要介紹了基于Java Gradle復(fù)制項目模塊過程圖解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-06-06
  • springboot實現(xiàn)定時器(一看即會,非常簡單)

    springboot實現(xiàn)定時器(一看即會,非常簡單)

    這篇文章主要介紹了springboot實現(xiàn)定時器(一看即會,非常簡單),具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • Intellij IDEA Debug調(diào)試技巧(小結(jié))

    Intellij IDEA Debug調(diào)試技巧(小結(jié))

    這篇文章主要介紹了Intellij IDEA Debug調(diào)試技巧(小結(jié)),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-10-10
  • SpringBoot配置Redis連接池的實現(xiàn)步驟

    SpringBoot配置Redis連接池的實現(xiàn)步驟

    本文主要介紹了SpringBoot配置Redis連接池的實現(xiàn)步驟,詳細的講解了連接池的作用、配置方式、連接池參數(shù)說明,具有一定的參考價值,感興趣的可以了解一下
    2025-03-03
  • Java之Swagger配置掃描接口以及開關(guān)案例講解

    Java之Swagger配置掃描接口以及開關(guān)案例講解

    這篇文章主要介紹了Java之Swagger配置掃描接口以及開關(guān)案例講解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • SpringBoot整合MQTT小結(jié)匯總

    SpringBoot整合MQTT小結(jié)匯總

    MQTT 客戶端是運行 MQTT 庫并通過網(wǎng)絡(luò)連接到 MQTT 代理的任何設(shè)備,是一種基于發(fā)布/訂閱(publish/subscribe)模式的“輕量級”通訊協(xié)議,該協(xié)議構(gòu)建于 TCP/IP 協(xié)議上,由 IBM 于 1999 年發(fā)明,對SpringBoot整合MQTT相關(guān)知識感興趣的朋友一起看看吧
    2022-01-01
  • 使用IDEA創(chuàng)建maven父子工程項目 (圖文)

    使用IDEA創(chuàng)建maven父子工程項目 (圖文)

    本文主要介紹了使用IDEA創(chuàng)建maven父子工程項目,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-04-04
  • Spring Boot應(yīng)用監(jiān)控的實戰(zhàn)教程

    Spring Boot應(yīng)用監(jiān)控的實戰(zhàn)教程

    Spring Boot 提供運行時的應(yīng)用監(jiān)控和管理功能,下面這篇文章主要給大家介紹了關(guān)于Spring Boot應(yīng)用監(jiān)控的相關(guān)資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-05-05

最新評論