当前位置:网站首页>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
边栏推荐
- 采用VNC Viewer方式远程连接树莓派
- 数据集-故障诊断:西储大学轴承的各项数据以及数据说明
- Solution to boost library link error
- Connexion à distance de la tarte aux framboises en mode visionneur VNC
- BBR encounters cubic
- CDN acceleration requires the domain name to be filed first
- Go project operation method
- 理想汽车×OceanBase:当造车新势力遇上数据库新势力
- Jinglianwen technology's low price strategy helps AI enterprises reduce model training costs
- Fusion de la conversion et de la normalisation des lots
猜你喜欢

采用VNC Viewer方式远程连接树莓派

How difficult is it to be high? AI rolls into the mathematics circle, and the accuracy rate of advanced mathematics examination is 81%!

Solution: exceptiole 'xxxxx QRTZ_ Locks' doesn't exist and MySQL's my CNF file append lower_ case_ table_ Error message after names startup

【ML】李宏毅三:梯度下降&分类(高斯分布)

【直播预约】数据库OBCP认证全面升级公开课

理想汽车×OceanBase:当造车新势力遇上数据库新势力

面试过了,起薪16k

Prometheus deployment

"A good programmer is worth five ordinary programmers!"

跨境电商如何通过打好数据底座,实现低成本稳步增长
随机推荐
Fudian bank completes the digital upgrade | oceanbase database helps to layout the distributed architecture of the middle office
Win11如何开启目视控制?Win11开启目视控制的方法
Agnosticism and practice makes perfect
Fusion de la conversion et de la normalisation des lots
Intranet penetration | teach you how to conduct intranet penetration hand in hand
Catalogue of digital image processing experiments
JDBC练习案例
Why can't the start method be called repeatedly? But the run method can?
Potplayer set minimized shortcut keys
Third party payment function test point [Hangzhou multi tester _ Wang Sir] [Hangzhou multi tester]
海思调用接口之Makefile配置
Bean加载控制
Prometheus deployment
潘多拉 IOT 开发板学习(HAL 库)—— 实验4 串口通讯实验(学习笔记)
Win11麦克风测试在哪里?Win11测试麦克风的方法
Eight bit responder [51 single chip microcomputer]
Jinglianwen technology's low price strategy helps AI enterprises reduce model training costs
(stinger) use pystinger Socks4 to go online and not go out of the network host
Brief introduction to common sense of Zhongtai
php 获取真实ip