当前位置:网站首页>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笔记
边栏推荐
- "A good programmer is worth five ordinary programmers!"
- The concepts of terminal voltage, phase voltage and line voltage in FOC vector control and BLDC control are still unclear
- Win11麦克风测试在哪里?Win11测试麦克风的方法
- ping域名报错unknown host,nslookup/systemd-resolve可以正常解析,ping公网地址通怎么解决?
- 万物并作,吾以观复|OceanBase 政企行业实践
- 用matlab调用vs2015来编译vs工程
- Strictly abide by the construction period and ensure the quality, this AI data annotation company has done it!
- Quantitative analysis of PSNR, SSIM and RMSE
- Troubleshooting the cause of the crash when STM32 serial port dam receives 253 bytes
- 富滇银行完成数字化升级|OceanBase数据库助力布局分布式架构中台
猜你喜欢

Realize the linkage between bottomnavigationview and navigation

C# MVC创建一个视图摆脱布局的影响

Win11系统explorer频繁卡死无响应的三种解决方法
![[favorite poems] OK, song](/img/1a/e4a4dcca494e4c7bb0e3568f708288.png)
[favorite poems] OK, song

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

解决:exceptiole ‘xxxxx.QRTZ_LOCKS‘ doesn‘t exist以及mysql的my.cnf文件追加lower_case_table_names后启动报错

RuntimeError: no valid convolution algorithms available in CuDNN

BBR 遭遇 CUBIC

“一个优秀程序员可抵五个普通程序员!”

QT qpprogressbar details
随机推荐
CDN acceleration requires the domain name to be filed first
Alibaba cloud award winning experience: how to use polardb-x
简述中台的常识
第三方支付功能测试点【杭州多测师_王sir】【杭州多测师】
Go basic anonymous variable
Go basic data type
非路由组件之头部组件和底部组件书写
BBR 遭遇 CUBIC
Pandora IOT development board learning (HAL Library) - Experiment 3 key input experiment (learning notes)
PotPlayer设置最小化的快捷键
ServletContext learning diary 1
【直播预约】数据库OBCP认证全面升级公开课
Yolox enhanced feature extraction network panet analysis
php 获取真实ip
Why does RTOS system use MPU?
Compose 中的 'ViewPager' 详解 | 开发者说·DTalk
The use of 8255 interface chip and ADC0809
“一个优秀程序员可抵五个普通程序员!”
Ideal car × Oceanbase: when the new forces of car building meet the new forces of database
SharedPreferences save list < bean > to local and solve com google. gson. internal. Linkedtreemap cannot be cast to exception