当前位置:网站首页>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笔记
边栏推荐
- 密码技术---分组密码的模式
- Prometheus deployment
- Yolox enhanced feature extraction network panet analysis
- C MVC creates a view to get rid of the influence of layout
- Markdown basic grammar
- 跨境电商如何通过打好数据底座,实现低成本稳步增长
- 在SOUI里使用真窗口时使用SOUI的滚动条
- ServletContext learning diary 1
- Static file display problem
- Solution to boost library link error
猜你喜欢
数字图像处理实验目录
非路由组件之头部组件和底部组件书写
C#中Linq用法汇集
基于Pyqt5工具栏按钮可实现界面切换-1
详解Promise使用
Prometheus deployment
Typical case of data annotation: how does jinglianwen technology help enterprises build data solutions
Redis expiration policy +conf record
RuntimeError: no valid convolution algorithms available in CuDNN
設置單擊右鍵可以選擇用VS Code打開文件
随机推荐
[hardware] origin of standard resistance value
Prometheus deployment
潘多拉 IOT 开发板学习(HAL 库)—— 实验3 按键输入实验(学习笔记)
Getting started with golang: for Range an alternative method of modifying the values of elements in slices
Which common ports should the server open
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:
Three solutions to frequent sticking and no response of explorer in win11 system
Pandora IOT development board learning (HAL Library) - Experiment 4 serial port communication experiment (learning notes)
[analysis of STL source code] imitation function (to be supplemented)
Temperature measurement and display of 51 single chip microcomputer [simulation]
Solution: exceptiole 'xxxxx QRTZ_ Locks' doesn't exist and MySQL's my CNF file append lower_ case_ table_ Error message after names startup
Print out mode of go
Editor Caton
万物并作,吾以观复|OceanBase 政企行业实践
MySQL queries nearby data And sort by distance
CDN 加速,需要域名先备案
Win11如何开启目视控制?Win11开启目视控制的方法
"A good programmer is worth five ordinary programmers!"
聊聊内存模型与内存序
VIM interval deletion note