MyBatis传递多个参数的方法

1、使用 hashMap 传递多个参数

使用的是dao层接口动态代理实现的方式
接口中的方法

List<Person> getPersonByAge(Map<String, Object> map)

对应的映射文件

<select id="getPersonByAge" resultType="Person" parameterType="map">
    SELECT * FROM persons WHERE age BETWEEN #{start} AND #{end}
</select>

调用

//hashmap
Map<String, Object> map = new HashMap<>();
map.put("start", 24);
map.put("end", 25);
List<Person> list = personMapper.getPersonByAge(map);
list.forEach(System.out::println);

注意:Map集合中的key要与#{key}相同才可以

2、使用POJO传递多个参数

List<Person> getPerson(Person person);

对应的映射文件

<select id="getPerson" resultType="Person" parameterType="Person">
    SELECT * FROM persons WHERE name = #{name} AND age = #{age}
</select>

调用

//pojo
Person p = new Person();
p.setName("lgh");
p.setAge(25);
List<Person>list = personMapper.getPerson(p);
list.forEach(System.out::println);

对应的pojo实体类

package com.xiya.entity;

import java.time.ZoneId;
import java.util.Date;

/**
 * Created by N3verL4nd on 2017/5/8.
 */
public class Person {
    private int id;
    private String name;
    private int age;
    private Date birth;

    public Person() {
        System.out.println("默认构造函数");
    }

    public Person(int id, String name, int age) {
        System.out.println("构造函数(int id, String name, int age)");
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public Person(String name, int age, Date birth) {
        System.out.println("构造函数(String name, int age, Date birth)");
        this.name = name;
        this.age = age;
        this.birth = birth;
    }

    public int getId() {
        System.out.println("getId");
        return id;
    }

    public void setId(int id) {
        System.out.println("setId");
        this.id = id;
    }

    public String getName() {
        System.out.println("getName");
        return name;
    }

    public void setName(String name) {
        System.out.println("setName");
        this.name = name;
    }

    public int getAge() {
        System.out.println("getAge");
        return age;
    }

    public void setAge(int age) {
        System.out.println("setAge");
        this.age = age;
    }

    public Date getBirth() {
        return birth;
    }

    public void setBirth(Date birth) {
        this.birth = birth;
    }

    @Override
    public String toString() {
        return "Person{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", birth=" + birth.toInstant().atZone(ZoneId.systemDefault()).toLocalDate()
                +
                '}';
    }
}

输出

默认构造函数
setName
setAge
getName//Why
getAge//Why
2017-06-15 10:37:22,568 [main] DEBUG [com.xiya.dao.PersonMapper.getPerson] - ==> Preparing: SELECT * FROM persons WHERE name = ? AND age = ?
getName//填充sql语句,类比下PreparedStatement的使用
getAge//
2017-06-15 10:37:22,600 [main] DEBUG [com.xiya.dao.PersonMapper.getPerson] - > Parameters: lgh(String), 25(Integer)
默认构造函数
setId
setName
setAge
默认构造函数
setId
setName
setAge
默认构造函数
setId
setName
setAge
2017-06-15 10:37:22,626 [main] DEBUG [com.xiya.dao.PersonMapper.getPerson] - <
Total: 3
Person{id=4, name=‘lgh’, age=25, birth=2017-06-15}
Person{id=5, name=‘lgh’, age=25, birth=2017-06-15}
Person{id=7, name=‘lgh’, age=25, birth=2017-06-15}

List容器的生成:首先调用Person实体类的构造函数,然后使用相应的set方法初始化Person实体类对象。使用lambda表达式输出时,找到三条符合条件的记录,所以会有3组输出结果。
感到困惑的是第一个getName和getAge。
我们在getName上设置断点,debug走起。
在这里插入图片描述

从图中可以看到与MyBatis缓存有关。查找资料得知MyBatis有一级缓存和二级缓存。
对于一级缓存,MyBatis是默认开启的,它的处理流程:
在这里插入图片描述

第一次发起查询用户id为1的用户信息,先去找缓存中是否有id为1的用户信息,如果没有,从数据库查询用户信息。
得到用户信息,并将用户信息存储到一级缓存中。
如果sqlSession去执行commit操作(执行插入、更新、删除),清空SqlSession中的一级缓存,这样做的目的为了让缓存中存储的是最新的信息,避免脏读。
第二次发起查询用户id为1的用户信息,先去找缓存中是否有id为1的用户信息,缓存中有,直接从缓存中获取用户信息。
独一无二的SELECT 语句用来区分每一次的查询,也就是要在缓存中保存SELECT语句。

鉴于java基础和MyBatis基础不牢固,分析就只能到这里了。

3、使用@Param注解

注意:这种方法,parameterType属性不能写。

接口中对应的方法

List<Person> getPerson(@Param("age")int age);

对应的映射文件

<select id="getPerson" resultType="Person" >
    SELECT * FROM persons WHERE age = #{age}
</select>

测试

List<Person>list = personMapper.getPerson(25);
list.forEach(System.out.println);

4、如果不使用@Param注解,根据索引查找

不使用注解,就在sql映射配置文件中#{arg0},#{arg1},根据索引来找到对应的参数。
不能使用#{0},#{1},之前看网上说这法写,但是不可以报错。

注意:这种方法,parameterType属性不能写。

测试:

List<Person> getPersons(int start, int end);

<select id="getPersons" resultType="cn.bjut.entity.Person">
  SELECT * FROM persons WHERE age BETWEEN #{start} AND #{end}
</select>

观察报错:

org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.binding.BindingException: Parameter ‘start’ not found. Available parameters are [arg1, arg0, param1, param2]

顾可以如下使用:

List<Person> getPersons(int arg0, int arg1);
<select id="getPersons" resultType="cn.bjut.entity.Person">
  SELECT * FROM persons WHERE age BETWEEN #{arg0} AND #{arg1}
</select>

相应的:arg0与arg1可以替换为param1与param2

转载该博客请点击

发布了60 篇原创文章 · 获赞 16 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_44142032/article/details/94061419
今日推荐