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://i-blog.csdnimg.cn/blog_migrate/52bd48916dd978334e5ffcb9453f77bc.png)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-tDwffjcE-1684997920737)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20230503150312472.png)]](/https://i-blog.csdnimg.cn/blog_migrate/0974596807c46b6bacee96fb346b16ca.png)
2.xml命名空间使用对应xxxmapper接口的权限定名
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-TO8R9nHF-1684997920738)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20230503150537670.png)]](/https://i-blog.csdnimg.cn/blog_migrate/6371990c5f3e6d9246150c8c9aa96f8f.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://i-blog.csdnimg.cn/blog_migrate/79cbc046e2adcccbea3e0aa71b773882.png)
mapper文件
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-OcPqv2vr-1684997920739)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20230503152801372.png)]](/https://i-blog.csdnimg.cn/blog_migrate/57098ae79554b9c170b7486e4fbb43df.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://i-blog.csdnimg.cn/blog_migrate/a500c1aecc373dacceeeea94ce54088e.png)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-QnZxj1u2-1684997920741)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20230503161618315.png)]](/https://i-blog.csdnimg.cn/blog_migrate/52f7ad4cad8f75b3ac3d5982aceb5055.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://i-blog.csdnimg.cn/blog_migrate/42a08a4d9d877678caec164d191834d8.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://i-blog.csdnimg.cn/blog_migrate/eccd685f841ec6c0aee532427c68ee47.png)
mapper接口
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-5IfLo17G-1684997920743)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20230503205731007.png)]](/https://i-blog.csdnimg.cn/blog_migrate/31005a6cf31b12ba0f923d9c580d3efa.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://i-blog.csdnimg.cn/blog_migrate/678077fd9557b248aabbbc617999f0d9.png)
测试类
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-BbYOftJk-1684997920745)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20230503205617420.png)]](/https://i-blog.csdnimg.cn/blog_migrate/c52a69623479cf6258f8fec87c5b9dd3.png)
id}
mapper接口
[外链图片转存中...(img-t06bZELT-1684997920744)]
测试类
[外链图片转存中...(img-BbYOftJk-1684997920745)]
文章详细介绍了MyBatis中Mapper接口的使用,包括接口与XML映射文件的关联规则,方法命名与SQL执行ID的匹配,以及参数和返回类型的规定。还讨论了Mapper接口的底层动态代理实现,多条件查询的处理,如使用#防止SQL注入,以及动态SQL的运用,如if、where、set和foreach在条件判断和循环遍历中的作用。同时,文章提到了在实际操作中的注意事项和最佳实践。

1万+

被折叠的 条评论
为什么被折叠?



