Mybatis動態SQL傳參
阿新 • • 發佈:2018-11-07
傳入單個String
public void selectExample(String param);
<!-- Mybatis不做特殊處理,xml中引數名隨便寫什麼Mybatis都可以取到 -->
<select id="selectExample" parameterType="java.lang.String" resultMap="resultMap">
select * from user where name = #{name}
</select>
傳入多個String
public void selectExample (String name,String level);
<!-- Mybatis會把傳入的引數自動封裝成Map型別
* Map 的key值就是從param1...paramN ..
* map.put("param1",name)
* map.put("param2",level)
* 引數名不能隨便寫,必須為Map的key-->
<select id="selectExample" parameterType="java.lang.String" resultMap="resultMap">
select * from user where name = #{param1} and level = #{param2}
</select>
// 使用@Param註解自定義Map封裝資料的key值
public void selectExample(@Param("name") String name,@Param("level") String level);
<select id="selectExample" parameterType="java.lang.String" resultMap="resultMap">
select * from user where name = #{name} and level = #{level}
</ select>
傳入集合
public int deleteByList(List<String> list);
public int deleteByArray(String[] ary);
<!--Mybatis會把傳入的引數自動封裝成Map型別
* List:map.put("list",list) key為list
* Array:map.put("array",ary) key為array
* -->
<delete id="deleteByList">
delete from user where id in
<foreach collection="list" open="(" separator="," close=")" item="item">
#{item}
</foreach>
</delete>
<delete id="deleteByArray">
delete from user where id in
<foreach collection="array" open="(" separator="," close=")" item="item">
#{item}
</foreach>
</delete>