当前位置:网站首页>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笔记
边栏推荐
- Redis 过期策略+conf 记录
- Writing of head and bottom components of non routing components
- The difference between new and make in golang
- 跨境电商如何通过打好数据底座,实现低成本稳步增长
- @BindsInstance在Dagger2中怎么使用
- CDN 加速,需要域名先备案
- Li Kou brush questions (2022-6-28)
- The concepts of terminal voltage, phase voltage and line voltage in FOC vector control and BLDC control are still unclear
- Simple square wave generating circuit [51 single chip microcomputer and 8253a]
- Interface switching based on pyqt5 toolbar button -1
猜你喜欢

golang入门:for...range修改切片中元素的值的另类方法

面试过了,起薪16k

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

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

数字图像处理实验目录

Hisilicon VI access video process

Detailed explanation and application of merging and sorting

BBR 遭遇 CUBIC

Compose 中的 'ViewPager' 详解 | 开发者说·DTalk

Go basic constant definition and use
随机推荐
[live broadcast appointment] database obcp certification comprehensive upgrade open class
为什么RTOS系统要使用MPU?
Print out mode of go
ServletContext learning diary 1
力扣刷题(2022-6-28)
基于Pyqt5工具栏按钮可实现界面切换-2
The difference between new and make in golang
VIM interval deletion note
ping域名报错unknown host,nslookup/systemd-resolve可以正常解析,ping公网地址通怎么解决?
The first batch of Tencent cloud completed the first cloud native security maturity assessment in China
Cryptographic technology -- key and ssl/tls
Strictly abide by the construction period and ensure the quality, this AI data annotation company has done it!
(stinger) use pystinger Socks4 to go online and not go out of the network host
阿里云有奖体验:如何使用 PolarDB-X
Value sequence < detailed explanation of daily question >
Arduino - character judgment function
FOC矢量控制及BLDC控制中的端电压、相电压、线电压等概念别还傻傻分不清楚
Typical case of data annotation: how does jinglianwen technology help enterprises build data solutions
Win11系统explorer频繁卡死无响应的三种解决方法
Configuration clic droit pour choisir d'ouvrir le fichier avec vs Code