当前位置:网站首页>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笔记
边栏推荐
- Doorplate making C language
- Start from the bottom structure to learn the customization and testing of FPGA --- Xilinx ROM IP
- Writing of head and bottom components of non routing components
- Interface switching based on pyqt5 toolbar button -1
- YOLOX加强特征提取网络Panet分析
- Typical case of data annotation: how does jinglianwen technology help enterprises build data solutions
- Is 408 not fragrant? The number of universities taking the 408 examination this year has basically not increased!
- ServletContext learning diary 1
- Alibaba cloud award winning experience: how to use polardb-x
- ADC of stm32
猜你喜欢
C MVC creates a view to get rid of the influence of layout
Interface switching based on pyqt5 toolbar button -1
Golang common settings - modify background
How difficult is it to be high? AI rolls into the mathematics circle, and the accuracy rate of advanced mathematics examination is 81%!
Go basic anonymous variable
Detailed explanation and application of merging and sorting
Win11启用粘滞键关闭不了怎么办?粘滞键取消了但不管用怎么解决
4 special cases! Schools in area a adopt the re examination score line in area B!
Li Kou brush questions (2022-6-28)
Remote connection of raspberry pie by VNC viewer
随机推荐
C# MVC创建一个视图摆脱布局的影响
Prometheus deployment
提交代码流程
Set right click to select vs code to open the file
Where is the win11 microphone test? Win11 method of testing microphone
golang入门:for...range修改切片中元素的值的另类方法
Numerical solution of partial differential equations with MATLAB
Win11启用粘滞键关闭不了怎么办?粘滞键取消了但不管用怎么解决
实现BottomNavigationView和Navigation联动
Detailed explanation and application of merging and sorting
抖音实战~点赞数量弹框
C MVC creates a view to get rid of the influence of layout
Eight bit responder [51 single chip microcomputer]
【Redis笔记】压缩列表(ziplist)
2022年最新最全软件测试面试题大全
内网渗透 | 手把手教你如何进行内网渗透
[favorite poems] OK, song
为什么RTOS系统要使用MPU?
Sword finger offer II 099 Sum of minimum paths - double hundred code
Talk about memory model and memory order