Mybatis使用之参数传递

https://blog.csdn.net/crave_shy/article/details/45872375

一:简介
      

       主要记录Mybatis是如何映射传递的参数的。分四种来记录:1、java基本类型的传递、2、Java对象形式传递 3、多参数传递4、集合类型参数传递

 

二:具体方式
 

      2.1 java基本类型
 

       以整形为例、映射文件:

 

    <select id="getAuthorById" parameterType="int" resultType="org.alien.mybatis.samples.model.Author">
        SELECT *
        FROM author
        WHERE id = #{id}
    </select>

       映射方法:

    Author getAuthorById(int id);

       测试代码:

 

    private AuthorMapper authorMapper;
 
    public AuthorMapperTest() {
        authorMapper = MybatisUtil.getSqlSession().getMapper(AuthorMapper.class);
    }
 
    @Test
    public void testGetAuthorById() throws Exception {
        Author author = authorMapper.getAuthorById(1);
        Assert.assertNotNull(author);
    }

      2.2. java对象
      

       以Author类为例、映射文件:

    <select id="getAuthorWithValidate" parameterType="org.alien.mybatis.samples.model.Author"
            resultType="org.alien.mybatis.samples.model.Author">
        SELECT *
        FROM author
        WHERE username = #{username} AND password = #{password}
    </select>

       映射方法:

    Author getAuthorWithValidate(Author author);

       测试方法:

 

    @Test
    public void testGetAuthorWithValidate() throws Exception {
        Author author = authorMapper.getAuthorWithValidate(new Author("star_year", "alien"));
        Assert.assertNotNull(author);
    }

      2.3 多参数
 

       多参数分为两种:1、将参数放在Map中。 2、使用Mybatis的注解@Param来指定。

 

       Map类型映射文件:

 

    <select id="getAuthorByMultiCondition" parameterType="hashMap" resultType="author">
        SELECT *
        FROM
            (
                SELECT
                    A.*,
                    ROWNUM RN
                FROM (SELECT *
                      FROM author
                      WHERE username LIKE '%' || #{username} || '%') A
                WHERE ROWNUM <= #{endRecord}
            )
        WHERE RN >= #{startRecord}
    </select>

       Map类型映射方法:

 

    List<Author> getAuthorByMultiCondition(Map<String, Object> map);

       Map类型测试方法:

 

    @Test
    public void testGetAuthorByMultiCondition() throws Exception {
        Map<String, Object> map = new HashMap<>();
        map.put("startRecord", 0);
        map.put("endRecord", 10);
        map.put("username", "star_year");
        List<Author> authors = authorMapper.getAuthorByMultiCondition(map);
        Assert.assertNotNull(authors);
    }

       @Param类型映射文件:

 

    <select id="getAuthorByUsername" resultType="Author">
        SELECT *
        FROM author
        WHERE username LIKE '%' || #{username} || '%' AND email LIKE '%' || #{email} || '%'
    </select>

       @Param类型映射方法:

 

    List<Author> getAuthorByUsername(@Param("username") String username, @Param("email") String email);

       @Param类型测试方法:

 

    @Test
    public void testGetAuthorByUsername() throws Exception {
        List<Author> authorList = authorMapper.getAuthorByUsername("star_year", "46185");
        Assert.assertNotNull(authorList);
    }

      2.4 集合类型参数
 

       常用与根据一系列id来做一些操作。

       以Author类为例、映射文件:

    <select id="getAuthorByIdCollection" resultType="author">
        select * from author where id in
        <foreach collection="list" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>

       映射方法:

    List<Author> getAuthorByIdCollection(List<Integer> idList);

       测试方法:

 

    @Test
    public void testGetAuthorByIdCollection() throws Exception {
        List<Integer> idList = new ArrayList<>();
        idList.add(1);
        idList.add(2);
        List<Author> authorList = authorMapper.getAuthorByIdCollection(idList);
        Assert.assertNotNull(authorList);
    }


三:${paramName} 与 #{paramName}区别


        使用#{parameterName}引用参数的时候,Mybatis会把这个参数认为是一个字符串,例如传入参数是“Smith”,那么在SQL(Select * from emp where name = #{employeeName})使用的时候就会转换为Select * from emp where name = 'Smith';同时在SQL(Select * from emp where name = ${employeeName})使用的时候就会转换为Select * from emp where name = Smith。
       再次,从安全性上考虑,能使用#尽量使用#来传参,因为这样可以有效防止SQL注入的问题。


四:补充


        更多内容:Mybatis 目录

        github地址:https://github.com/andyChenHuaYing/scattered-items/tree/master/items-mybatis

        源码下载地址:http://download.csdn.net/detail/chenghuaying/8713311
 

       Author 类:

public class Author {
    private int id;
    private String username;
    private String password;
    private String email;
    private String bio;
    private String favouriteSection;
  
    //getter setter ...
}

--------------------- 
作者:AlienStar 
来源:CSDN 
原文:https://blog.csdn.net/crave_shy/article/details/45872375 
版权声明:本文为博主原创文章,转载请附上博文链接!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值