码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2018-11-13 23:05:54      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:属性   iterable   index   int   prepare   sep   object   suse   com   

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

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

 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.用法区别

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

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

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

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

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

可简单理解:

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

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

如: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注入。

 4.但是如果使用在order by 中就需要使用 $。

 5.在大多数情况下还是经常使用#,但在不同情况下必须使用$. 

二、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 学习---${ }与#{ }获取输入参数的区别、Foreach的用法

标签:属性   iterable   index   int   prepare   sep   object   suse   com   

原文地址:https://www.cnblogs.com/wang-Java-begining/p/9955356.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!