mybatis中的mapper.xml使用循環(huán)語句
更新時間:2022年02月08日 10:44:14 作者:代碼搬暈工
這篇文章主要介紹了mybatis中的mapper.xml使用循環(huán)語句,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
mapper.xml使用循環(huán)語句
mapper.java,傳的參數(shù)是map
List<實體類> getList(Map<String,Object> paraMap);
mapper.xml
<!--select:對應sql的select語句, id:方法名,parameterType:參數(shù)類型,resultMap:返回對象類型(BaseResultMap:標簽-->
<!--<resultMap id="BaseResultMap" type="實體類包路徑"> 實體類的映射 不改的話一般都是這個名字)-->
<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)下標列式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<實體類> 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相當于執(zhí)行力java的for循環(huán),他的屬性:
collection指定集合的上下文參數(shù)名稱比如這里的@Param("list")item指定遍歷的每一個數(shù)據(jù)的變量,一般叫it,可以使用it.userName來獲取具體的值index集合的索引值,從0開始separator遍歷每條記錄并添加分隔符
除了批量插入,使用SQL in查詢多個用戶時也會使用
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é)點開始時自定義的分隔符
close
表示是節(jié)點結(jié)束時自定義的分隔符
執(zhí)行后會變成:
select * from user where id in (?,?,?)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
PowerJob的ServerDiscoveryService工作流程源碼解讀
這篇文章主要為大家介紹了PowerJob的ServerDiscoveryService工作流程源碼解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-12-12
Jmeter中的timeshift()函數(shù)獲取當前時間進行加減
這篇文章主要介紹了Jmeter中的timeshift()函數(shù)獲取當前時間進行加減,TimeShift(格式,日期,移位,語言環(huán)境,變量)可對日期進行移位加減操作,本文給大家詳細講解,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-10-10
Spring?Boot?利用?XML?方式整合?MyBatis
這篇文章主要介紹了Spring?Boot?利用?XML?方式整合?MyBatis,文章圍繞主題的相關資料展開詳細的內(nèi)容介紹,具有一定的參考價值,組要的小伙伴可以參考一下2022-05-05
Java的Hibernate框架中用于操作數(shù)據(jù)庫的HQL語句講解
這篇文章主要介紹了Java的Hibernate框架中用于操作數(shù)據(jù)庫的HQL語句講解,Hibernate是Java的SSH三大web開發(fā)框架之一,需要的朋友可以參考下2016-01-01

