目录
1.parameterType
2.dao接口方法参数一个简单类型的参数
3.dao接口方法中有多个简单类型的参数
4.dao接口方法使用一个对象作为参数
5.dao接口中多个简单类型的参数,使用位置
6.dao接口参数是一个Map
参数就是通过java程序把数据传入到mapper文件中的sql语句。参数主要是指dao接口方法的形参
1.parameterType
parameterType:表示参数的类型,指定dao方法的形参数据类型。这个形参的数据类型是给mybatis使用。mybatis是给sql语句的参数赋值时使用。
PreparedStatement.setXXX(位置, 值)
要想理解这里必须对JDBC中的PreparedStatement有足够的了解,推荐看我之前写的一篇文章
我们来看mapper文件中select查询语句
<select id="selectStudentById" resultType="com.lu.entity.Student">
select id, name, email, age from student where id = #{studentId}
</select>
mybatis要执行的sql语句为:
select id, name, email, age from student where id = ?
?是占位符,使用JDBC中的PreparedStatement执行这样的sql语句
PreparedStatement ps = conn.prepareStatement("select id, name, email, age from student where id = ?");
我们给 ? 位置进行赋值
- 如果参数是Integer,则执行ps.setInt(1, 1005)
- 如果参数是String,则执行ps.setString(1, "1005")
那么parameterType的第一个用法:
java类型的全限定类型名称 parameterType = "java.lang.Integer"
那么代码就是这样写:
<select id="selectStudentById" parameterType="java.lang.Integer" resultType="com.lu.entity.Student">
select id, name, email, age from student where id = #{studentId}
</select>
第二种用法,使用别名
别名 | 映射的类型 |
_byte | byte |
_long | long |
_short | short |
_int | int |
_integer | int |
_double | double |
_float | float |
_boolean | boolean |
string | String |
byte | Byte |
long | Long |
short | Short |
int | Integer |
integer | Integer |
double | Double |
float | Float |
boolean | Boolean |
date | Date |
decimal | BigDecimal |
bigdecimal | BigDecimal |
object | Object |
map | Map |
hashmap | HashMap |
list | List |
arraylist | ArrayList |
collection | Collection |
iterator | Iterator |
既然我们这里是Integer类型,那么别名就是int,那么代码就是这么写
<select id="selectStudentById" parameterType="int" resultType="com.lu.entity.Student">
select id, name, email, age from student where id = #{studentId}
</select>
我们在测试类中测试一下
发现是可以执行的
但是我们发现一个问题,好像parameterType不写也是可以的,没错,parameterType不是强制的,mybatis通过反射机制是可以得到参数类型的,绝大多数情况下我们不需要写这个参数。
2.dao接口方法参数一个简单类型的参数
如果dao接口中方法的形参是一个简单类型,那么mapper文件中,获取这个参数值,使用#{任意字符}
简单类型:八种基本类型 + String
我们来讲一个例子,首先在dao接口中定义一个方法
package com.lu.dao;
import com.lu.entity.Student;
public interface StudentDao {
Student selectStudentByEmail(String email);
}
在mapper文件中来写查询代码
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.lu.dao.StudentDao">
<!--使用insert,update,delete,select标签写sql-->
<select id="selectStudentByEmail" resultType="com.lu.entity.Student">
select id, name, email, age from student where email = #{studentEmail}
</select>
</mapper>
我们去测试类中去调用
@Test
public void testselectStudentByEmail() {
SqlSession session = MyBatisUtil.getSqlSession();
StudentDao dao = session.getMapper(StudentDao.class);
Student student = dao.selectStudentByEmail("zhangsan@qq.com");
System.out.println(student);
session.close();
}
控制台输出:
3.dao接口方法中有多个简单类型的参数
这里我们要使用@Param这个注解
@Param:命名参数,在方法的形参前面使用的,定义参数名。
我们先来看一下不适用这个注解会出现什么情况
我们在dao接口中重新定义一个方法
List<Student> selectByNameOrAge(String name, Integer age);
在mapper文件中写代码
<select id="selectByNameOrAge" resultType="com.lu.entity.Student">
select id, name, email, age from student where name = #{studentName} or age = #{studentAge}
</select>
在测试类中测试
@Test
public void testSelectByNameOrAge() {
SqlSession session = MyBatisUtil.getSqlSession();
StudentDao dao = session.getMapper(StudentDao.class);
List<Student> students = dao.selectByNameOrAge("张三", 25);
students.forEach(student -> System.out.println(student));
session.close();
}
控制台输出:
报错了,mybatis不能识别多个参数,所以这时候我们就需要使用@Param注解
该注解的使用位置在dao接口中
List<Student> selectByNameOrAge(@Param("myname") String name, @Param("myage") Integer age);
当使用了@Param后,例如@Param("myname"),在mapper文件中,使用#{命名的参数},
例如#{myname},那么mapper文件中就需要改成
<select id="selectByNameOrAge" resultType="com.lu.entity.Student">
select id, name, email, age from student where name = #{myname} or age = #{myage}
</select>
重新在测试类汇总进行测试
可以看到测试成功了,这种方式只针对少量参数,如果参数比较多,四五个或者更多,那么这样就会非常繁琐,如果参数非常多,我们推荐使用对象的方式
4.dao接口方法使用一个对象作为参数
方法的形参是一个java对象,这个java对象表示多个参数。使用对象的属性值作为参数使用。
下面我们来举一个例子
我们首先在dao接口中定义一个方法
/*
* 一个java对象作为参数(对象有属性,每个属性有set,get方法)
*/
List<Student> selectByObject(Student student);
然后在mapper文件中写sql语句
<!--
一个java对象作为方法的参数,使用对象的属性作为参数值使用
简单地语法:#{属性名},mybatis调用此属性的getXXX()方法获取属性值
-->
<select id="selectByObject" resultType="com.lu.entity.Student">
select id, name, email, age from student where name = #{name} or age = #{age}
</select>
这里我们只写了name和age,那么测试类中就可以这么写
@Test
public void testSelectByObject() {
SqlSession session = MyBatisUtil.getSqlSession();
StudentDao dao = session.getMapper(StudentDao.class);
Student student = new Student();
student.setName("张三");
student.setAge(25);
List<Student> students = dao.selectByObject(student);
students.forEach(stu -> System.out.println(stu));
session.close();
}
控制台输出:
可以看到可以正常输出
我们必须明白我们的参数不一定使我们定义的Student实体类,也可以是其他类,只要该类的属性有set和get方法即可
5.dao接口中多个简单类型的参数,使用位置
参数位置:dao接口中方法的形参列表,从左往右,参数位置是0,1,2...
语法格式:#{arg0},#{arg1}
下面来讲一个例子:
我们首先在接口中定义一个方法
/*
* 使用位置
* */
List<Student> selectByPosition(String name, Integer age);
mapper文件中
<select id="selectByPositon" resultType="com.lu.entity.Student">
select id, name, email, age from student where name = #{arg0} or age = #{arg1}
</select>
在测试类中
@Test
public void testSelectByPositon() {
SqlSession session = MyBatisUtil.getSqlSession();
StudentDao dao = session.getMapper(StudentDao.class);
List<Student> students = dao.selectByPosition("张三", 18);
students.forEach(student -> System.out.println(student));
session.close();
}
控制台输出:
这种方法看起来好像更简单,但是我们不推荐使用这种方法,因为代码可读性低,我们要想知道传的是什么值,就必须去dao接口中查看方法的参数,不能见名知意
6.dao接口参数是一个Map
map作为dao接口的参数,使用key获取参数值,mapper文件中, 语法格式 #{key}
我们来写一个例子:
我们首先在dao接口中定义一个方法
//使用Map作为参数
List<Student> selectStudentByMap(Map<String, Object> map);
mapper文件中:
<select id="selectStudentByMap" resultType="com.lu.entity.Student">
select id, name, email, age from student where name = #{myname} or age = #{myage}
</select>
测试类中:
@Test
public void testSelectByMap() {
SqlSession session = MyBatisUtil.getSqlSession();
StudentDao dao = session.getMapper(StudentDao.class);
//使用Map传递参数
Map<String, Object> data = new HashMap<>();
data.put("myname", "张三");
data.put("myage", 20);
List<Student> students = dao.selectStudentByMap(data);
students.forEach(student -> System.out.println(student));
session.close();
}
控制台输出: