{"id":230647,"date":"2021-12-08T08:55:53","date_gmt":"2021-12-08T00:55:53","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=230647"},"modified":"2021-11-24T09:56:24","modified_gmt":"2021-11-24T01:56:24","slug":"mybatis-batch-insert","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/mybatis-batch-insert.html","title":{"rendered":"mybatis\u63d2\u5165\u6570\u636e\u3001\u6279\u91cf\u63d2\u5165\u6570\u636e"},"content":{"rendered":"
1. UserMapper.java\u63a5\u53e3\u4e2d\u63d2\u5165\u51fd\u6570\u5982\u4e0b\uff1a<\/p>\n
\r\n\/*\r\n * \u65b0\u589e\u7528\u6237-----\u4f7f\u7528useGeneratedKeys\u65b9\u5f0f\r\n * \u6d4b\u8bd5\u4e3b\u952e\u81ea\u589e\u7684\u503c \u8fd9\u91cc \u4e3b\u952e\u81ea\u589e\u7684\u503c\u4e0d\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012 \u76f4\u63a5\u7531\u6570\u636e\u5e93\u90a3\u8fb9\u8d4b\u503c\r\n * *\/\r\n int insert4(SysUser sysUser);\r\n<\/pre>\n2. UserMapper.xml\u4e2dinsert4\u51fd\u6570\u5982\u4e0b\uff1a<\/p>\n
\r\n<insert id="insert4" useGeneratedKeys="true" keyProperty="id">\r\n insert into sys_user (\r\n user_name,user_password,\r\n <if test="userEmail != null and userEmail !=''">\r\n user_email,\r\n <\/if>\r\n user_info,head_img,create_time)\r\n values (\r\n #{userName}, #{userPassword},\r\n <if test="userEmail != null and userEmail !=''">\r\n #{userMail},\r\n <\/if>\r\n #{userInfo},#{headImg, jdbcType = BLOB},\r\n #{createTime, jdbcType= TIMESTAMP}\r\n );\r\n <\/insert>\r\n<\/pre>\n3. \u6d4b\u8bd5\u51fd\u6570\u5982\u4e0b\uff1a<\/p>\n
\r\n@Test\r\n public void testInsert4(){\r\n SqlSession sqlSession = getSqlSession();\r\n try{\r\n UserMapper userMapper = sqlSession.getMapper(UserMapper.class);\r\n \/\/\u521b\u5efa\u4e00\u4e2auser\u5bf9\u8c61\r\n SysUser user = new SysUser();\r\n user.setUserName(\"test1\");\r\n user.setUserPassword(\"123456\");\r\n user.setUserEmail(\"test@mybatis.tk\");\r\n user.setUserInfo(\"test info\");\r\n user.setHeadImg(new byte[]{1,2,3});\r\n user.setCreateTime(new Date());\r\n int result = userMapper.insert3(user);\r\n Assert.assertEquals(1,result);\r\n Assert.assertNotNull(user.getId());\r\n }finally {\r\n sqlSession.commit();\r\n sqlSession.close();\r\n }\r\n }\r\n<\/pre>\n4. \u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n
<\/p>\n
\u4e8c\u3001 mybatis\u6279\u91cf\u63d2\u5165\u6570\u636e<\/strong><\/div>\n1. \u63a5\u53e3\u5982\u4e0b\uff1a<\/p>\n
\r\n\/*\r\n * \u6279\u91cf\u63d2\u5165\u7528\u6237\u4fe1\u606f\r\n * *\/\r\n int insertList(ListuserList);\r\n<\/sysuser><\/pre>\n 2. xml\u6587\u4ef6\u4e2d\uff1a<\/p>\n
\r\n<insert id="insertList">\r\n insert into sys_user(user_name,user_password,\r\n user_email,user_info,head_img,create_time)\r\n values\r\n <foreach collection="list" item="user" separator=",">\r\n #{user.userName},#{user.userPassword},\r\n #{user.userEmail},#{user.userInfo},\r\n #{user.headImg ,jdbcType = BLOB},\r\n #{user.createTime, jdbcType = TIMESTAMP}\r\n <\/foreach>\r\n\r\n <\/insert>\r\n<\/pre>\n\u6ce8\uff1aforeach\u6807\u7b7e<\/p>\n
collection\uff1a\u503c\u4e3a\u8981\u8fed\u4ee3\u5faa\u73af\u7684\u5c5e\u6027\u540d\u3002<\/p>\n
item\uff1a\u53d8\u91cf\u540d\uff0c\u503c\u4e3a\u4ece\u6cd5\u4ee3\u5bf9\u8c61\u4e2d\u53d6\u51fa\u7684\u6bcf\u4e00\u4e2a\u503c\u3002<\/p>\n
index\uff1a\u7d22\u5f15\u7684\u5c5e\u6027\u540d\uff0c\u5728\u96c6\u5408\u6570\u7ec4\u60c5\u51b5\u4e0b\u503c\u4e3a\u5f53\u524d\u7d22\u5f15\u503c \u5f53\u9009\u4ee3\u5faa\u73af\u7684\u5bf9\u8c61\u662f Map\u7c7b\u578b\u65f6\uff0c\u8fd9\u4e2a\u503c\u4e3a Map key \uff08\u952e\u503c\uff09\u3002<\/p>\n
3. \u6d4b\u8bd5\u51fd\u6570\u5982\u4e0b\uff1a<\/p>\n
\r\n@Test\r\n public void testInsertList(){\r\n SqlSession sqlSession = getSqlSession();\r\n try {\r\n UserMapper userMapper = sqlSession.getMapper(UserMapper.class);\r\n ListuserList = new ArrayList<\/sysuser> ();\r\n for(int i=0 ; i<2 ; i++){\r\n SysUser sysUser = new SysUser();\r\n sysUser.setUserName(\"test_\" + i);\r\n sysUser.setUserPassword(\"123456\");\r\n sysUser.setUserEmail(\"wct@qq.com\");\r\n }\r\n int result = userMapper.insertList(userList);\r\n Assert.assertEquals(2,result);\r\n\r\n }finally {\r\n sqlSession.close();\r\n }\r\n\r\n }\r\n<\/sysuser><\/pre>\n","protected":false},"excerpt":{"rendered":" 1. UserMapper.java\u63a5\u53e3\u4e2d\u63d2\u5165\u51fd\u6570\u5982\u4e0b\uff1a \/* * \u65b0\u589e\u7528\u6237—–\u4f7f\u7528useGenerat […]<\/p>\n","protected":false},"author":1903,"featured_media":230661,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-230647","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/230647","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1903"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=230647"}],"version-history":[{"count":3,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/230647\/revisions"}],"predecessor-version":[{"id":230665,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/230647\/revisions\/230665"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/230661"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=230647"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=230647"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=230647"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}