当前位置:网站首页>Mapper agent development
Mapper agent development
2022-07-02 23:30:00 【pengege666】
List of articles
Mapper Agent development overview
There is a hard coding problem in writing code before , As shown in the figure below .
This call selectList() The parameters passed by the method are in the mapping configuration file namespace.id value . This is not convenient for later maintenance .
If you use Mapper Agency mode ( Here's the picture ) There is no hard coding problem .
Use Mapper The purpose of agency :
- Solve the hard coding in the native way
- Simplify late execution SQL
Use Mapper agent

step1: Definition and SQL Mapping files with the same name Mapper Interface , And will Mapper Interface and SQL The mapping files are placed in the same directory .


step2: Set up SQL Mapping files namespace The attribute is Mapper The interface is fully qualified .
step3: stay Mapper Define methods in interfaces , The method name is SQL In the mapping file sql Of the statement id, And keep the parameter type consistent with the return value type 

step4: change UserMapper.xml Configuration in progress mapper route 
step5: code 
MybatiDemo2.java
public class MybatiDemo2 {
public static void main(String[] args) throws IOException {
//1. load mybatis Core profile for , obtain SqlSessionFactory
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//2. obtain SqlSession object , Use it to execute sql
SqlSession sqlSession = sqlSessionFactory.openSession();
//3. perform sql
//List<User> users = sqlSession.selectList("test.selectAll"); // The argument is a string , The string must be the of the mapping profile namespace.id
//System.out.println(users);
// 3.1 obtain UserMapper The proxy object of the interface
UserMapper userMapper=sqlSession.getMapper(UserMapper.class);
List<User> users=userMapper.selectAll();
System.out.println(users);
//4. Release resources
sqlSession.close();
}
}

Be careful :
If Mapper Interface name and SQL The mapping file name is the same , And in the same directory , You can use packet scanning to simplify SQL Loading of mapping file . That is, modify the configuration of the loading mapping configuration file of the core configuration file to
<mappers>
<!-- load sql The mapping file -->
<!-- <mapper resource="com/itheima/mapper/UserMapper.xml"/>-->
<!--Mapper Agency mode -->
<package name="com.itheima.mapper"/>
</mappers>
Return value type alias
In the mapping configuration file resultType The type of data that needs to be encapsulated ( The fully qualified name of the class ). And it's particularly troublesome to write like this every time ,Mybatis Provides Type the alias (typeAliases) It can simplify the writing of this part .
First, you need to configure the type alias in the core configuration file , Which means giving pojo All classes under the package are aliased ( An alias is a class name ), Case insensitive . The contents are as follows :
With the above configuration , We can simplify the mapping configuration file resultType Attribute value writing
<mapper namespace="com.itheima.mapper.UserMapper">
<select id="selectAll" resultType="user">
select * from tb_user;
</select>
</mapper>
Reference resources
JavaWebNotes note
边栏推荐
- 密码技术---密钥和SSL/TLS
- [proteus simulation] 51 MCU +lcd12864 push box game
- Go project operation method
- CDN 加速,需要域名先备案
- JDBC练习案例
- 万物并作,吾以观复|OceanBase 政企行业实践
- 解决:exceptiole ‘xxxxx.QRTZ_LOCKS‘ doesn‘t exist以及mysql的my.cnf文件追加lower_case_table_names后启动报错
- 【Proteus仿真】51单片机+LCD12864推箱子游戏
- Cryptography -- the mode of block cipher
- [ml] Li Hongyi III: gradient descent & Classification (Gaussian distribution)
猜你喜欢
![[error record] the flutter reports an error (could not resolve io.flutter:flutter_embedding_debug:1.0.0.)](/img/93/dc940caebe176177e4323317ebf4fa.jpg)
[error record] the flutter reports an error (could not resolve io.flutter:flutter_embedding_debug:1.0.0.)

Prometheus deployment

跨境电商如何通过打好数据底座,实现低成本稳步增长

基于FPGA的VGA协议实现
![[redis notes] compressed list (ziplist)](/img/83/ff38fabb1baebc5fa4d3d72da315dc.png)
[redis notes] compressed list (ziplist)

BBR 遭遇 CUBIC

How can cross-border e-commerce achieve low-cost and steady growth by laying a good data base

Go basic data type

JDBC教程

采用VNC Viewer方式遠程連接樹莓派
随机推荐
Eight bit responder [51 single chip microcomputer]
C#中Linq用法汇集
Call vs2015 with MATLAB to compile vs Project
潘多拉 IOT 开发板学习(HAL 库)—— 实验3 按键输入实验(学习笔记)
Interface switching based on pyqt5 toolbar button -2
SharedPreferences save list < bean > to local and solve com google. gson. internal. Linkedtreemap cannot be cast to exception
【Redis笔记】压缩列表(ziplist)
MarkDown基本语法
CDN acceleration requires the domain name to be filed first
Implementation of VGA protocol based on FPGA
All things work together, and I will review oceanbase's practice in government and enterprise industry
SQL进阶语法
可知论与熟能生巧
BBR 遭遇 CUBIC
(毒刺)利用Pystinger Socks4上线不出网主机
聊聊内存模型与内存序
Solution: exceptiole 'xxxxx QRTZ_ Locks' doesn't exist and MySQL's my CNF file append lower_ case_ table_ Error message after names startup
Boost库链接错误解决方案
The concepts of terminal voltage, phase voltage and line voltage in FOC vector control and BLDC control are still unclear
潘多拉 IOT 开发板学习(HAL 库)—— 实验4 串口通讯实验(学习笔记)