01-接口代理方式实现Dao层-实现规则的介绍
就是不用接口实现类了
采用 Mybatis 的代理开发方式实现 DAO 层的开发,这种方式是我们后面进入企业的主流。
Mapper 接口开发方法只需要程序员编写Mapper 接口(相当于Dao 接口),由Mybatis 框架根据接口定义创建接口的动态代理对象,代理对象的方法体同上边Dao接口实现类方法。
Mapper 接口开发需要遵循以下规范:
1) Mapper.xml文件中的namespace与mapper接口的全限定名相同
2) Mapper接口方法名和Mapper.xml中定义的每个statement的id相同
3) Mapper接口方法的输入参数类型和mapper.xml中定义的每个sql的parameterType的类型相同
4) Mapper接口方法的输出参数类型和mapper.xml中定义的每个sql的resultType的类型相同
总结:
接口开发的方式: 程序员只需定义接口,就可以对数据库进行操作,那么具体的对象怎么创建?
1.程序员负责定义接口
2.在操作数据库,mybatis框架根据接口,通过动态代理的方式生成代理对象,负责数据库的crud操作
02-接口代理方式实现Dao层-代码的实现
public Student selectById(Integer id) {
Student stu = null;
SqlSession sqlSession = null;
InputStream is = null;
try{
//1.加载核心配置文件
is = Resources.getResourceAsStream("MyBatisConfig.xml");
//2.获取SqlSession工厂对象
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
//3.通过工厂对象获取SqlSession对象
sqlSession = sqlSessionFactory.openSession(true);
//4.获取StudentMapper接口的实现类对象
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class); // StudentMapper mapper = new StudentMapperImpl();
//5.通过实现类对象调用方法,接收结果
stu = mapper.selectById(id);
} catch (Exception e) {
} finally {
//6.释放资源
if(sqlSession != null) {
sqlSession.close();
}
if(is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//7.返回结果
return stu;
}
package com.itheima.service.impl;
import com.itheima.bean.Student;
import com.itheima.mapper.StudentMapper;
import com.itheima.service.StudentService;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
/*
业务层实现类
*/
public class StudentServiceImpl implements StudentService {
@Override
public List<Student> selectAll() {
List<Student> list =null;
SqlSession sqlSession =null;
InputStream is =null;
try {
// 1.加载核心配置文件
is = Resources.getResourceAsStream("MyBatisConfig.xml");
// 2.获取SqlSession工厂对象
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
// 3. 通过工厂对象获取SqlSession对象
sqlSession = sqlSessionFactory.openSession(true);
// 4. 获取StudentMapper接口的实现类对象
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
// 5. 通过实现类对象调用方法,接受结果
list = mapper.selectAll();
}catch (Exception e) {
}finally {
// 6.释放资源
if(sqlSession!=null) {
sqlSession.close();
}
if(is!=null) {
try{
is.close();
}catch (IOException e) {
e.printStackTrace();
}
}
}
//7.返回结果
return list;
}
@Override
public Student selectById(Integer id) {
Student stu =null;
SqlSession sqlSession =null;
InputStream is =null;
try {
// 1.加载核心配置文件
is = Resources.getResourceAsStream("MyBatisConfig.xml");
// 2.获取SqlSession工厂对象
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
// 3. 通过工厂对象获取SqlSession对象
sqlSession = sqlSessionFactory.openSession(true);
// 4. 获取StudentMapper接口的实现类对象
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
// 5. 通过实现类对象调用方法,接受结果
stu = mapper.selectById(id);
}catch (Exception e) {
}finally {
// 6.释放资源
if(sqlSession!=null) {
sqlSession.close();
}
if(is!=null) {
try{
is.close();
}catch (IOException e) {
e.printStackTrace();
}
}
}
//7.返回结果
return stu;
}
@Override
public Integer insert(Student stu){
Integer result =null;
SqlSession sqlSession =null;
InputStream is =null;
try {
// 1.加载核心配置文件
is = Resources.getResourceAsStream("MyBatisConfig.xml");
// 2.获取SqlSession工厂对象
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
// 3. 通过工厂对象获取SqlSession对象
sqlSession = sqlSessionFactory.openSession(true);
// 4. 获取StudentMapper接口的实现类对象
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
// 5. 通过实现类对象调用方法,接受结果
result = mapper.insert(stu);
}catch (Exception e) {
}finally {
// 6.释放资源
if(sqlSession!=null) {
sqlSession.close();
}
if(is!=null) {
try{
is.close();
}catch (IOException e) {
e.printStackTrace();
}
}
}
//7.返回结果
return result;
}
@Override
public Integer update(Student stu) {
Integer result =null;
SqlSession sqlSession =null;
InputStream is =null;
try {
// 1.加载核心配置文件
is = Resources.getResourceAsStream("MyBatisConfig.xml");
// 2.获取SqlSession工厂对象
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
// 3. 通过工厂对象获取SqlSession对象
sqlSession = sqlSessionFactory.openSession(true);
// 4. 获取StudentMapper接口的实现类对象
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
// 5. 通过实现类对象调用方法,接受结果
result = mapper.update(stu);
}catch (Exception e) {
}finally {
// 6.释放资源
if(sqlSession!=null) {
sqlSession.close();
}
if(is!=null) {
try{
is.close();
}catch (IOException e) {
e.printStackTrace();
}
}
}
//7.返回结果
return result;
}
@Override
public Integer delete(Integer id) {
Integer result =null;
SqlSession sqlSession =null;
InputStream is =null;
try {
// 1.加载核心配置文件
is = Resources.getResourceAsStream("MyBatisConfig.xml");
// 2.获取SqlSession工厂对象
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
// 3. 通过工厂对象获取SqlSession对象
sqlSession = sqlSessionFactory.openSession(true);
// 4. 获取StudentMapper接口的实现类对象
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
// 5. 通过实现类对象调用方法,接受结果
result = mapper.delete(id);
}catch (Exception e) {
}finally {
// 6.释放资源
if(sqlSession!=null) {
sqlSession.close();
}
if(is!=null) {
try{
is.close();
}catch (IOException e) {
e.printStackTrace();
}
}
}
//7.返回结果
return result;
}
}
package com.itheima.controller;
import com.itheima.bean.Student;
import com.itheima.service.StudentService;
import com.itheima.service.impl.StudentServiceImpl;
import org.junit.Test;
import java.util.List;
/*
控制层测试类
*/
public class StudentController {
//创建业务层对象
private StudentService service = new StudentServiceImpl();
//查询全部功能测试
@Test
public void selectAll() {
List<Student> students = service.selectAll();
for (Student stu : students) {
System.out.println(stu);
}
}
//根据id查询功能测试
@Test
public void selectById() {
Student stu = service.selectById(3);
System.out.println(stu);
}
//新增功能测试
@Test
public void insert() {
Student stu = new Student(4,"赵六",26);
Integer result = service.insert(stu);
System.out.println(result);
}
//修改功能测试
@Test
public void update() {
Student stu = new Student(4,"赵六",16);
Integer result = service.update(stu);
System.out.println(result);
}
//删除功能测试
@Test
public void delete() {
Integer result = service.delete(4);
System.out.println(result);
}
}
03-接口代理方式实现Dao层-源码分析
-
分析动态代理对象如何生成的?
通过动态代理开发模式,我们只编写一个接口,不写实现类,我们通过 getMapper() 方法最终获取到 org.apache.ibatis.binding.MapperProxy 代理对象,然后执行功能,而这个代理对象正是 MyBatis 使用了 JDK 的动态代理技术,帮助我们生成了代理实现类对象。从而可以进行相关持久化操作。
-
分析方法是如何执行的?
动态代理实现类对象在执行方法的时候最终调用了 mapperMethod.execute() 方法,这个方法中通过 switch 语句根据操作类型来判断是新增、修改、删除、查询操作,最后一步回到了 MyBatis 最原生的 SqlSession 方式来执行增删改查。
04-接口代理方式实现Dao层-小结
接口代理方式可以让我们只编写接口即可,而实现类对象由 MyBatis 生成。
实现规则 :
- 映射配置文件中的名称空间必须和 Dao 层接口的全类名相同。
- 映射配置文件中的增删改查标签的 id 属性必须和 Dao 层接口的方法名相同。
- 映射配置文件中的增删改查标签的 parameterType 属性必须和 Dao 层接口方法的参数相同。
- 映射配置文件中的增删改查标签的 resultType 属性必须和 Dao 层接口方法的返回值相同。
- 获取动态代理对象 SqlSession 功能类中的 getMapper() 方法。