mybatis中的mapper.xml使用循環(huán)語句
mapper.xml使用循環(huán)語句
mapper.java,傳的參數(shù)是map
List<實(shí)體類> getList(Map<String,Object> paraMap);
mapper.xml
<!--select:對(duì)應(yīng)sql的select語句, id:方法名,parameterType:參數(shù)類型,resultMap:返回對(duì)象類型(BaseResultMap:標(biāo)簽--> <!--<resultMap id="BaseResultMap" type="實(shí)體類包路徑"> 實(shí)體類的映射 不改的話一般都是這個(gè)名字)--> <select id="getList" parameterType="java.util.Map" resultMap="BaseResultMap"> ? select * from table where? ? <!-- 判斷--> ? <if test="a!= null"> ? ? ? a = #{a,jdbcType=VARCHAR} ? </if> ? <if test="list!= null"> ? ? and id in ? ? <!-- for循環(huán), item:循環(huán)后的值, index:循環(huán)下標(biāo)列式for循環(huán)的 i ,collection:參數(shù)名--> ? ? <!-- open="(" close=")" separator="," 就是把循環(huán)的值組成 (item1,item2,item3)的格式--> ? ? <foreach item="item" index="index" collection="list" open="(" close=")" separator=","> ?? ? #{item} ? ? </foreach> ? </if> </select>
參數(shù),數(shù)組,list都行
Map<String,Object> map = new HashMap<String, Object>(); map.put("a","參數(shù)"); map.put("list",數(shù)組、List都行) List<實(shí)體類> list = mapper.getList(map);
mybatis xml循環(huán)語句
MyBatis很好的支持批量插入,使用foreach即可滿足
首先創(chuàng)建DAO方法
package com.youkeda.comment.dao; import com.youkeda.comment.dataobject.UserDO; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; import java.time.LocalDateTime; import java.util.List; @Mapper public interface UserDAO { ? ? int batchAdd(@Param("list") List<UserDO> userDOs); }
<insert id="batchAdd" parameterType="java.util.List" useGeneratedKeys="true" keyProperty="id"> ? ? INSERT INTO user (user_name, pwd, nick_name,avatar,gmt_created,gmt_modified) ? ? VALUES ? ? <foreach collection="list" item="it" index="index" separator =","> ? ? ? ? (#{it.userName}, #{it.pwd}, #{it.nickName}, #{it.avatar},now(),now()) ? ? </foreach > </insert>
foreach相當(dāng)于執(zhí)行力java的for循環(huán),他的屬性:
collection
指定集合的上下文參數(shù)名稱比如這里的@Param("list")item
指定遍歷的每一個(gè)數(shù)據(jù)的變量,一般叫it,可以使用it.userName來獲取具體的值index
集合的索引值,從0開始separator
遍歷每條記錄并添加分隔符
除了批量插入,使用SQL in查詢多個(gè)用戶時(shí)也會(huì)使用
package com.youkeda.comment.dao; import com.youkeda.comment.dataobject.UserDO; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; import java.time.LocalDateTime; import java.util.List; @Mapper public interface UserDAO { ? ? List<UserDO> findByIds(@Param("ids") List<Long> ids); }
<select id="findByIds" resultMap="userResultMap"> ? ? select * from user ? ? <where> ? ? ? ? id in ? ? ? ? <foreach item="item" index="index" collection="ids" ? ? ? ? ? ? ? ? ? ? open="(" separator="," close=")"> ? ? ? ? ? ? #{item} ? ? ? ? </foreach> ? ? </where> </select>
open
表示的是節(jié)點(diǎn)開始時(shí)自定義的分隔符
close
表示是節(jié)點(diǎn)結(jié)束時(shí)自定義的分隔符
執(zhí)行后會(huì)變成:
select * from user where id in (?,?,?)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
PowerJob的ServerDiscoveryService工作流程源碼解讀
這篇文章主要為大家介紹了PowerJob的ServerDiscoveryService工作流程源碼解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12Jmeter中的timeshift()函數(shù)獲取當(dāng)前時(shí)間進(jìn)行加減
這篇文章主要介紹了Jmeter中的timeshift()函數(shù)獲取當(dāng)前時(shí)間進(jìn)行加減,TimeShift(格式,日期,移位,語言環(huán)境,變量)可對(duì)日期進(jìn)行移位加減操作,本文給大家詳細(xì)講解,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-10-10Spring?Boot?利用?XML?方式整合?MyBatis
這篇文章主要介紹了Spring?Boot?利用?XML?方式整合?MyBatis,文章圍繞主題的相關(guān)資料展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,組要的小伙伴可以參考一下2022-05-05Java的Hibernate框架中用于操作數(shù)據(jù)庫的HQL語句講解
這篇文章主要介紹了Java的Hibernate框架中用于操作數(shù)據(jù)庫的HQL語句講解,Hibernate是Java的SSH三大web開發(fā)框架之一,需要的朋友可以參考下2016-01-01