DAO代理实现数据库操作
1、去掉Dao接口实现类
2、getMapper获取代理对象
只需调用 SqlSession 的 getMapper()方法,即可获取指定接口的实现类对 象。该方法的参数为指定 Dao 接口类的 class 值。
1
2
|
SqlSession session = factory.openSession(); StudentDao dao = session.getMapper(StudentDao. class ); |
使用工具类
1
2
|
StudentDao studentDao = MyBatisUtil.getSqlSession().getMapper(StudentDao. class ); |
getMapper()创建的对象,是代替我们自己创建的 StudentDaoImpl 类
3、使用 Dao 代理对象方法执行 sql 语句
select方法进行查询
1
2
3
4
5
|
@Test public void testSelect() throws IOException { final List<Student> studentList = studentDao.selectStudents(); studentList.forEach( stu -> System.out.println(stu)); } |
insert方法进行插入
1
2
3
4
5
6
7
8
9
10
|
@Test public void testInsert() throws IOException { Student student = new Student(); student.setId( 1006 ); student.setName( "林浩" ); student.setEmail( "linhao@163.com" ); student.setAge( 26 ); int nums = studentDao.insertStudent(student); System.out.println( "使用 Dao 添加数据:" +nums); } |
4、深入理解参数
从 java 代码中把参数传递到 mapper.xml 文件。
parameterType
parameterType: 接口中方法参数的类型, 类型的完全限定名或别名。这个属 性是可选的,因为 MyBatis 可以推断出具体传入语句的参数,默认值为未设置 (unset)。接口中方法的参数从 java 代码传入到 mapper 文件的 sql 语句。
- int 或 java.lang.Integer
- hashmap 或 java.util.HashMap
- list 或 java.util.ArrayList
- student 或 com.bjpowernode.domain.Student
<select>,<insert>,<update>,<delete>都可以使用 parameterType 指定类型。
eg:
1
2
3
4
5
6
7
|
< delete id = "deleteStudent" parameterType = "int" > delete from student where id=#{studentId} </ delete > 等同于 < delete id = "deleteStudent" parameterType = "java.lang.Integer" > delete from student where id=#{studentId} </ delete > |
一个简单参数
Dao 接口中方法的参数只有一个简单类型(java 基本类型和 String),占位符 #{ 任意字符 },和方法的参数名无关。
接口方法
1
|
Student selectById( int id); |
mapper文件
1
2
3
|
< select id = "selectById" resultType = "com.bjpowernode.domain.Student" > select id,name,email,age from student where id=#{studentId} </ select > |
#{studentId} , studentId 是自定义的变量名称,和方法参数名无关。
测试方法
1
2
3
4
5
6
|
@Test public void testSelectById(){ //一个参数 Student student = studentDao.selectById( 1005 ); System.out.println( "查询 id 是 1005 的学生:" +student); } |
使用@Param
当 Dao 接口方法多个参数,需要通过名称使用参数。 在方法形参前面加 入@Param(“自定义参数名”),mapper 文件使用#{自定义参数名}。
例如定义 List<Student> selectStudent( @Param(“personName”)
String name ) { … }
mapper 文件 select * from student where name =
#{ personName}
接口方法
1
2
|
List<Student> selectMultiParam( @Param ( "personName" ) String name, @Param ( "personAge" ) int age); |
Mapper文件
1
2
3
4
|
< select id = "selectMultiParam" resultType = "com.bjpowernode.domain.Student" > select id,name,email,age from student where name=#{personName} or age =#{personAge} </ select > |
测试方法
1
2
3
4
5
|
@Test public void testSelectMultiParam(){ List<Student> stuList = studentDao.selectMultiParam( "李力" , 20 ); stuList.forEach( stu -> System.out.println(stu)); } |
使用对象
使用 java 对象传递参数, java 的属性值就是 sql 需要的参数值。 每一个属性就是一个参数。
语法格式: #{ property,javaType=java 中数据类型名
jdbcType=数据类型名称 } javaType, jdbcType 的类型 MyBatis 可以检测出来,一般不需要设置。常用格式 #{ property }
创建保存参数值的对象 QueryParam
1
2
3
4
5
6
|
package com.bjpowernode.vo; public class QueryParam { private String queryName; private int queryAge; //set ,get 方法 } |
接口方法
1
|
List<Student> selectMultiObject(QueryParam queryParam); |
Mapper文件
1
2
3
4
5
6
7
8
9
10
|
< select id = "selectMultiObject" resultType = "com.bjpowernode.domain.Student" > select id,name,email,age from student where name=#{queryName} or age =#{queryAge} </ select > 或 < select id = "selectMultiObject" resultType = "com.bjpowernode.domain.Student" > select id,name,email,age from student where name=#{queryName,javaType=string,jdbcType=VARCHAR} or age =#{queryAge,javaType=int,jdbcType=INTEGER} </ select > |
测试方法
1
2
3
4
5
6
7
8
|
@Test public void selectMultiObject(){ QueryParam qp = new QueryParam(); qp.setQueryName( "李力" ); qp.setQueryAge( 20 ); List<Student> stuList = studentDao.selectMultiObject(qp); stuList.forEach( stu -> System.out.println(stu)); } |
到此这篇关于MyBatis详细讲解DAO代理的使用的文章就介绍到这了,更多相关MyBatis DAO代理内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://blog.csdn.net/m0_54925305/article/details/123487142