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ù)庫中order的配置不同這里需要注意下。
Mybatis@Insert注解批量插入數(shù)據(jù)庫
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)文章
Java集合中的fail-fast(快速失敗)機(jī)制詳解
這篇文章主要給大家介紹了關(guān)于Java集合中fail-fast(快速失敗)機(jī)制的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02基于SpringMVC中的路徑參數(shù)和URL參數(shù)實(shí)例
這篇文章主要介紹了基于SpringMVC中的路徑參數(shù)和URL參數(shù)實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-02-02使用socket進(jìn)行服務(wù)端與客戶端傳文件的方法
這篇文章主要介紹了使用socket進(jìn)行服務(wù)端與客戶端傳文件的方法,需要的朋友可以參考下2017-08-08在Spring boot的項(xiàng)目中使用Junit進(jìn)行單體測(cè)試
今天小編就為大家分享一篇關(guān)于spring boot使用Junit進(jìn)行測(cè)試,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2018-12-12SpringBoot返回多種格式的數(shù)據(jù)的實(shí)現(xiàn)示例
本文主要介紹了SpringBoot返回多種格式的數(shù)據(jù)的實(shí)現(xiàn)示例,主要包括了FastJson,xml,pdf,excel,資源流,具有一定的參考價(jià)值,感興趣的可以了解一下2021-10-10@ConfigurationProperties加載外部配置方式
這篇文章主要介紹了@ConfigurationProperties加載外部配置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03java字符轉(zhuǎn)碼的三種方法總結(jié)及實(shí)例
這篇文章主要介紹了 java字符轉(zhuǎn)碼的三種方法總結(jié)及實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-03-03