Mybatis 学习---${ }与#{ }获取输入参数的区别、Foreach的用法

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

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

 1 <mapper namespace="com.hh.dao.UserDao">
 2     <select id="selectByIdList" resultType="com.hh.domain.SysUser">
 3         select * from user where id=#{id} and username=#{username}  4     </select>
 5 </mapper>
 6 或者  7 
 8 <mapper namespace="com.hh.dao.UserDao">
 9     <select id="selectByIdList" resultType="com.hh.domain.SysUser">
10         select * from user where id=${id} and username=${username} 11     </select>
12 </mapper>

2.用法区别sql

  mybatis在处理${}形式时,会直接把{id}和{username}获取的值拼接到sql中;如{id}值为“10”,{username}值为“张三”,即直接处理数组

select * from user where id="10" and username="张三" 

  mybatis在处理#{}形式时,会经过jdbc中的PreparedStatement先预编译sql语句为一个参数占位符,以下列形式:mybatis

select * from user where id=? and username=? 

而后在用PreparedStatement把对应占位符处的值代替占位符app

可简单理解:dom

  ${}传入值时,sql解析时,参数是不带引号的。spa

  #{} 传入值时,sql解析时,参数是带引号code

如:name-->cy
  eg:  
  select id,name,age from student where name=#{name}   -- name='cy'

   select id,name,age from student where name=${name}   -- name=cy 

 3.#{}的形式,mybatis会先提早预编译sql语句,而后再将参数设置到sql语句中,防止sql注入。
xml

 4.可是若是使用在order by 中就须要使用 $。对象

 5.在大多数状况下仍是常常使用#,但在不一样状况下必须使用$. 

2、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>
相关文章
相关标签/搜索