Mybatis动态SQL传参

传入单个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>

猜你喜欢

转载自blog.csdn.net/xuexuan_050848/article/details/83277997
今日推荐