mybaitis批量插入怎么得到批量返回的自增ID mybaitis批量插入怎么得到批量返回的自增ID

mybaitis\u6279\u91cf\u63d2\u5165\u600e\u4e48\u5f97\u5230\u6279\u91cf\u8fd4\u56de\u7684\u81ea\u589eID

1,\u5bf9\u4e8e\u652f\u6301\u751f\u6210\u81ea\u589e\u4e3b\u952e\u7684\u6570\u636e\u5e93:useGenerateKeys\u548ckeyProperty\u3002 2,\u4e0d\u652f\u6301\u751f\u6210\u81ea\u589e\u4e3b\u952e\u7684\u6570\u636e\u5e93:\u3002

1\u3001\u5bf9\u4e8e\u652f\u6301\u751f\u6210\u81ea\u589e\u4e3b\u952e\u7684\u6570\u636e\u5e93\uff1auseGenerateKeys\u548ckeyProperty\u3002 2\u3001\u4e0d\u652f\u6301\u751f\u6210\u81ea\u589e\u4e3b\u952e\u7684\u6570\u636e\u5e93\uff1a\u3002 \u4f46\u662f\u600e\u5bf9\u6279\u91cf\u63d2\u5165\u6570\u636e\u8fd4\u56de\u81ea\u589e\u4e3b\u952e\u7684\u89e3\u51b3\u65b9\u5f0f\u7f51\u4e0a\u770b\u5230\u7684\u8fd8\u662f\u6bd4\u8f83\u5c11\uff0c\u81f3\u5c11\u767e\u5ea6\u7684\u7ed3\u679c\u6bd4\u8f83\u5c11\u3002

1、对于支持生成自增主键的数据库:useGenerateKeys和keyProperty。

2、不支持生成自增主键的数据库:<selectKey>。

但是怎对批量插入数据返回自增主键的解决方式网上看到的还是比较少,至少百度的结果比较少。

Mybatis官网资料提供如下:

First, if your database supports auto-generated key fields (e.g. MySQL and SQL Server), then you can simply set useGeneratedKeys="true" and set the keyProperty to the target property and you're done. For example, if the Authortable above had used an auto-generated column type for the id, the statement would be modified as follows:

<insert id="insertAuthor" useGeneratedKeys="true"
keyProperty="id">
insert into Author (username,password,email,bio)
values (#{username},#{password},#{email},#{bio})
</insert>
If your database also supports multi-row insert, you can pass a list or an array of Authors and retrieve the auto-generated keys.

<insert id="insertAuthor" useGeneratedKeys="true"
keyProperty="id">
insert into Author (username, password, email, bio) values
<foreach item="item" collection="list" separator=",">
(#{item.username}, #{item.password}, #{item.email}, #{item.bio})
</foreach>
</insert>
从官网资料可以看出Mybatis是支持批量插入时返回自增主键的。(百度上说不支持的,多打脸 开玩笑的)

但是在本地测试的时候使用上述方式确实不能返回自增id,而且还报错(不认识keyProperty中指定的Id属性),然后在网上找相关资料。终于在Stackoverflow上面找到了一些信息。

解决办法:

1、升级Mybatis版本到3.3.1。

2、在Dao中不能使用@param注解。

3、Mapper.xml中使用list变量接受Dao中的集合。

参考地址:

http://stackoverflow.com/questions/18566342/mybatis-use-generated-keys-for-batch-insert
http://stackoverflow.com/questions/28453475/mybatis-getting-id-from-inserted-array-of-object-returns-error
https://github.com/mybatis/mybatis-3/pull/324
http://stackoverflow.com/questions/28453475/mybatis-getting-id-from-inserted-array-of-object-returns-error#
https://github.com/abel533/mybatis-3/blob/master/src/main/java/org/apache/ibatis/executor/keygen/Jdbc3KeyGenerator.java

  • Mybatis 璇濡備綍鎵ц鎵归噺鎻掑叆?
    绛旓細Mybatis 鎵归噺鎻掑叆鏈変簲绉嶆柟寮忥紝鍒嗗埆鏄細1. 寰幆鎻掑叆锛2. JDBC 鎵瑰鐞嗭紱3. MyBatis 鐨勬墜鍔ㄦ壒閲忔彁浜わ紱4. MyBatis 浠ラ泦鍚堟柟寮忔壒閲忔柊澧锛5. MyBatis-Plus 鎻愪緵鐨 SaveBatch 鏂规硶鍜 InsertBatchSomeColumn 鏂规硶銆傚叾涓紝鎺ㄨ崘浣跨敤 SaveBatch 鏂规硶鍜 InsertBatchSomeColumn 鏂规硶锛屽洜涓哄畠浠彲浠ヨ嚜鍔ㄥ鐞嗕富閿拰绱㈠紩...
  • Mybatis涓濡備綍瀹炵幇鎵归噺鏁版嵁鐨鎻掑叆,璇峰啓鍑洪厤缃枃浠剁殑閰嶇疆淇℃伅浠ュ強Java浠 ...
    绛旓細method涓哄搴旂殑宸ュ巶绫讳腑鐨勬柟娉曪紝鏂规硶涓殑@Param(鈥渓ist鈥)鏄洜涓鎵归噺鎻掑叆浼犲叆鐨勬槸涓涓猯ist锛屼絾鏄Mybatis浼氬皢鍏跺寘瑁呮垚涓涓猰ap銆傚叾涓璵ap鐨刱ey涓衡渓ist鈥濓紝value涓轰紶鍏ョ殑list銆
  • mybatis鍔ㄦ乻ql瀹炵幇鎵归噺鎻掑叆
    绛旓細榛樿鎯呭喌涓嬶紝 #{}璇硶浼氫績浣MyBatis鐢熸垚PreparedStatement灞炴у苟涓斾娇鐢≒reparedStatement鐨勫弬鏁(=?)鏉ヨ缃笺傚鏋滀綘鎯崇洿鎺ュ皢鏈洿鏀圭殑瀛楃涓蹭唬鍏ュ埌sql涓紝鍙互浣跨敤${}銆備篃灏辨槸璇达紝MyBatis鐪嬪埌 #{}浼氳涓轰綘鍦ㄧ粰sql涓殑鍙橀噺璧嬪硷紝灏卞儚JDBC缂栫▼涓粰闂彿璧嬪间竴鏍(姣斿MyBatis浼氬垽鏂畠鐨勭被鍨嬶紝骞惰嚜鍔ㄥ湪鍓嶅悗鍔犲崟...
  • mybatis鎵归噺鎿嶄綔,鎬庝箞瀹炵幇
    绛旓細鏂规硶涓锛<insert id="insertbatch" parameterType="java.util.List"> <selectKey keyProperty="fetchTime" order="BEFORE"resultType="java.lang.String"> SELECT CURRENT_TIMESTAMP()</selectKey> insert into kangaiduoyaodian ( depart1, depart2, product_name,generic_name, img, product_specificati...
  • mybatis 鎵归噺鎿嶄綔鏁版嵁
    绛旓細mybatis鐨勬壒閲忔搷浣滄湁涓ょ鏂瑰紡锛涓鏄娇鐢╢oreach鏍囩锛屼簩鏄娇鐢╩ybatis鐨凚ATCH妯″瀷 鍦▁ml涓氳繃foreach瀵硅〃鏁版嵁杩涜寰幆鎿嶄綔 鍦╫racle涓笉鏀寔insert into product(name, type, price) values ('a', 'tv', 1233), ('b', 'ac', 3455),...('','','')杩欑褰㈠紡鐨剆ql锛屽洜姝racle鎵归噺鎻掑叆浣跨敤 in...
  • mybaitis鎵归噺鎻掑叆鎬庝箞寰楀埌鎵归噺杩斿洖鐨勮嚜澧濱D
    绛旓細1銆佸浜庢敮鎸佺敓鎴愯嚜澧炰富閿殑鏁版嵁搴擄細useGenerateKeys鍜宬eyProperty銆2銆佷笉鏀寔鐢熸垚鑷涓婚敭鐨勬暟鎹簱锛<selectKey>銆備絾鏄庡鎵归噺鎻掑叆鏁版嵁杩斿洖鑷涓婚敭鐨勮В鍐虫柟寮忕綉涓婄湅鍒扮殑杩樻槸姣旇緝灏戯紝鑷冲皯鐧惧害鐨勭粨鏋滄瘮杈冨皯銆Mybatis瀹樼綉璧勬枡鎻愪緵濡備笅锛欶irst, if your database supports auto-generated key fields (e.g. ...
  • mybatis鎬庝箞鎵归噺鎻掑叆鏁版嵁搴
    绛旓細绗竴绉嶆柟娉曪細浣犲彲浠ュ湪鎻掑叆涓婚敭鐨勬椂鍊欑敤搴忓垪鐢熸垚锛屽氨鏄负涓婚敭id璧嬪紉xx_seq.nextval锛岃繖涓簭鍒楁槸浣犲垱寤虹殑杩欎釜table鐨勫簭鍒 绗簩绉嶆柟娉曪細浣犲彲浠ユ寜鐓фゼ涓婅鐨勶紝鐢ㄤ富閿槧灏勶紝褰撶劧瀵逛簬mysql杩欑鏈夎嚜澧炰富閿拰oracle杩欑鐢ㄥ簭鍒楃殑闇瑕佷笉鍚岀殑璁剧疆锛屾ゼ涓婁唬鐮 寰堟竻妤氾紝涓嶅仛璧樿堪銆傜涓夌鏂规硶锛氭湁浜鸿涓嶇敤娣诲姞涓婚敭锛...
  • mybatis 鎵归噺鎻掑叆SqlServer 濡備綍瀹炵幇
    绛旓細insert into user <foreach collection="list" item="item" index="index" separator="UNION ALL"> select #{item.name},${item.age} </foreach>
  • MyBatis 鎵ц鍣ㄧ殑鎵ц鏁堢巼濡備綍鎻愬崌?
    绛旓細MyBatis 鎵ц鍣ㄧ殑鎵ц鏁堢巼鍙互閫氳繃浠ヤ笅鏂瑰紡杩涜鎻愬崌锛1. 浣跨敤 BatchExecutor锛屽畠鍙互閲嶅浣跨敤宸茬粡棰勫鐞嗙殑璇彞锛屽苟涓旀壒閲忔墽琛屾墍鏈夎鍙ワ紝澶ф壒閲忔ā寮忎笅鎬ц兘鏇翠紭銆2. 鍦 MyBatis 鐨勬槧灏勬枃浠朵腑浣跨敤 foreach 鏍囩鏉ュ疄鐜鎵归噺鎻掑叆銆3. 灏 MyBatis session 鐨 executor type 璁句负 Batch锛岀劧鍚庡娆℃墽琛屾彃鍏ヨ鍙ャ
  • mybatis-plus瑙e喅 sqlserver鎵归噺鎻掑叆list鎶ラ敊
    绛旓細瑙e喅鐗堟湰:3.0.6 鍘熷洜鍒嗘瀽:mybatis-plus榛樿浣跨敤Jdbc3KeyGenerator杩涜娣诲姞,浣嗘槸sqlserver涓嶆敮鎸佹壒閲忚繑鍥瀒d锛屾墍浠ヤ細鎶涘嚭濡備笅寮傚父 瑙e喅鏂规: 閲嶅啓榛樿saveBatch鍜宻aveOrUpdateBatch(缂虹偣鏄鎵归噺娣诲姞涓嶈兘杩斿洖id锛屽浜庝笉闇瑕佽繑鍥瀒d鐨勫満鏅傜敤)灏咼dbc3KeyGenerator鏇挎崲涓篘oKeyGenerator 绗竴姝: 寤虹珛NoahSqlMethod...
  • 扩展阅读:mixkit素材官网入口 ... mybatisplus批量更新 ... mybatis高效批量insert ... mybatis-plus面试题 ... mybatis 批量插数据 ... mybatisplus批量查询 ... mybatisplus批量insert ... mybatis selectkey ... mybatis批量insert的几种方式 ...

    本站交流只代表网友个人观点,与本站立场无关
    欢迎反馈与建议,请联系电邮
    2024© 车视网