mapper接口和原理
之前的持久层组成部分:UserMapper.xml+IUserDAO+UserDAOimpl
使用mapper接口:UserMapper.xml+UserMaper接口
mapper接口的好处;
避免持久层里面传入参数错误:以前里面写错了不会报错,只有等到运行代码才能看到错误,第二个参数的类型是Objiect
MAPPer使用注意
1.mapper接口的命名为xxxMapper,包的路径和对应的xml文件的路径相同,编译后的路径相同
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Lj9OHtNc-1684997920735)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20230503150259299.png)]](https://img-blog.csdnimg.cn/16619d3a658644dab3486f002e1d9e72.png)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-tDwffjcE-1684997920737)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20230503150312472.png)]](https://img-blog.csdnimg.cn/7fc1d373dd6c457b8f91cfb11452f7c7.png)
2.xml命名空间使用对应xxxmapper接口的权限定名
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-TO8R9nHF-1684997920738)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20230503150537670.png)]](https://img-blog.csdnimg.cn/c6f2854c61634b8d9f5c1b009a4a9c13.png)
3.mapper 接口方法名和mapper.xml文件中的(select | update | delete | insert)id值一样
xml文件
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-KNyNJHDm-1684997920739)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20230503152734887.png)]](https://img-blog.csdnimg.cn/5d924ed91ac54bec9fd582713be2bfe3.png)
mapper文件
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-OcPqv2vr-1684997920739)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20230503152801372.png)]](https://img-blog.csdnimg.cn/47aac3088f1748e692f854194c2c1b04.png)
4.方法返回的类型对应xml中 resultType / resultMap 类型
5.方法的参数类型对应 SQL 元素中定义的 paramterType 类型(一般不写)。
在测试中使用mapper接口
@Test
public void get() {
SqlSession session = MybatisUtil.getSession();
UserMapper mapper = session.getMapper(UserMapper.class);
System.out.println(mapper.get(1L));
session.close();
}
注意:mapper.get是中的get是mapper接口中的方法名,里面的传的值的类型由接口进行规定
mapper接口原理
底层就是mybatis使用动态代理来创建mapper的实现类对象
底层的操作方式和以前一样
使用mapper进行多条件查询
使用多条件的时候,mybatis会将多个条件进行封装成map集合(arg0,arg1,param1,param2),
此时在xml中的select语句直接使用username=#{username} and password=#{password}就会出错
解决办法:在xxxMapper中加上@param注解,
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-nLImMwC8-1684997920740)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20230503161637126.png)]](https://img-blog.csdnimg.cn/e913056376e14dbc9cfc2b99f41d9150.png)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-QnZxj1u2-1684997920741)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20230503161618315.png)]](https://img-blog.csdnimg.cn/86a09a3de2f942d2aab35fa92f9987d7.png)
Mybatis中的#和$的区别
1.相同点
都可以获取map或者javabean中的对象信息
2.不同点
(1)**#会将所有传过来的任何类型的参数加上单引号,$**不会自动加上,只能手动在传参数那里加单引号,否则将传过来的参数作为SQL语句的一部分
(2)#可以防止sql注入问题(preparaStatement),安全,$不安全
(3)#支持将简单类型(string int)作为参数值,$不行
Mybatis中的动态sql:if和where
**使用地方:**进行多条件判断的时候
xml中的if作用
test里面的是一个boolean表达式
xml中的where作用
满足条件就将where加进去,不满足条件的就不将where进行拼接
where和if联合使用,使用CDATA进行包裹运算符,或者使用转义符
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-7VzFHvDG-1684997920742)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20230503190758002.png)]](https://img-blog.csdnimg.cn/0abcfcfbcf0c436092ef82c6abe1f025.png)
Mybatis中的动态sql:set
使用set可以防止修改数据的时候丢失数据,当某个属性传入的值为null的时候,会丢失数据
set和if配合进行使用,在if的外层加上set
**注意事项:**修改的时候mapper接口不能返回任何类型,否则报错,弄一个测试类进行测试
xml
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-peWEaOxT-1684997920742)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20230503203550475.png)]](https://img-blog.csdnimg.cn/9e2c9bd7c8504273bed7a5c676ebfd6f.png)
mapper接口
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-5IfLo17G-1684997920743)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20230503205731007.png)]](https://img-blog.csdnimg.cn/65c8acf690df4e3fb721f74fff137635.png)
测试类
@Test
public void update(){
SqlSession session = MybatisUtil.getSession();
EmployeeMapper mapper = session.getMapper(EmployeeMapper.class);
Employee employee = new Employee();
employee.setSalary(new BigDecimal(1300));
employee.setId(1L);
mapper.update(employee);
session.commit();
session.close();
}
Mybatis中的动态sql:foreach
使用地方:批量删除的时候,使用的数组或者集合进行删除就使用foreach遍历进行删除
注意事项:编写mapper接口的时候需要增加一个注解,否则一个数组xml问津不能进行识别
xml文件
<delete id="delete" >
delete from employee
where id in
<!--
collection:需要遍历的数组或者集合
open:开始拼接的字符串
item:遍历后元素的别名
separatotor:遍历元素后使用的分隔符
close:遍历结束拼接的字符串
-->
<foreach collection="ids" open="(" item="id" separator="," close=")">
#{id}
</foreach>
</delete>
mapper接口
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-t06bZELT-1684997920744)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20230503205546291.png)]](https://img-blog.csdnimg.cn/14d6fea391f04bb5a4e162249fecdd1f.png)
测试类
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-BbYOftJk-1684997920745)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20230503205617420.png)]](https://img-blog.csdnimg.cn/06c668ccf51e4b639724ceb8d09293bf.png)
id}
mapper接口
[外链图片转存中...(img-t06bZELT-1684997920744)]
测试类
[外链图片转存中...(img-BbYOftJk-1684997920745)]



















