MyBatis動態(tài)SQL foreach標(biāo)簽實(shí)現(xiàn)批量插入的方法示例
需求:查出給定id的記錄:
<select id="getEmpsByConditionForeach" resultType="comtestbeansEmployee">
SELECT * FROM tb1_emplyee WHERE id IN
<foreach collection="list" item="item_id" separator="," open="(" close=")">
#{item_id}
</foreach>
</select>
關(guān)于foreach標(biāo)簽,有幾個屬性應(yīng)該注意一下:
- collection:指定要遍歷的集合:
- list類型的參數(shù)會特殊處理封裝在map中,map的key就叫l(wèi)ist
- item:將當(dāng)前遍歷出的元素賦值給指定的變量
- separator:每個元素之間的分隔符
- open:遍歷出所有結(jié)果拼接一個開始的字符
- close:遍歷出所有結(jié)果拼接一個結(jié)束的字符
- index:索引。遍歷list的時候是index就是索引,item就是當(dāng)前值
- 遍歷map的時候index表示的就是map的key,item就是map的值
- #{變量名}就能取出變量的值也就是當(dāng)前遍歷出的元素
測試方法:
@Test
public void testDynamicSqlTest() throws IOException{
SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
//1、獲取到的SqlSession不會自動提交數(shù)據(jù)
SqlSession openSession = sqlSessionFactoryopenSession();
try
{
EmployeeMapperDymanicSQL mapper=openSessiongetMapper(EmployeeMapperDymanicSQLclass);
/*Employee employee=new Employee(1,"lili",null,"1");*/
List<Employee> emps=mappergetEmpsByConditionForeach(ArraysasList(1,2,3,4));
for (Employee e:emps){
Systemoutprintln(e);
}
}
finally {
openSessionclose();
}
}
foreach標(biāo)簽也可以實(shí)現(xiàn)實(shí)現(xiàn)批量插入(刪除)數(shù)據(jù):
這里以批量插入數(shù)據(jù)為例:
<insert id="addEmps">
INSERT INTO tb1_emplyee(last_name,email,gender,d_id)
VALUES
<foreach collection="emps" item="emp" separator=",">
(#{emplastName},#{empemail},#{empgender},#{empdeptid})
</foreach>
</insert>
對應(yīng)的接口:
public void addEmps(@Param("emps")List<Employee> emps);
測試方法
@Test
public void testBatchSave() throws IOException{
SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
//1、獲取到的SqlSession不會自動提交數(shù)據(jù)
SqlSession openSession = sqlSessionFactoryopenSession();
try
{
EmployeeMapperDymanicSQL mapper=openSessiongetMapper(EmployeeMapperDymanicSQLclass);
List<Employee> emps=new ArrayList<Employee>();
empsadd(new Employee(null,"Eminem","Eminem@com","1",new Department(1)));
empsadd(new Employee(null,"2Pac","2Pac@com","1",new Department(1)));
mapperaddEmps(emps);
openSessioncommit();
}
finally {
openSessionclose();
}
}
到此這篇關(guān)于MyBatis動態(tài)SQL foreach標(biāo)簽實(shí)現(xiàn)批量插入的方法示例的文章就介紹到這了,更多相關(guān)MyBatis動態(tài)SQL foreach批量插入內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Mybatis中一條SQL使用兩個foreach的問題及解決
- 基于mybatis注解動態(tài)sql中foreach工具的方法
- mybatis 查詢sql中in條件用法詳解(foreach)
- Mybatis動態(tài)SQL foreach標(biāo)簽用法實(shí)例
- mybatis foreach批量插入數(shù)據(jù):Oracle與MySQL區(qū)別介紹
- Mybatis中動態(tài)SQL,if,where,foreach的使用教程詳解
- Mybatis動態(tài)SQL之if、choose、where、set、trim、foreach標(biāo)記實(shí)例詳解
- Mybatis動態(tài)SQL?foreach批量操作方法
相關(guān)文章
Java中如何動態(tài)創(chuàng)建接口的實(shí)現(xiàn)方法
這篇文章主要介紹了Java中如何動態(tài)創(chuàng)建接口的實(shí)現(xiàn)方法的相關(guān)資料,需要的朋友可以參考下2017-09-09
SpringBoot整合mybatisplus和druid的示例詳解
這篇文章主要介紹了SpringBoot整合mybatisplus和druid的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-08-08
Java設(shè)計(jì)模式中的設(shè)計(jì)原則之合成復(fù)用原則詳解
這篇文章主要介紹了Java設(shè)計(jì)模式中的設(shè)計(jì)原則之合成復(fù)用原則詳解,原則是盡量使用合成/聚合的方式,而不是使用繼承聚合關(guān)系表示的是整體和部分的關(guān)系,整體與部分可以分開,可以理解為成員變量和當(dāng)前類的關(guān)系就是聚合關(guān)系,需要的朋友可以參考下2023-11-11
Java 并發(fā)編程ArrayBlockingQueue的實(shí)現(xiàn)
這篇文章主要介紹了Java 并發(fā)編程ArrayBlockingQueue的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02
java項(xiàng)目導(dǎo)出為.exe執(zhí)行文件的方法步驟
最近做了個項(xiàng)目,想要轉(zhuǎn)換成可執(zhí)行文件,那么java項(xiàng)目如何導(dǎo)出為.exe執(zhí)行文件,本文就介紹一下,主要使用jar2exe軟件,感興趣的可以了解一下2021-05-05

