Mybatis 传参数
valten Lv4

#{}与${}

注意以下两个符号的使用:

  • **#{}**:MyBatis创建预处理语句属性从而设置安全的值(比如?)。常用作查询条件的值,例如:where name=#{value}。
    该参数可以指定一个确切的数据类型,

    例如: #{property,javaType=int,jdbcType=NUMERIC}.

  • **${}**: MyBatis不会修改或转义字符串,将会直接在SQL语句中插入一个不改变的字符串,常用于拼凑sql的实体部分,
    例如:select * from ${tableName}

在执行SQL时MyBatis会自动通过对象中的属性给SQL中参数赋值,它会自动将Java类型转换成数据库的类型。而一旦传入的是null它就无法准确判断这个类型应该是什么,就有可能将类型转换错误,从而报错。

要解决这个问题,需要针对这些可能为空的字段,手动指定其转换时用到的类型。

一般情况下,我们没有必要按个字段去识别/判断它是否可以为空,而是将所有的字段都当做可以为空,全部手动设置转换类型。

#{} 会自动补单引号 order by ‘num asc’

${} 不会自动补单引号 order by num asc

参数是list

Oracle批量新增

1
2
3
4
5
6
7
8
9
<insert id="addBatch" parameterType="list">
insert into tb_gl_ypbg_clxx_gxc (id, bg_id, gxlx, car_no, car_track, del )
<foreach collection="list" item="item" index="index" separator="union all">
select #{item.id,jdbcType=VARCHAR}, #{item.bgId,jdbcType=VARCHAR},
#{item.gxlx,jdbcType=VARCHAR}, #{item.carNo,jdbcType=VARCHAR},
#{item.carTrack,jdbcType=VARCHAR}, '0'
from dual
</foreach>
</insert>

Oracle批量修改

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<update id="updateBatch" parameterType="list">
<foreach collection="list" item="item" index="index" open="begin" close=";end;" separator=";">
update tb_gl_ypbg_clxx_gxc
<set>
<if test="item.gxlx != null and item.gxlx != ''">
gxlx = #{item.gxlx,jdbcType=VARCHAR},
</if>
modify_username = #{item.modifyPid,jdbcType=VARCHAR},
modify_truename = #{item.modifyUser,jdbcType=VARCHAR},
modify_date = sysdate,
modify_unit = #{item.modifyUnit,jdbcType=VARCHAR},
modify_unit_code = #{item.modifyUnitCode,jdbcType=VARCHAR}
</set>
where id = #{item.id,jdbcType=CHAR} and del = '0'
</foreach>
</update>

批量删除

1
2
3
4
5
6
<delete id="deleteAjypYpbgByIds" parameterType="list">
delete from ajyp_ypbg where ASJBH in
<foreach item="asjbh" collection="list" open="(" separator="," close=")">
#{asjbh}
</foreach>
</delete>

四种传参方式

方式一、顺序传参

mapper.java文件:

1
public User selectUser(String name, int deptId);

mapper.xml文件:

1
2
3
<select id="selectUser" resultType="com.wyj.entity.po.User">
select * from user where userName = #{0} and deptId = #{1}
</select>

注意:里面的数字代表你传入参数的顺序,不是特别建议使用这种方法传递参数,特别是参数个数多的时候

方式二、注解@Param传参

mapper.java文件:

1
public User selectUser(@Param("userName") String name, int @Param("deptId") id);

mapper.xml文件:

1
2
3
<select id="selectUser" resultType="com.wyj.entity.po.User">
select * from user where userName = #{userName} and deptId = #{deptId}
</select>

注意:在xml文件中就只能以在@Param注解中声明的参数名称获取参数

方式三、Map集合传参

mapper.java文件:

1
public User selectUser(Map<String, Object> params);

mapper.xml文件:

1
2
3
<select id="selectUser" parameterType="java.util.Map" resultType="com.wyj.entity.po.User">
select * from user where userName = #{userName} and deptId = #{deptId}
</select>

方式四、JavaBean实体类传参

mapper.java文件:

1
public User selectUser(User user);

mapper.xml文件:

1
2
3
<select id="selectUser" parameterType="com.wyj.entity.po.User" resultType="com.wyj.entity.po.User">
select * from user where userName = #{userName} and deptId = #{deptId}
</select>
  • 本文标题:Mybatis 传参数
  • 本文作者:valten
  • 创建时间:2020-09-25 13:30:56
  • 本文链接:https://valtenhyl.github.io/Mybatis/mybatis-parameters/
  • 版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
 评论