mybatis中foreach的用法及#{}和${}的区别

foreach用法

    SQL语法中有时会使用IN关键字,例如id in (1,2,3).可以使用${id}方式取值,但这种写法不能给你防止SQL注入,想避免SQL注入就需要用#{}的方式,这时就要配合使用foreach标签来满足需求。
    foreach包含以下属性:
- collection:必填,值为要迭代循环的属性名,这个属性值的情况有很多。
- item:变量名,值为从迭代对象中取出的每一个值。
- index:索引的属性名,在集合数组情况下值为当前索引值,当迭代循环的对象是Map类型时,这个值为Map的key(键值)。
- open:整个循环内容开头的字符串。
- close:整个循环内容结尾的字符串。
- separator:每次循环的分隔符。

    foreach可以对数组,Map或实现了Iterable接口(如List,Set)的对象进行遍历。数组在处理时会转换为List对象,因此foreach遍历的对象可以分为两大类:Iterable类型和Map类型。这两种类型在遍历时情况不一样。

1.foreach实现in集合

    foreach实现in集合(或数组)是最简单和最常用的一种情况,下面介绍如何根据传入的用户id集合查询出所有符合条件的用户。
1).UseMapper接口中增加如下方法:

/**
*根据用户id集合查询
*@param idList
*@return
/
List<SysUser> selectByIdList(List<Long> idList);

2).在UserMapper.xml中添加如下SQL:

<select id="selectByIdList" resultType="com.hh.domain.SysUser">
    select id,username,password from user where id in
    <foreach collection="list" open="(" close=")" separator="," item="id" index="i">
    #{id}
    </foreach>
</select>
2.foreach实现批量插入

1).UseMapper接口中增加如下方法:

/**
*批量插入用户信息
*@param userList
*@return
*/
int insertList(List<SysUser> userList);

2).在UserMapper.xml中添加如下SQL:

<insert id="insertList">
    insert into user(id,username,password) values 
    <foreach collection="list" item="user" separator=",">
    (
    #{user.id},#{user.username},#{user.password}
    )
    </foreach>
</insert>

注:通过item指定了循环变量名后,在引用值得时候使用的是“属性.属性”的方式,如user.id.

3.foreach实现动态UPDATE

    以参数类型为Map为例来讲解foreach如何实现动态UPDATE.
    当参数是Map类型的时候,foreach标签的index属性值对应的不是索引值,而是Map中的key,利用这个key可以实现动态UPDATE.
1).UseMapper接口中增加如下方法:

/**
*通过Map更新列
*@param map
*@return
*/
int updateByMap(Map<String,Object> map);

2).在UserMapper.xml中添加如下SQL:

<update id="updateByMap">
    update user
    set
    <foreach collection="_parameter" item="val" index="key" separator=",">
        ${key} = #{val}
    </foreach>
    where id=#{id}
</update>

Mybatis中用#{}和${}获取输入参数的区别

    1.“#{}“和“${}”都可以从接口输入中的map对象或者pojo对象中获取输入的参数值。例如:

<mapper namespace="com.hh.dao.UserDao">
    <select id="selectByIdList" resultType="com.hh.domain.SysUser">
        select * from user where id=#{id} and username=#{username}
    </select>
</mapper>

或者

<mapper namespace="com.hh.dao.UserDao">
    <select id="selectByIdList" resultType="com.hh.domain.SysUser">
        select * from user where id=${id} and username=${username}
    </select>
</mapper>

    上面两种形式都可以通过id和username条件查询用户信息,但是这两种形式是有本质的区别:mybatis在处理${}形式时,会直接吧{id}和{username}获取的值拼接到sql中。例如{id}的值为“11”,{username}的值为“hh”,即mybatis直接处理sql语句为:
select * from user where id="11" and username="hh"
    mybatis在处理#{}形式时,会通过jdbc中的PreparedStatement先预编译sql语句为下列形式:
select * from user where id=? and username=?
然后在用PreparedStatement把对应占位符处的值代替占位符。
    2.#{}比${}的优势
        id和username取出来的值会直接拼接到sql语句中,会有安全问题。
        例如:id对应的值为“11”#,则{id}获取到的值为“11”#,此时根本不用管username是什么了,例如username的值为“hh”,则{username}获取到的值为“hh”,mybatis会拼接到sql为:
select * from use where id="11"# and username="hh"
由于sql中#表示注释的意思,所以mybatis就把上面的语句翻译为
select * from user where id="11"
执行词条语句,根本不关心username,因此会有安全问题。
        但是用#{}的形式就不会出现这种情况,对于#{}的形式,mybatis会先提前预编译sql语句,然后再将参数设置到sql语句中,防止sql注入。

猜你喜欢

转载自blog.csdn.net/kbh528202/article/details/80902944
今日推荐