21、参数的传递

参数传递

mybatis的parameterType标签可以可定义传入的数据类型传入的类型分为以下几类
1. 基本数据类型(包括包装类型、String类型、Date类型)
2. List(Set)、Map
3. 实体类型

传入单个参数的示例

Dao层的方法

  Author selectByPrimaryKey(Integer id);

对应的mapper

    <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from author
        where id = #{id,jdbcType=INTEGER}
    </select>

传入到个参数

传入多个参数有三种

使用Map参数

Dao层的方法

 Author selectByPrimaryKey(Map<String,String> map);

Map的实际值map.put(“name”,”zhang”)
map.put(“id”,”1”)
对应的mapper

    <select id="selectByPrimaryKey" parameterType="Map" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from author
        where id = #{id,jdbcType=INTEGER}
        where name = #{name,jdbcType=INTEGER}
    </select>

按位置匹配

Dao层的方法

 Author selectByPrimaryKey(Integer id, String name);

对应的mapper

    <select id="selectByPrimaryKey"  resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from author
        where id = #{0,jdbcType=INTEGER}
        where name = #{1,jdbcType=INTEGER}
    </select>

使用注解匹配

Dao层的方法

 Author selectByPrimaryKey(@Param("id") Integer id, @Param("name") String name);

对应的mapper

    <select id="selectByPrimaryKey"   resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from author
        where id = #{id,jdbcType=INTEGER}
        where name = #{name,jdbcType=INTEGER}
    </select>

推荐用此种方案

使用对象传递参数

Dao层的方法

 Author selectByPrimaryKey(Author author);

对应的mapper

    <select id="selectByPrimaryKey"  parameterType="Author"  resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from author
        where id = #{id,jdbcType=INTEGER}
        where name = #{name,jdbcType=INTEGER}
    </select>

Author对象里面包含有name字段和id字段

区别相同的参数名

如果dao层的方法中的参数有相同的时候
例如

 Author selectByPrimaryKey(@Param("author")Author author,@Param("name")String name);

假如Author也有名字为name的字段,如何使用Author对象中的name字段

```xml
    <select id="selectByPrimaryKey"  parameterType="Author"  resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from author
        where id = #{name,jdbcType=INTEGER}使用的是String类型的name字段
        where name = #{author.name,jdbcType=INTEGER}使用的是author对象中的name字段
    </select>

${}和#{}的区别


  1. #将传入的数据都当成一个字符串,会对自动传入的数据加一个双引号。如:order by #user_id#,如果传入的值是111,那么解析成sql时的值为order by “111”, 如果传入的值是id,则解析成的sql为order by “id”.
  2. sqlorderby user_id$,如果传入的值是111,那么解析成sql时的值为order by user_id, 如果传入的值是id,则解析成的sql为order by id.
  3. #方式能够很大程度防止sql注入。
  4. $方式无法防止Sql注入。
  5. $方式一般用于传入数据库对象,例如传入表名.
  6. 一般能用#的就别用$.

MyBatis排序时使用order by 动态参数时需要注意,用$而不是#

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值