目前我本人正在学习MyBatis框架,在原先了解并且懵懵懂懂使用的基础上,开始系统正式的学习。阐述了MVC架构模式和三层架构,明晰了在Web项目中的普遍编码层次,认识了框架,回顾了JDBC连接数据库,建立了使用MyBatis和MySQL的Maven项目,解释了STDOUT_LOGGING日志和手动提交事务,记录了MyBatis中#占位符的使用方法,回顾了MyBatis执行SQL语句的过程和使用到的一些重要类和接口,记录了将固定化的代码整合到一个工具类MyBatisUtil中,以减少代码量。记录了dao层接口的实现以及为什么要实现它。记录了MyBatis动态代理和使用动态代理的要求以及使用了动态代理生成的实现类,记录了parameterType属性和它的使用。本篇博客记录MyBatis框架下执行SQL语句传递简单类型参数的方法。
我上一篇博文记录的是parameterType属性的使用,感兴趣的读者可以前往查阅,链接如下:
执行SQL语句的时候,有时候条件语句中的条件不确定或者要插入的数据不确定,这样就需要我们传递参数。传递的参数类型又很多种,本篇博文记录的值传递简单类型参数的方法。
简单类型参数是指:java自带的基本数据类型,包括包装类和非包装类,另外再加上String类型。例如:Integer,int,char,Character等。
我直接使用代码举一个例子:
dao层接口中的方法代码如下所示:
public Student selectById(Integer id);
我只写了一个简单的方法,里面使用的形参变量名是 id。
在mapper.xml文件中对应的标签代码如下所示:
- <select id="selectById" resultType="com.dcy.domain.Student" parameterType="int">
- select id,name,email,age from student where id=#{studentId}
- select>
当在Main方法中执行测试代码的时候,可以正常的执行这个SQL语句。方法中的形参变量名是id,而mapper.xml文件中占位符处使用的变量名是studentId。
明显二者不一致,但是不妨碍传参和语句的执行!!
只传递一个简单类型的参数的情况就是上面举例的那种。只传一个简单类型的参数,那么dao层方法中的形参变量名和mapper.xml文件中SQL语句占位符处的变量名可以是任意值。在Main方法中进行测试时,传递的实参变量名也可以是任意值!!
但是,按照编写代码的规范,强烈建议:dao层接口方法的形参变量名和mapper.xml文件中SQL语句占位符处变量名保持一致!!
当传递多个简单类型参数的时候,由于有多个参数,就牵扯到位置对应的问题,此时需要使用MyBatis提供的注解:@Param注解,它有一个value属性。
@Param注解用在dao层接口方法的形参变量名的前面,每个形参都需要有。这个注解的作用就是为此形参指定在mapper.xml文件中引用这个形参值的变量名,指定的变量名就是这个注解的value属性值!!
不多说了,直接看代码,先是dao层接口代码如下所示:
- package com.dcy.dao;
-
- import com.dcy.domain.Student;
- import org.apache.ibatis.annotations.Param;
-
- import java.util.List;
-
- public interface StudentDao {
- public Student selectById(Integer id);
- public List
selectStudents(); - public int insertStudent(Student student);
- //测试多个简单变量的情况
- public List
selectStudentByAgeOrEmail(@Param(value = "age")int age,@Param(value = "email")String email); -
- }
然后是mapper.xml文件中对应这个方法的select标签,代码如下:
- mapper
- PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
- "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
-
- <mapper namespace="com.dcy.dao.StudentDao">
- <select id="selectById" resultType="com.dcy.domain.Student" parameterType="int">
- select id,name,email,age from student where id=#{studentId}
- select>
- <insert id="insertStudent" parameterType="com.dcy.domain.Student">
- insert into student values (#{id},#{name},#{email},#{age})
- insert>
- <select id="selectStudents" resultType="com.dcy.domain.Student">
- select * from student
- select>
- <select id="selectStudentByAgeOrEmail" resultType="com.dcy.domain.Student">
- select * from student where age=#{age} or email=#{email}
- select>
-
- mapper>
最后在Main方法中进行必要的测试:
- package com.dcy;
-
- import com.dcy.dao.StudentDao;
- import com.dcy.domain.Student;
- import com.dcy.utils.MyBatisUtil;
- import org.apache.ibatis.session.SqlSession;
-
- import java.util.List;
-
- public class starter02 {
- public static void main(String[] args) {
- SqlSession sqlSession= MyBatisUtil.getSqlSession();
- StudentDao studentDao = sqlSession.getMapper(StudentDao.class);
- List
students = studentDao.selectStudentByAgeOrEmail(25, "haha@qq.com"); - System.out.println(students);
- }
- }