[mybatis]映射文件_select_resultMap_关联查询_association分步查询延迟加载
生活随笔
收集整理的這篇文章主要介紹了
[mybatis]映射文件_select_resultMap_关联查询_association分步查询延迟加载
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
association分步查詢
場景一
查詢Employee的同時查詢員工對應(yīng)的部門
Employee===Department
一個員工有與之對應(yīng)的部門信息
Employee表:
Department表:
延遲加載
Employee ===> Dept
我們每次查詢Employee對象的時候,都將一起查詢出來
分段查詢的基礎(chǔ)上,使用延遲加載
可以使部門信息在我們使用的時候再去查詢
lazyLoadingEnabled & aggressiveLazyLoading
<settings><setting name="lazyLoadingEnabled" value="true"/><setting name="aggressiveLazyLoading" value="false"/></settings>
場景二
查詢部門的時候?qū)⒉块T對應(yīng)的所有員工信息也查詢出來
public class Department {private Integer id;private String departmentName;private List<Employee> emps;} public interface DepartmentMapper {public Department getDeptByIdPlus(Integer id);} <resultMap id="MyDept" type="com.atguigu.mybatis.bean.Department"><id column="did" property="id"></id><result column="dept_name" property="departmentName"></result><!--collection定義關(guān)聯(lián)集合類型的屬性的封裝規(guī)則ofType:指定集合里面元素的類型--><collection property="emps" ofType="com.atguigu.mybatis.bean.Employee" ><!--定義這個集合中元素的封裝規(guī)則--><id column="eid" property="id"></id><result column="last_name" property="lastName"></result><result column="email" property="email"></result><result column="gender" property="gender"></result></collection></resultMap><!-- public Department getDeptByIdPlus(Integer id);--><select id="getDeptByIdPlus" resultMap = "MyDept">SELECT d.id did, d.dept_name dept_name ,e.id eid,e.last_name last_name,e.email email,e.gender genderFROM tb1_dept d LEFT JOIN tb1_employee e ON d.id = e.d_id WHERE d.id = #{id}</select> @Testpublic void test04() throws IOException {SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();SqlSession sqlSession = sqlSessionFactory.openSession();try{DepartmentMapper mapper = sqlSession.getMapper(DepartmentMapper.class);Department deptByIdPlus = mapper.getDeptByIdPlus(1);System.out.println(deptByIdPlus);System.out.println(deptByIdPlus.getEmps());}finally {sqlSession.close();}}擴(kuò)展:多列的值傳遞過去
將多列的值封裝map傳遞
column="{key1=column1,key2=column2}"
fetchType=“l(fā)azy”:表示使用延遲加載;
lazy:延遲
eager:立即
總結(jié)
以上是生活随笔為你收集整理的[mybatis]映射文件_select_resultMap_关联查询_association分步查询延迟加载的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 怎么运用电脑自带的画图软件快速改小图片电
- 下一篇: 菜市场里的打印时光:手机照片打印受青岛老