当前位置:网站首页>Mapper代理开发
Mapper代理开发
2022-07-02 22:16:00 【pengege666】
Mapper代理开发概述
之前写代码存在硬编码的问题,如下图所示。
这里调用 selectList() 方法传递的参数是映射配置文件中的 namespace.id值。这样写也不便于后期的维护。
如果使用 Mapper 代理方式(如下图)则不存在硬编码问题。
使用Mapper 代理方式的目的:
- 解决原生方式中的硬编码
- 简化后期执行SQL
使用Mapper代理

step1:定义与SQL映射文件同名的Mapper接口,并且将Mapper接口和SQL映射文件放置在同一目录下。


step2:设置SQL映射文件的namespace属性为Mapper接口全限定名。
step3:在 Mapper 接口中定义方法,方法名就是SQL映射文件中sql语句的id,并保持参数类型和返回值类型一致

step4:更改UserMapper.xml配置中mapper路径
step5:编码
MybatiDemo2.java
public class MybatiDemo2 {
public static void main(String[] args) throws IOException {
//1. 加载mybatis的核心配置文件,获取 SqlSessionFactory
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//2. 获取SqlSession对象,用它来执行sql
SqlSession sqlSession = sqlSessionFactory.openSession();
//3. 执行sql
//List<User> users = sqlSession.selectList("test.selectAll"); //参数是一个字符串,该字符串必须是映射配置文件的namespace.id
//System.out.println(users);
// 3.1获取UserMapper接口的代理对象
UserMapper userMapper=sqlSession.getMapper(UserMapper.class);
List<User> users=userMapper.selectAll();
System.out.println(users);
//4. 释放资源
sqlSession.close();
}
}

注意:
如果Mapper接口名称和SQL映射文件名称相同,并在同一目录下,则可以使用包扫描的方式简化SQL映射文件的加载。也就是将核心配置文件的加载映射配置文件的配置修改为
<mappers>
<!--加载sql映射文件-->
<!-- <mapper resource="com/itheima/mapper/UserMapper.xml"/>-->
<!--Mapper代理方式-->
<package name="com.itheima.mapper"/>
</mappers>
返回值类型起别名
在映射配置文件中的 resultType 属性需要配置数据封装的类型(类的全限定名)。而每次这样写是特别麻烦的,Mybatis 提供了 类型别名(typeAliases) 可以简化这部分的书写。
首先需要现在核心配置文件中配置类型别名,也就意味着给pojo包下所有的类起了别名(别名就是类名),不区分大小写。内容如下:
通过上述的配置,我们就可以简化映射配置文件中 resultType 属性值的编写
<mapper namespace="com.itheima.mapper.UserMapper">
<select id="selectAll" resultType="user">
select * from tb_user;
</select>
</mapper>
参考
JavaWebNotes笔记
边栏推荐
- Go project operation method
- The difference between new and make in golang
- RuntimeError: no valid convolution algorithms available in CuDNN
- Strictly abide by the construction period and ensure the quality, this AI data annotation company has done it!
- Submit code process
- How can cross-border e-commerce achieve low-cost and steady growth by laying a good data base
- [redis notes] compressed list (ziplist)
- VIM interval deletion note
- All things work together, and I will review oceanbase's practice in government and enterprise industry
- CDN acceleration requires the domain name to be filed first
猜你喜欢

Implementation of VGA protocol based on FPGA
![[favorite poems] OK, song](/img/1a/e4a4dcca494e4c7bb0e3568f708288.png)
[favorite poems] OK, song

Value sequence < detailed explanation of daily question >

BBR encounters cubic

力扣刷题(2022-6-28)

Ideal car × Oceanbase: when the new forces of car building meet the new forces of database
![[npuctf2020]ezlogin XPath injection](/img/6e/dac4dfa0970829775084bada740542.png)
[npuctf2020]ezlogin XPath injection

Go basic anonymous variable

Typical case of data annotation: how does jinglianwen technology help enterprises build data solutions

ADC of stm32
随机推荐
What can I do after buying a domain name?
Remote connection of raspberry pie by VNC viewer
Application of containerization technology in embedded field
Redis expiration policy +conf record
How does win11 turn on visual control? Win11 method of turning on visual control
Eight bit responder [51 single chip microcomputer]
Realize the linkage between bottomnavigationview and navigation
Solution to boost library link error
[adjustment] postgraduate enrollment of Northeast Petroleum University in 2022 (including adjustment)
[Yangcheng cup 2020] easyphp
Tronapi wave field interface - source code without encryption - can be opened twice - interface document attached - packaging based on thinkphp5 - detailed guidance of the author - July 1, 2022 08:43:
Win11启用粘滞键关闭不了怎么办?粘滞键取消了但不管用怎么解决
golang入门:for...range修改切片中元素的值的另类方法
RuntimeError: no valid convolution algorithms available in CuDNN
Strictly abide by the construction period and ensure the quality, this AI data annotation company has done it!
Simple square wave generating circuit [51 single chip microcomputer and 8253a]
20220524_ Database process_ Statement retention
Go project operation method
购买完域名之后能干什么事儿?
Three solutions to frequent sticking and no response of explorer in win11 system