您的位置:首页 > 其它

MyBatis传递多个参数的方法

2017-06-15 13:44 405 查看

1、使用 hashMap 传递多个参数 

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);


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注解

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);

如果不使用@Param注解,也是可以的。
测试:

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。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: