当前位置:网站首页>Mybaits: interface proxy implementation Dao
Mybaits: interface proxy implementation Dao
2022-06-22 17:16:00 【Liu Chu, Ge Nian】
List of articles
1. Interface proxy implementation Dao
1.1 Agent development mode introduction
use Mybatis The agent development method of DAO Layer development , This way is the mainstream for us to enter the enterprise .
Mapper Interface development methods only need programmers to write Mapper Interface ( amount to Dao Interface ), from Mybatis The framework creates the dynamic proxy object of the interface according to the definition of the interface , The method body of the proxy object is the same as the above Dao Interface implementation class methods .
Mapper Interface development needs to follow the following specifications :
1) Mapper.xml In the document namespace And mapper The full limit name of the interface is the same
2) Mapper Interface method names and Mapper.xml Each of the statement Of id identical
3) Mapper Input parameter types and mapper.xml Each of the sql Of parameterType Same type of
4) Mapper Output parameter types and mapper.xml Each of the sql Of resultType Same type of
summary :
The way of interface development : Programmers just need to define interfaces , You can operate the database , How to create specific objects ?
1. Programmers are responsible for defining interfaces
2. Operating the database ,mybatis The framework is based on the interface , Generate proxy objects through dynamic proxy , Responsible for database management crud operation
1.2. To write StudentMapper Interface

For example, the following code example :
Mapper.class
/* Persistence layer interface */
public interface StudentMapper {
// Query all
public abstract List<Student> selectAll();
}
Mapper.xml
<!-- mapper: The core root tag namespace attribute : The name space -->
<mapper namespace="com.yyl.mapper.StudentMapper">
<sql id="select" >SELECT * FROM student</sql>
<!-- select: The label of the query function id attribute : Unique identification resultType attribute : Specify the result mapping object type parameterType attribute : Specifies the parameter mapping object type -->
<select id="selectAll" resultType="student">
<include refid="select"/>
</select>
</mapper>
1.3 Test agent mode
public List<Student> selectAll() {
List<Student> list = null;
SqlSession sqlSession = null;
InputStream is = null;
try{
//1. Load core profile
is = Resources.getResourceAsStream("MyBatisConfig.xml");
//2. obtain SqlSession Factory object
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
//3. Get... Through factory objects SqlSession object
sqlSession = sqlSessionFactory.openSession(true);
//4. obtain StudentMapper Implementation class object of interface
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class); // StudentMapper mapper = new StudentMapperImpl();
//5. Call methods by implementing class objects , Receive results
list = mapper.selectAll();
} catch (Exception e) {
} finally {
//6. Release resources
if(sqlSession != null) {
sqlSession.close();
}
if(is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//7. Return results
return list;
}
The operation results are as follows :
1.4 Source code analysis
Analyze how dynamic proxy objects are generated ?
Through dynamic agent development mode , We only write one interface , Don't write implementation classes , We go through getMapper() Method finally gets org.apache.ibatis.binding.MapperProxy Proxy object , And then perform the function , And the proxy object is MyBatis Used JDK Dynamic proxy technology , Help us generate proxy implementation class objects . So that you can perform the relevant persistence operations .
How analytical methods are performed ?
When the dynamic proxy implementation class object executes the method, it finally calls mapperMethod.execute() Method , In this method, through switch The statement is new according to the operation type 、 modify 、 Delete 、 Query operation , The last step is back to MyBatis The most original SqlSession Method to perform the addition, deletion, modification and query .
1.5 A summary of knowledge
Interface proxy allows us to write only interfaces , The implementation class object is created by MyBatis Generate .
Implementation rules :
- The namespace in the mapping configuration file must be the same as Dao The full class name of the layer interface is the same .
- Add, delete, and query tags in the mapping configuration file id Property must be the same as Dao Layer interface has the same method name .
- Add, delete, and query tags in the mapping configuration file parameterType Property must be the same as Dao The parameters of the layer interface method are the same .
- Add, delete, and query tags in the mapping configuration file resultType Property must be the same as Dao The return value of the layer interface method is the same .
- Get the dynamic proxy object SqlSession In the functional class getMapper() Method .
边栏推荐
- Xftp 7(FTP/SFTP客户端) V7.0.0107 官方中文免费正式版(附文件+安装教程)
- ABP Framework 5.3.0 版本新增功能和变更说明
- In the era of video explosion, who is supporting the high-speed operation of video ecological network?
- mysql5.7.27安装之windows8.1 64
- [psychology] emotional psychology - collision between contemporary thoughts and traditional thoughts (this article will be continuously updated from time to time)
- 图计算Hama-BSP模型的运行流程
- Hello Playwright:(7)模拟键盘和鼠标
- Is the CSC securities account given by qiniu school true? Is it safe to open an account
- 同花顺容易开户么?网上开户安全么?
- Post to asp Net core length limitation and solution when transferring data
猜你喜欢

Xftp 7(FTP/SFTP客户端) V7.0.0107 官方中文免费正式版(附文件+安装教程)

Mybaits:接口代理方式实现Dao

Huawei cloud recruits partners in the field of industrial intelligence to provide strong support + commercial realization

视频会议时听不到声音该如何处理?

以小见大:一个领域建模的简单示例,理解“领域驱动”。

Description of new features and changes in ABP Framework version 5.3.0

What should I do if I can't hear a sound during a video conference?

JMeter use case

STM32 ADC acquisition via DMA (HAL Library)

Apache ShardingSphere 一文读懂
随机推荐
快速掌握 ASP.NET 身份认证框架 Identity - 用户注册
JMeter use case
What are the characteristics of the interactive whiteboard? Function introduction of electronic whiteboard
团队管理|如何提高技术 Leader 的思考技巧?
购买指南丨如何购买一台高质量会议平板,这几个方面一定要对比
In the era of video explosion, who is supporting the high-speed operation of video ecological network?
Blazor University (31)表单 —— 验证
linux系统维护篇:mysql8.0.13源码下载及安装之“傻瓜式”操作步骤(linux-centos6.8)亲测可用系列
来厦门了!线上交流限额免费报名中
有同学问PHP要学什么框架?
vs2017 在调试状态不显示QString值的解决方法
spark关于数据倾斜问题
Short video source code development, high-quality short video source code need to do what?
视频会议时听不到声音该如何处理?
Figure operation flow of HAMA BSP Model
使用 POST 向 ASP.NET Core 传递数据时的长度限制与解决方案
Cross platform brake browser
be based on. NETCORE development blog project starblog - (12) razor page dynamic compilation
scala-for的基本应用
Source code analysis of spark cache