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:對應(yīng)sql的select語句, id:方法名,parameterType:參數(shù)類型,resultMap:返回對象類型(BaseResultMap:標(biāo)簽-->
<!--<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)下標(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<實體類> 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指定遍歷的每一個數(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)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
spring無法引入注解及import org.springframework.web.bind.annota
本文主要介紹了spring無法引入注解及import org.springframework.web.bind.annotation.*報錯的解決,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06
解讀Spring接口方法加@Transactional失效的原因
這篇文章主要介紹了Spring接口方法加@Transactional失效的原因解讀,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03
RestTemplate使用之如何設(shè)置請求頭、請求體
這篇文章主要介紹了RestTemplate使用之如何設(shè)置請求頭、請求體問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07
System.currentTimeMillis()計算方式與時間的單位轉(zhuǎn)換詳解
這篇文章主要介紹了System.currentTimeMillis()計算方式與時間的單位轉(zhuǎn)換詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05

