当前位置:网站首页>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
边栏推荐
- Use redis to realize self increment serial number
- Go basic anonymous variable
- Hisilicon VI access video process
- Redis expiration policy +conf record
- Cryptographic technology -- key and ssl/tls
- “一个优秀程序员可抵五个普通程序员!”
- Quantitative analysis of PSNR, SSIM and RMSE
- Win11自动关机设置在哪?Win11设置自动关机的两种方法
- Prometheus deployment
- Call vs2015 with MATLAB to compile vs Project
猜你喜欢
golang入门:for...range修改切片中元素的值的另类方法
程序员版本的八荣八耻~
Integration of revolution and batch normalization
【STL源码剖析】仿函数(待补充)
CDN acceleration requires the domain name to be filed first
2022年最新最全软件测试面试题大全
Solution: exceptiole 'xxxxx QRTZ_ Locks' doesn't exist and MySQL's my CNF file append lower_ case_ table_ Error message after names startup
Data set - fault diagnosis: various data and data description of bearings of Western Reserve University
实现BottomNavigationView和Navigation联动
“一个优秀程序员可抵五个普通程序员!”
随机推荐
Win11系统explorer频繁卡死无响应的三种解决方法
理想汽车×OceanBase:当造车新势力遇上数据库新势力
Quantitative analysis of PSNR, SSIM and RMSE
(毒刺)利用Pystinger Socks4上线不出网主机
MySQL基础
购买完域名之后能干什么事儿?
Use the scroll bar of souI when using the real window in souI
“一个优秀程序员可抵五个普通程序员!”
Jinglianwen technology's low price strategy helps AI enterprises reduce model training costs
可知论与熟能生巧
Ping domain name error unknown host, NSLOOKUP / system d-resolve can be resolved normally, how to Ping the public network address?
Writing of head and bottom components of non routing components
万物并作,吾以观复|OceanBase 政企行业实践
解决:exceptiole ‘xxxxx.QRTZ_LOCKS‘ doesn‘t exist以及mysql的my.cnf文件追加lower_case_table_names后启动报错
内网渗透 | 手把手教你如何进行内网渗透
BBR 遭遇 CUBIC
Tiktok actual combat ~ number of likes pop-up box
golang中new与make的区别
第三方支付功能测试点【杭州多测师_王sir】【杭州多测师】
Prometheus deployment