MyBatis接口代理:
- 采用 Mybatis 的代理开发方式实现 DAO 层的开发,这种方式是目前的主流方式。
- Mapper 接口开发方法只需要程序员编写Mapper 接口(相当于Dao 接口),由Mybatis 框架根据接口定义创建接口的动态代理对象,代理对象的方法体同上边Dao接口实现类方法。
Mapper接口开发需要遵循以下规范:
- 映射配置文件中的
namespace
与mapper接口的类名相同- 映射配置文件中的增删改查标签的
id
属性要和Mappe接口中的方法名相同- 映射配置文件中的增删改查标签的
parameterType
属性要和Mappe接口中的方法参数相同- 映射配置文件中的增删改查标签的
resultType
属性要和Mappe接口中方法的返回值相同
接口代理原理:
分析动态代理对象如何生成的?
通过动态代理开发模式,我们只编写一个接口,不写实现类,我们通过 getMapper() 方法最终获取到 org.apache.ibatis.binding.MapperProxy 代理对象,然后执行功能,而这个代理对象正是 MyBatis 使用了 JDK 的动态代理技术,帮助我们生成了代理实现类对象。从而可以进行相关持久化操作。
分析方法是如何执行的?
动态代理实现类对象在执行方法的时候最终调用了 mapperMethod.execute() 方法,这个方法中通过 switch 语句根据操作类型来判断是新增、修改、删除、查询操作,最后一步回到了 MyBatis 最原生的 SqlSession 方式来执行增删改查。
接口代理实现演示:
- 编写StudentMapper接口
public interface StudentMapper {
//查询全部
public abstract List<Student> selectAll();
//根据id查询
public abstract Student selectById(Integer id);
//新增数据
public abstract Integer insert(Student stu);
//修改数据
public abstract Integer update(Student stu);
//删除数据
public abstract Integer delete(Integer id);
}
- Service接口
public interface StudentService {
//查询全部
public abstract List<Student> selectAll();
//根据id查询
public abstract Student selectById(Integer id);
//新增数据
public abstract Integer insert(Student stu);
//修改数据
public abstract Integer update(Student stu);
//删除数据
public abstract Integer delete(Integer id);
}
- 映射文件
<?xml version="1.0" encoding="UTF-8" ?>
<!--MyBatis的DTD约束-->
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--
mapper:核心根标签
namespace属性:名称空间
-->
<mapper namespace="com.mybatis.mapper.StudentMapper">
<!--
select:查询功能的标签
id属性:唯一标识
resultType属性:指定结果映射对象类型
parameterType属性:指定参数映射对象类型
-->
<select id="selectAll" resultType="student">
SELECT * FROM student
</select>
<select id="selectById" resultType="student" parameterType="int">
SELECT * FROM student WHERE id = #{id}
</select>
<!--对于增删改返回的都是影响行数,所以resultType属性可以不写-->
<insert id="insert" parameterType="student">
INSERT INTO student VALUES (#{id},#{name},#{age})
</insert>
<update id="update" parameterType="student">
UPDATE student SET name = #{name},age = #{age} WHERE id = #{id}
</update>
<delete id="delete" parameterType="int">
DELETE FROM student WHERE id = #{id}
</delete>
</mapper>
- Service实现类
public class StudentMapperImpl implements StudentService {
@Override
public List<Student> selectAll() {
List<Student> list = null;
SqlSession sqlSession = null;
InputStream rs = null;
try {
// 加载核心配置文件
rs = Resources.getResourceAsStream("MybatisConfig.xml");
// 获取工厂对象
SqlSessionFactory build = new SqlSessionFactoryBuilder().build(rs);
// 获取SqlSession对象
sqlSession = build.openSession(true);
// 获取接口实现类对象
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
// 通过实现类对象调用方法,接收结果
list = mapper.selectAll();
} catch (IOException e) {
e.printStackTrace();
} finally {
// 释放资源SS
if (sqlSession != null) {
sqlSession.close();
}
if (rs != null) {
try {
rs.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
// 返回结果
return list;
}
@Override
public Student selectById(Integer id) {
Student stu = null;
SqlSession sqlSession = null;
InputStream rs = null;
try {
// 加载核心配置文件
rs = Resources.getResourceAsStream("MybatisConfig.xml");
// 获取工厂对象
SqlSessionFactory build = new SqlSessionFactoryBuilder().build(rs);
// 获取SqlSession对象
sqlSession = build.openSession(true);
// 获取接口实现类对象
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
// 通过实现类对象调用方法,接收结果
stu = mapper.selectById(id);
} catch (IOException e) {
e.printStackTrace();
} finally {
// 释放资源SS
if (sqlSession != null) {
sqlSession.close();
}
if (rs != null) {
try {
rs.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return stu;
}
@Override
public Integer insert(Student stu) {
Integer result = null;
SqlSession sqlSession = null;
InputStream rs = null;
try {
// 加载核心配置文件
rs = Resources.getResourceAsStream("MybatisConfig.xml");
// 获取工厂对象
SqlSessionFactory build = new SqlSessionFactoryBuilder().build(rs);
// 获取SqlSession对象
sqlSession = build.openSession(true);
// 获取接口实现类对象
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
// 通过实现类对象调用方法,接收结果
result = mapper.insert(stu);
} catch (IOException e) {
e.printStackTrace();
} finally {
// 释放资源SS
if (sqlSession != null) {
sqlSession.close();
}
if (rs != null) {
try {
rs.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return result;
}
@Override
public Integer update(Student stu) {
Integer result = null;
SqlSession sqlSession = null;
InputStream rs = null;
try {
// 加载核心配置文件
rs = Resources.getResourceAsStream("MybatisConfig.xml");
// 获取工厂对象
SqlSessionFactory build = new SqlSessionFactoryBuilder().build(rs);
// 获取SqlSession对象
sqlSession = build.openSession(true);
// 获取接口实现类对象
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
result = mapper.update(stu);
} catch (IOException e) {
e.printStackTrace();
} finally {
// 释放资源SS
if (sqlSession != null) {
sqlSession.close();
}
if (rs != null) {
try {
rs.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return result;
}
@Override
public Integer delete(Integer id) {
Integer result = null;
SqlSession sqlSession = null;
InputStream rs = null;
try {
// 加载核心配置文件
rs = Resources.getResourceAsStream("MybatisConfig.xml");
// 获取工厂对象
SqlSessionFactory build = new SqlSessionFactoryBuilder().build(rs);
// 获取SqlSession对象
sqlSession = build.openSession(true);
// 获取接口实现类对象
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
result = mapper.delete(id);
} catch (IOException e) {
e.printStackTrace();
} finally {
// 释放资源SS
if (sqlSession != null) {
sqlSession.close();
}
if (rs != null) {
try {
rs.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return result;
}
}
- 测试类
public class StuTest {
// 创建业务层对象
private StudentService service = new StudentMapperImpl();
//查询全部功能测试
@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);
}
}