当前位置:网站首页>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 .
边栏推荐
- [cursor nesting] nesting of MySQL stored procedure cursors
- Redis implements the correct posture of the delay queue
- 启牛学堂给的中信建投证券账户是不是真的?开户安全吗
- Spark streaming receiver startup and data receiving
- Description of new features and changes in ABP Framework version 5.3.0
- Docker 之MySQL 重启,提示Error response from daemon: driver failed programming external connectivity on **
- JMeter use case
- Post to asp Net core length limitation and solution when transferring data
- Basic application of scala for
- WPF效果第一百九十篇之再耍ListBox
猜你喜欢
随机推荐
跨平台Brave浏览器
LETV group payment system architecture sharing for processing 100000 high concurrent orders per second
spark-cache的源码分析
[mysql] data synchronization prompt: specified key was too long; max key length is 767 bytes
[cursor nesting] nesting of MySQL stored procedure cursors
.NetCore实现图片缩放与裁剪 - 基于ImageSharp
同花顺软件是什么?手机开户安全么?
linux系统维护篇:mysql8.0.13源码下载及安装之“傻瓜式”操作步骤(linux-centos6.8)亲测可用系列
Xshell 7(SSH远程终端工具) v7.0.0109 官方中文正式版(附文件+安装教程)
NLog自定义Target之MQTT
Blazor University (31) form - Validation
数据库mysql 主从方案
You call this crap high availability?
MySQL master-slave connection prompt of docker: communications link failure
0 basic how to get started software testing, can you succeed in changing careers?
视频会议时听不到声音该如何处理?
Vs2017 solution to not displaying qstring value in debugging status
WPF 实现星空效果
同花顺容易开户么?网上开户安全么?
Windows8.1 64 installed by mysql5.7.27









