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。
相关文章推荐
- 解析Mybatis连续传递多个参数的方法
- mybatis补充教程之二:传递多个参数的方法(Multiple parameters)
- mybatis补充教程之二:传递多个参数的方法(Multiple parameters)
- mybatis补充教程之二:传递多个参数的方法(Multiple parameters)
- Mybatis传递参数的几种方法
- each用法、mybatis传递多个参数方法
- mybatis 传递多个参数 --解决mybatis查询使用多个参数方法--javabean传统方法和map方法
- myBatis,mapper查询方法参数传递出错
- Mybatis中传递多个参数的4种方法总结
- mybatis 关联映射传递多参数方法
- mybatis 传递多个参数的方法(Multiple parameters)
- MyBatis传递多个参数的方法
- asp.net 三种基本的参数传递方法(web forms)
- JSP传递中文参数乱码问题的解决方法
- 在网页间传递参数的两种常用方法:Querystring和session。
- 用javascritp脚本在页面中传递参数和接受参数的方法
- 静态HTML页面传递参数的方法 (表单)
- 使用xmlhttp.send()方法的参数来传递大容量的数据
- 给Web Services方法传递数组参数
- java学习笔记(四)----对象、数组作为参数传递,静态变量、静态方法的使用,内部类,使用文档注释