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

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

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

一、單個(gè)插入

  • 接口方法:
    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.獲取對(duì)應(yīng)mapper
           PlayerDao mapper = sqlSession.getMapper(PlayerDao.class);
           // 4.執(zhí)行查詢語(yǔ)句并返回結(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.獲取對(duì)應(yīng)mapper
            PlayerDao mapper = sqlSession.getMapper(PlayerDao.class);
            // 4.執(zhí)行查詢語(yǔ)句并返回結(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 文件如下,其實(shí)就是: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” 這兩個(gè)標(biāo)簽起作用
  • 另外我用的mybatis版本是 3.4.1

三、注意

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

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

相關(guān)文章

  • 淺談Java安全之C3P0的使用

    淺談Java安全之C3P0的使用

    本文主要介紹了淺談Java安全之C3P0的使用,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • dubbo轉(zhuǎn)http方式調(diào)用方式

    dubbo轉(zhuǎn)http方式調(diào)用方式

    當(dāng)前項(xiàng)目計(jì)劃去掉網(wǎng)關(guān)層,前端將直接通過HTTP調(diào)用后端dubbo服務(wù),為此,后端需新增controller層,實(shí)現(xiàn)請(qǐng)求兼容和統(tǒng)一轉(zhuǎn)發(fā),保證前端調(diào)用方式不變
    2025-10-10
  • Java之map的常見用法講解與五種循環(huán)遍歷實(shí)例代碼理解

    Java之map的常見用法講解與五種循環(huán)遍歷實(shí)例代碼理解

    map是一組鍵值對(duì)的組合,通俗理解類似一種特殊的數(shù)組,a[key]=val,只不過數(shù)組元素的下標(biāo)是任意一種類型,而且數(shù)組的元素的值也是任意一種類型。有點(diǎn)類似python中的字典。通過"鍵"來取值,類似生活中的字典,已知索引,來查看對(duì)應(yīng)的信息
    2021-09-09
  • IDEA之web項(xiàng)目導(dǎo)入jar包方式

    IDEA之web項(xiàng)目導(dǎo)入jar包方式

    這篇文章主要介紹了IDEA之web項(xiàng)目導(dǎo)入jar包方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-05-05
  • Springboot 如何使用@Async整合線程池

    Springboot 如何使用@Async整合線程池

    這篇文章主要介紹了Springboot 使用@Async整合線程池的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • myatisplus的saveOrUpdate的提交總是update問題

    myatisplus的saveOrUpdate的提交總是update問題

    這篇文章主要介紹了myatisplus的saveOrUpdate的提交總是update問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-11-11
  • 淺談Spring自定義注解從入門到精通

    淺談Spring自定義注解從入門到精通

    這篇文章主要介紹了淺談Spring自定義注解從入門到精通,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • 詳解mybatis 批量更新數(shù)據(jù)兩種方法效率對(duì)比

    詳解mybatis 批量更新數(shù)據(jù)兩種方法效率對(duì)比

    這篇文章主要介紹了詳解mybatis 批量更新數(shù)據(jù)兩種方法效率對(duì)比,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-02-02
  • Java微服務(wù)分布式調(diào)度Elastic-job環(huán)境搭建及配置

    Java微服務(wù)分布式調(diào)度Elastic-job環(huán)境搭建及配置

    Elastic-Job在配置中提供了JobEventConfiguration,支持?jǐn)?shù)據(jù)庫(kù)方式配置,會(huì)在數(shù)據(jù)庫(kù)中自動(dòng)創(chuàng)建JOB_EXECUTION_LOG和JOB_STATUS_TRACE_LOG兩張表以及若干索引,來記錄作業(yè)的相關(guān)信息
    2023-02-02
  • SSH框架網(wǎng)上商城項(xiàng)目第15戰(zhàn)之線程、定時(shí)器同步首頁(yè)數(shù)據(jù)

    SSH框架網(wǎng)上商城項(xiàng)目第15戰(zhàn)之線程、定時(shí)器同步首頁(yè)數(shù)據(jù)

    這篇文章主要為大家詳細(xì)介紹了SSH框架網(wǎng)上商城項(xiàng)目第15戰(zhàn)之線程、定時(shí)器同步首頁(yè)數(shù)據(jù),感興趣的小伙伴們可以參考一下
    2016-06-06

最新評(píng)論

南京市| 大新县| 漯河市| 改则县| 牟定县| 正定县| 海城市| 龙海市| 西平县| 佳木斯市| 镶黄旗| 大连市| 孙吴县| 海阳市| 郴州市| 河东区| 宁夏| 那坡县| 花莲市| 白玉县| 弥渡县| 武陟县| 冷水江市| 丹江口市| 静宁县| 宜章县| 盘山县| 德格县| 宣城市| 宜兰市| 神木县| 宁晋县| 枣阳市| 虞城县| 盐城市| 枣庄市| 江达县| 汪清县| 武宁县| 甘泉县| 卫辉市|