MyBatis进阶--接口代理方式实现Dao 和动态SQL
MyBatis接口代理方式实现Dao层
接口代理方式-实现规则
传统方式实现Dao层,我们既要写接口。还要写实现类。而MyBatis框架可以帮助我们省略写Dao层接口实现类的步骤。程序员只需要编写接口,由MyBatis框架根据接口的定义来创
1该接口的动态代理对象。
实现规则:
1.映射配置文件中的名称空间必须和Dao层接口的全类名相同
2.映射配置文件中的增删改查标签的id属性必须和Dao层接口的方法名相同
3.映射配置文件中的增删改查标签的paramrterType属性必须和Dao层接口方法的参数相同
4.映射配置文件中的增删改查标签的resultType属性必须和Dao层接口方法的返回值相同
接口代理方式-代码实现
1.删除mapper层接口的实现类
2.修改映射配置文件
3.修改service层接口的实现类,采用接口代理方式实现功能

删除mapper层接口的实现类

修改映射配置文件
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); // StudentMapper mapper = new StudentMapperImpl(); //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); // 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;
} @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); // StudentMapper mapper = new StudentMapperImpl(); //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); // StudentMapper mapper = new StudentMapperImpl(); //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); // StudentMapper mapper = new StudentMapperImpl(); //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;
}
}
修改service层接口的实现类,采用接口代理方式实现功能
接口代理方式-源码分析
分析动态代理对象如何生成的?
通过动态代理开发的模式,我们只需要编写一个接口,不写实现类,我们通过getMapper()方法最终获取到
org.apache.ibatis.binding.MapperProxy代理对象,然后执行功能,而这个代理对象正是MyBatis使用了JDK的动态代理技术,帮助我们生成了代理实现类对象。从而可以进行相关持久化操作。
分析方法是如何执行的?
动态代理的实现类对象再执行方法的时候最终调用了maperMethod.execute()方法,这个方法中通过switch语句根据操作类型来判断是新增,修改,删除,查询操作,最后一步回到了MMyBatis最原生的SqlSession方式来执行增删改查。
接口代理方式小结
接口代理方式可以让我们只编写接口即可,而实现类对象由MyBatis生成。
实现规则:
1.映射配置文件中的名称空间必须和Dao层接口的全类名相同
2.映射配置文件中的增删改查标签的id属性必须和Dao层接口的方法名相同
3.映射配置文件中的增删改查标签的paramrterType属性必须和Dao层接口方法的参数相同
4.映射配置文件中的增删改查标签的resultType属性必须和Dao层接口方法的返回值相同
获取动态代理对象
SqlSession功能类中的getMapper()方法
删除mapper中的实现类 ,保留接口
在StudentMapper.xml中修改 <mapper namespace="com.itheima.mapper.StudentMapper">
更改service包下 StudentServiceImpl文件
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); // StudentMapper mapper = new StudentMapperImpl(); //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); // 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;
} @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); // StudentMapper mapper = new StudentMapperImpl(); //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); // StudentMapper mapper = new StudentMapperImpl(); //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); // StudentMapper mapper = new StudentMapperImpl(); //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;
}
}
创建dynamic下测试类Test01
package com.itheima.dynamic; import com.itheima.bean.Student;
import com.itheima.mapper.StudentMapper;
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 org.junit.Test; import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List; public class Test01 {
@Test
public void selectCondition() throws Exception {
InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml"); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is); SqlSession sqlSession = sqlSessionFactory.openSession(true); StudentMapper mapper = sqlSession.getMapper(StudentMapper.class); Student stu=new Student();
stu.setId(2);
stu.setName("李四");
//stu.setAge(23);
List<Student> list = mapper.selectCondition(stu); for (Student student:list){
System.out.println(student);
}
sqlSession.close();
is.close();
}
@Test
public void selectByIds() throws Exception {
InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml"); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is); SqlSession sqlSession = sqlSessionFactory.openSession(true); StudentMapper mapper = sqlSession.getMapper(StudentMapper.class); List<Integer> ids=new ArrayList<>();
ids.add(1);
ids.add(2);
List<Student> list = mapper.selectByIds(ids); for (Student student:list){
System.out.println(student);
}
sqlSession.close();
is.close();
}
}
MyBatis映射配置文件-----动态sql
动态SQL介绍
MyBatis映射配置文件中,前面我们的SQL都是比较简单的,有些时候业务逻辑复杂时,我们的SQL就时动态变化的,此时在前面学习的SQL就不能满足要求了。
多条件查询
id:3 name:王五 age:25
select * from Student where id=#{id} and name=#{name} and age=#{age}
id:3 name:王五
select * from Student where id=#{id} and name=#{name}
动态sql标签
<where>:条件标签。如果有动态条件,则使用该标签代替where关键字。
<if>:条件判断标签
<if test="条件判断">
查询条件拼接
</if>
<foreach>:循环遍历标签。适用于多个参数或者的关系。
<foreach collection="" open="" close="" item="" separator="">
获取参数
</foreach>
属性:
collection:参数容器类型,(list-集合,array-数组)。
open:开始的sql语句
close:结束额sql语句
item:参数变量名
separator:分隔符
Sql片段抽取
我们可以将一些重复的SQL语句进行抽取,以达到复用的效果
<sql>: 抽取SQL语句标签
<sql id ="片段唯一标识" >抽取的SQL语句</sql>
<include>:引入SQL片段标签
<include refid="片段唯一标识" />
StudentMapper.xml
<?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.itheima.mapper.StudentMapper">
<sql id="select">Select * FROM Student</sql>
<!--
select:查询功能的标签
id属性:唯一标识
resultType属性:指定结果映射对象的类型
parameterType:指定参数映射对象类型
-->
<select id="selectAll" resultType="student">
<include refid="select"/>
</select> <select id="selectById" resultType="student" parameterType="int">
<include refid="select"/> where id = #{id}
</select> <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>
<select id="selectCondition" resultType="student" parameterType="student">
<include refid="select"/>
<where>
<if test="id!=null">
id=#{id}
</if>
<if test="name!=null">
and name=#{name}
</if>
<if test="age!=null">
and age=#{age}
</if>
</where>
</select> <select id="selectByIds" resultType="student" parameterType="list">
<include refid="select"/>
<where>
<foreach collection="list" open="id in (" close=")" item="id" separator=",">
#{id}
</foreach>
</where>
</select> </mapper>
动态SQL小结
动态SQL指的就是SQL语句可以根据条件或者参数的不同进行动态的变化
<where>:条件标签
<if>:里面有个test标签可以帮我们进行判断
<foreach>:循环遍历的标签 collection="list" open="id in (" close=")" item="id" separator=","
<sql>:抽取SQL片段的标签
<include>:引入SQL片段的标签
MyBatis进阶--接口代理方式实现Dao 和动态SQL的更多相关文章
- Mybatis进阶学习笔记——动态代理方式开发Dao接口、Dao层(推荐第二种)
1.原始方法开发Dao Dao接口 package cn.sm1234.dao; import java.util.List; import cn.sm1234.domain.Customer; pu ...
- Mybatis的dao层实现 接口代理方式实现规范+plugins-PageHelper
Mybatis的dao层实现 接口代理方式实现规范 Mapper接口实现时的相关规范: Mapper接口开发只需要程序员编写Mapper接口而不用具体实现其代码(相当于我们写的Imp实现类) Mapp ...
- 分享公司DAO层动态SQL的一些封装
主题 公司在DAO层使用的框架是Spring Data JPA,这个框架很好用,基本不需要自己写SQL或者HQL就能完成大部分事情,但是偶尔有一些复杂的查询还是需要自己手写原生的Native SQL或 ...
- mybatis入门--mapper代理方式开发
不使用代理开发 之前,我们说了如何搭建mybatis框架以及我们使用mybatis进行简单的增删改查.现在,我们一起来构建一个dao层的完整代码.并用@test来模拟service层对dao层进行一下 ...
- spring整合mybatis(非代理方式)【我】
首先创建要给 maven 的war项目 不用代理的方式: 如果不适用Mapper代理的方式,配置就非常简单: 首先是pom文件(以下配置文件包含其他多余内容,仅供参考): <project xm ...
- MyBatis基础_连接池与事务、动态SQL、注解开发
一.MyBatis连接池及事务控制 1.连接池 在实际开发中,都会使用连接池,因为它可以减少获取连接缩消耗的时间.所谓连接池,就是存储数据库连接的容器.连接池中存储一定数量的数据库连接,当线程需要使用 ...
- Mybatis学习第三天——输入输出映射以及动态SQL
注意:以下传入数据与输出数据类型部分使用别名的方式,别名在SqlMapConfig.xml核心文件中配置 1.输入映射 1.1 传递简单数据类型 1.2 传递pojo中的类类型 1.3 传递Query ...
- mybatis源码学习(四):动态SQL的解析
之前的一片文章中我们已经了解了MappedStatement中有一个SqlSource字段,而SqlSource又有一个getBoundSql方法来获得BoundSql对象.而BoundSql中的sq ...
- mybatis --- 以接口的方式进行编程
); 我们来学习下接口方式的编程. 接口类IBookDao.java package com.mybatis.dao; import com.mybatis.model.Book; public i ...
随机推荐
- python编写自己的base64加解密工具
0x00 Base64编码的用途 在网络传输中,不是所的的内容都是可打印字符,其中绝大多数数据是不可见字符,base64可以基于64个可打印字符来表示这些带有不可打印字符的传输数据. 0x01 Bas ...
- 保姆级教程!使用k3d实现K3s高可用!
你是否曾经想尝试使用K3s的高可用模式?但是苦于没有3个"备用节点",或者没有设置相同数量的虚拟机所需的时间?那么k3d这个方案也许你十分需要噢! 如果你对k3d尚不了解,它的名字 ...
- Edge 浏览器开发工具新增了 3D 视图,你尝试了吗?
在使用开发者工具的时候,无意间发现了一个3D面板,如下: 仔细想想,这应该是之前 Firefox 的特性啊,不过后来去掉了,说是太难维护,没想到 Edge 也添加了这个特性. 使用该特性,你可以完成如 ...
- EfficientNet & EfficientDet 论文解读
概述 总体而言,这两篇论文都在追求一件事,那就是它们名字中都有的 efficient.只是两篇文章的侧重点不一样,EfficientNet 主要时研究如何平衡模型的深度 (depth).宽度 (wid ...
- 干货!Apache Hudi如何智能处理小文件问题
1. 引入 Apache Hudi是一个流行的开源的数据湖框架,Hudi提供的一个非常重要的特性是自动管理文件大小,而不用用户干预.大量的小文件将会导致很差的查询分析性能,因为查询引擎执行查询时需要进 ...
- SpringBoot整合阿里云OSS对象存储实现文件上传
1. 准备工作: 一.首先登录阿里云OSS对象存储控制台创建一个Bucket作为你的存储空间. 二.创建Access Keyan按要求创建进行,这里的方法步骤我就不展现出来了,你们可以自行查询阿里云文 ...
- 几十行代码实现ASP.NET Core自动依赖注入
在开发.NET Core web服务的时候,我们习惯使用自带的依赖注入容器来进行注入. 于是就会经常进行一个很频繁的的重复动作:定义一个接口->写实现类->注入 有时候会忘了写Add这一步 ...
- 03.ElementUI源码学习:代码风格检查和格式化配置(ESlint & Prettier)
书接上文.在团队协作中,为避免低级Bug.以及团队协作时不同代码风格对彼此造成的困扰与影响,会预先制定编码规范.使用 Lint工具和代码风格检测工具,则可以辅助编码规范执行,格式化代码,使样式与规则保 ...
- 做个开源博客学习Vite2 + Vue3 (三)博客设计和代码设计
项目搭建好了之后是不是可以编码了呢? 等等不要着急,我们是不是应该先设计一下?比如博客的功能等? 博客设计 先做个简单的个人博客,因为是个人版,所以可以省略注册.登录这些功能,表结构也可以简单一点. ...
- Compound Words UVA - 10391
You are to find all the two-word compound words in a dictionary. A two-word compound word is a wor ...