Mybatis注解方式@Insert的用法
Mybatis注解方式@Insert
1、不需要返回主鍵
@Insert({"insert into user(name, create_time) values(#{name}, #{createTime, jdbcType=TIMESTAMP})"})
int add(User user);2、返回自增主鍵
@Insert({"insert into user(name, create_time) values(#{name}, #{createTime, jdbcType=TIMESTAMP})" })
@Options(useGeneratedKeys = true, keyProperty = "id")
int add2(Useruser);注解多了一個(gè)@Options, 注解設(shè)置了useGeneratedKeys 和 keyProperty ,用法同xml中的配置。
3、返回非自增主鍵
@Insert({"insert into user(name, create_time) values(#{name}, #{createTime, jdbcType=TIMESTAMP})" })
@SelectKey(statement = "SELECT LAST_INSERT_ID()", keyProperty = "id", resultType = Long.class, before = false)
int add3(User user);配置和xml本質(zhì)上相同的,其中before=false 等同于order=”AFTER”. before=true 等同于order=”BEFORE”.
不同數(shù)據(jù)庫(kù)中order的配置不同這里需要注意下。
Mybatis@Insert注解批量插入數(shù)據(jù)庫(kù)
bean:實(shí)體類
? ? private Integer sysRoleFunctionId; ? ? private String operation; ? ? private Integer sysRoleId;
Mapper
? ? @Insert( "<script>" ?+
? ? ? ? ? ? ? ? "insert into sys_role_function (sys_role_function_id,operation,sys_role_id) ?VALUES " +
? ? ? ? ? ? ? ? "<foreach collection='list' item='item' index='index' separator=','> " +
? ? ? ? ? ? ? ? "(#{item.sysRoleFunctionId},#{item.operation},#{item.sysRoleId}) " +
? ? ? ? ? ? ? ? "</foreach>" +
? ? ? ? ? ? "</script>")
? ? int insertSysRoleFunction (@Param("list")List<SysRoleFunction> list);以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
spring boot RestTemplate 發(fā)送get請(qǐng)求的踩坑及解決
這篇文章主要介紹了spring boot RestTemplate 發(fā)送get請(qǐng)求的踩坑及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08
Java for循環(huán)性能優(yōu)化實(shí)現(xiàn)解析
這篇文章主要介紹了Java for循環(huán)性能優(yōu)化實(shí)現(xiàn)解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-01-01
SpringCloud使用集中配置組件Config規(guī)避信息泄露
項(xiàng)目應(yīng)用中,數(shù)據(jù)庫(kù)連接信息、Access-key、Secret-key等由于其及其敏感和特殊性,一旦泄露出去就很可能會(huì)使得應(yīng)用遭到黑客攻擊,例如數(shù)據(jù)庫(kù)賬號(hào)密碼泄露可能導(dǎo)致“拖庫(kù)”,甚至數(shù)據(jù)丟失。此等事件偶有發(fā)生,那么,在分布式微服務(wù)項(xiàng)目中,怎么避免這種情況呢2022-07-07
java編程實(shí)現(xiàn)根據(jù)EXCEL列名求其索引的方法
這篇文章主要介紹了java編程實(shí)現(xiàn)根據(jù)EXCEL列名求其索引的方法,涉及Java元素遍歷與數(shù)學(xué)運(yùn)算的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-11-11
Java 中文字符按Unicode排序的實(shí)現(xiàn)方法
這篇文章主要介紹了Java 中文字符按Unicode排序的實(shí)現(xiàn)方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-10-10
java網(wǎng)上圖書(shū)商城(2)Category模塊
這篇文章主要介紹了java網(wǎng)上圖書(shū)商城,Category模塊,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12
java多線程編程之使用thread類創(chuàng)建線程
在Java中創(chuàng)建線程有兩種方法:使用Thread類和使用Runnable接口。在使用Runnable接口時(shí)需要建立一個(gè)Thread實(shí)例2014-01-01
SpringBoot接口加密與解密的實(shí)現(xiàn)
這篇文章主要介紹了SpringBoot接口加密與解密的實(shí)現(xiàn)2023-10-10

