做h5网站,网站变app,产品互联网营销推广,中学生旅游网站开发的论文怎么写对于初学者#xff0c;如何进行mybatis的学习呢#xff1f;我总结了几点#xff0c;会慢慢的更新出来。首先大家需要了解mybatis是什么、用mybatis来做什么、为什么要用mybatis、有什么优缺点#xff1b;当知道了为什么的时候就开始了解如何用的问题#xff0c;如何使用my…对于初学者如何进行mybatis的学习呢我总结了几点会慢慢的更新出来。首先大家需要了解mybatis是什么、用mybatis来做什么、为什么要用mybatis、有什么优缺点当知道了为什么的时候就开始了解如何用的问题如何使用mybatis、有几种使用方式、各种方式的优缺点在这个阶段也会学习mybatis涉及到的一些标签的用法当知道了基础用法之后就开始接触一些高级的用法例如动态sql的使用、mybatis的缓存使用等至此在实战项目中使用mybatis进行开发已经没有问题了。
接下来就开始深入的研究一下mybatis这个持久层的框架在纯技术的方面进行研究提高自己的能力。首先大家需要了解一下mybatis的整体技术架构和工作原理接下来就开始了解一下mybatis各大核心组件的具体功能及其工作原理。至此算是对mybatis的原理简单的了解一下了由于博主的能力有限因此对于mybatis的框架技术研究也就到这里算结束了。
最后会了解一些其他的东西例如mybatis的逆向工程使用、如何开发一个mybatis插件在这里会介绍一下mybatis的分页实现等。
至此mybatis也算是入门了出去就可以和别人说你稍微了解mybatis框架对其也多少有一点自己的理解和看法了。
目录
1、级联属性的映射
2、association指定关联对象映射
3、association 分步查询嵌套查询
4、collection 指定关联集合对象映射
5、collection 分步查询嵌套查询
6、discriminator 鉴别器的使用 上一篇 介绍了输入映射和输出映射结束于自定义映射resultMap的更多用法这一篇主要针对resultMap 的一些高级用法进行叙述。
1、级联属性的映射
这里直接通过例子来看用法例如每一个用户对应一个部门查询用户信息时需要级联查询出部门的信息此时就可以使用resultMap 自定义映射关系具体如下
resultMap iduser2 typecom.app.mapper.Userid columnid propertyid/result columnname propertyname/result columndname propertydepartment.name/
/resultMapselect idselectUser2 resultMapuser2select a.id, a.name, b.tname from oa_user a, oa_department b where a.id #{id} and a.department b.id
/select
此时User 实体类的属性定义如下
private Long id; // id
private String name; // 用户名
private Department department; // 关联的部门
2、association指定关联对象映射
对于1中的sql 映射可以使用 association 标签进行指定关联对象的映射其具体使用如下
select idselectUser3 resultMapuser3select a.id, a.name, b.did, b.dname from oa_user a, oa_department b where a.id #{id} and a.department b.did
/selectresultMap iduser3 typecom.app.mapper.Userid columnid propertyid/result columnname propertyname/association propertydepartment javaTypecom.app.mapper.Departmentid columndid propertydid/result columndname propertyname//association
/resultMap
3、association 分步查询嵌套查询
有的时候sql 语句过于复杂无法一条语句获取到最终的结果需要多条语句或者为了实现延迟加载针对于 2 中例子可以通过嵌套查询的方式实现具体如下
select idgetUserById resultMapuser/selectresultMap typecom.app.mapper.User iduserid columnid propertyid/result columnname propertyname/!-- 嵌套一个查询N1查询 --association propertydepartmentselectcom.app.mapper.UserMapper.getDepartmentByDidcolumndid//resultMapselect idgetDepartmentByDid resultTypecom.app.mapper.Departmentselect did, name from department where did #{did}
/select
如果想要实现延时加载需要在mybatis全局配置文件中设置这个在 全局配置文件 介绍一篇中有介绍
setting namelazyLoadingEnabled valuetrue/setting
4、collection 指定关联集合对象映射
有的时候关联的数据是一个集合例如一个学生有多个老师此时就需要使用collection 标签进行结果对象映射了具体如下
select idgetStudentBySid resultMapstudent1select a.sid, a.sname, b.tid, b.tname from student a, teacher b where b.sid a.sid
/selectresultMap idstudent1 typecom.app.mapper.Studentid columnsid propertysid/result columnsname propertysname/collection propertyteachers ofTypecom.app.mapper.Teacherid columntid propertytid/result columntname propertytname//collection
/resultMap对应的java实体如下
private Long sid;
private String sname;
private ListTeacher teachers;
5、collection 分步查询嵌套查询
类似于 3 如果想要实现延时加载的话可以通过使用 嵌套查询N1查询的方式实现具体如下
select idgetStudentBySid resultMapstudent1select a.sid, a.sname, b.tid, b.tname from student a, teacher b where a.tid b.tid
/selectresultMap idstudent1 typecom.app.mapper.Studentid columnsid propertysid/result columnsname propertysname/collection propertyteachers selectcom.app.mapper.UserMapper.selectTeacherBySid columnsid fetchTypelazy/
/resultMap
select idgetStudentBySid resultTypecom.app.mapper.UserMapper.Teacherselect tid, tname from teacher where sid #{sid}
/select
6、discriminator 鉴别器的使用
鉴别器的作用类似于if ... else ... 选择的作用在进行分步查询时可以对前一步查询出来的自定字段值进行判断来确定下一步的执行行为例如查询用户字段根据用户的性别来进行不同联系方式的映射具体使用如下
select idgetUser resultMapuser3select uid, uname, sex, phone, email from user where uid #{uid}
/selectresultMap iduser3 typecom.app.mapper.Userid columnuid propertyuid/result columnuname propertyuname/discriminator javaTypeint!-- 1 为男若为1 则将用户的手机映射到联系方式 2 为女若为2 则将用户的邮箱映射到联系方式 --case value1result columnphone propertylink_info//casecase value2result columnemail propertylink_info//case/discriminator
/resultMap
这一篇这介绍到这里下一篇开始进行动态sql开发的介绍。