日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

[mybatis]映射文件_select_resultMap_关联查询_association分步查询延迟加载

發布時間:2023/12/4 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [mybatis]映射文件_select_resultMap_关联查询_association分步查询延迟加载 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

association分步查詢

場景一

查詢Employee的同時查詢員工對應的部門
Employee===Department
一個員工有與之對應的部門信息

Employee表:


Department表:

public interface DepartmentMapper {public Department getDeptById(Integer id);} public interface EmployeeMapperPlus {public Employee getEmpByIdStep(Integer id);} <!-- public Department getDeptById(Integer id); --><select id="getDeptById" resultType="com.atguigu.mybatis.bean.Department">select id,dept_name departmentName from tb1_dept where id = #{id}</select> <resultMap id="MyEmpStep" type="com.atguigu.mybatis.bean.Employee"><!-- 1.先按照員工id查詢員工信息--><!-- 2.根據查詢員工信息中的d_id值去部門表查出部門信息--><!-- 3.部門設置到員工中--><id column="id" property="id"></id><result column="last_name" property="lastName"></result><result column="email" property="email"></result><result column="gender" property="gender"></result><!-- association定義關聯對象的封裝規則select:表明當前屬性是調用select指定的方法查出的結果column:指定將哪一列的值傳給這個方法流程:使用select指定的方法(傳入column指定的這列參數的值)查出對象,并封裝給property指定的屬性--><association property="dept" select="com.atguigu.mybatis.dao.DepartmentMapper.getDeptById"column="d_id"></association></resultMap><!-- public Employee getEmpByIdStep(Integer id);--><select id = "getEmpByIdStep" resultMap="MyEmpStep">select * from tb1_employee where id = #{id}</select> @Testpublic void test03() throws IOException {SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();SqlSession sqlSession = sqlSessionFactory.openSession();try{EmployeeMapperPlus mapper = sqlSession.getMapper(EmployeeMapperPlus.class);Employee emp = mapper.getEmpByIdStep(1);System.out.println(emp);System.out.println(emp.getDept());}finally {sqlSession.close();}}

延遲加載

Employee ===> Dept
我們每次查詢Employee對象的時候,都將一起查詢出來
分段查詢的基礎上,使用延遲加載
可以使部門信息在我們使用的時候再去查詢

lazyLoadingEnabled & aggressiveLazyLoading

<settings><setting name="lazyLoadingEnabled" value="true"/><setting name="aggressiveLazyLoading" value="false"/></settings>


場景二

查詢部門的時候將部門對應的所有員工信息也查詢出來

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定義關聯集合類型的屬性的封裝規則ofType:指定集合里面元素的類型--><collection property="emps" ofType="com.atguigu.mybatis.bean.Employee" ><!--定義這個集合中元素的封裝規則--><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();}}

擴展:多列的值傳遞過去

將多列的值封裝map傳遞
column="{key1=column1,key2=column2}"
fetchType=“lazy”:表示使用延遲加載;
lazy:延遲
eager:立即

總結

以上是生活随笔為你收集整理的[mybatis]映射文件_select_resultMap_关联查询_association分步查询延迟加载的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。