日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

[mybatis]动态sql_foreach_遍历集合批量插入

發(fā)布時間:2023/12/4 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [mybatis]动态sql_foreach_遍历集合批量插入 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

foreach遍歷集合

  • collection:指定要遍歷的集合

  • list類型的參數(shù)會特殊處理封裝在map中,map的key就叫l(wèi)ist

  • item:將當前遍歷出的元素賦值給指定的變量

  • #{變量名}就能取出變量的值也就是當前遍歷出的元素

  • separator:每個元素之間的分隔符

  • open:遍歷出所有結果拼接一個開始的字符

  • close:遍歷出所有結果拼接一個結束的字符

  • index:索引。遍歷list的時候 index就是索引
    遍歷map的時候index表示的就是map的key,item就是map的值

public List<Employee> getEmpsByConditionForeach(@Param("ids") List<Integer> ids); <!-- public List<Employee> getEmpsByConditionForeach(@Param("ids")List<Integer> employee);--> <select id="getEmpsByConditionForeach" resultType="com.atguigu.mybatis.bean.Employee">select * from tb1_employee<!--collection:指定要遍歷的集合list類型的參數(shù)會特殊處理封裝在map中,map的key就叫l(wèi)istitem:將當前遍歷出的元素賦值給指定的變量#{變量名}就能取出變量的值也就是當前遍歷出的元素separator:每個元素之間的分隔符open:遍歷出所有結果拼接一個開始的字符close:遍歷出所有結果拼接一個結束的字符index:索引。遍歷list的時候 index就是索引遍歷map的時候index表示的就是map的key,item就是map的值--><foreach collection="ids" item="item_id" separator="," open = "where id in (" close = ")">#{item_id}</foreach></select>
  • 注意:這里的collection只能填list或者是map,如果想填ids,需要在參數(shù)上加@Param注解
@Testpublic void test02() throws IOException {SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();SqlSession sqlSession = sqlSessionFactory.openSession();try{EmployeeMapperDynamicSQL mapper = sqlSession.getMapper(EmployeeMapperDynamicSQL.class);List<Employee> list = mapper.getEmpsByConditionForeach(Arrays.asList(1, 2, 3, 4));for (Employee emp : list){System.out.println(emp);}//手動提交數(shù)據(jù)sqlSession.commit();}finally {sqlSession.close();}}

foreach批量插入

  • 方式一
public void addEmps(@Param("emps") List<Employee> emps); <insert id="addEmps">INSERT INTO tb1_employee(last_name,email,gender,d_id)values<foreach collection="emps" item = "emp" separator=",">(#{emp.lastName},#{emp.email},#{emp.gender},#{emp.dept.id})</foreach></insert> @Testpublic void test04() throws IOException {SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();SqlSession sqlSession = sqlSessionFactory.openSession();try{EmployeeMapperDynamicSQL mapper = sqlSession.getMapper(EmployeeMapperDynamicSQL.class);List<Employee> emps = new ArrayList<>();emps.add(new Employee(null,"smitdash","smitadsh@qq.com","1",new Department(1)));emps.add(new Employee(null,"allsadasen","allesadn@qq.com","0",new Department(1)));mapper.addEmps(emps);sqlSession.commit();}finally {sqlSession.close();}}
  • 方式二
    這種方式需要數(shù)據(jù)庫連接屬性allowMultiQueries=true

dbconfig.properties:

jdbc.url=jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&allowMultiQueries=true <insert id="addEmps"><foreach collection="emps" item = "emp" separator=";">INSERT INTO tb1_employee(last_name,email,gender,d_id)values(#{emp.lastName},#{emp.email},#{emp.gender},#{emp.dept.id})</foreach></insert>

總結

以上是生活随笔為你收集整理的[mybatis]动态sql_foreach_遍历集合批量插入的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內容還不錯,歡迎將生活随笔推薦給好友。