推上网站,新网官方网站,彩票网站怎么做代理,建设局网站查询association分步查询
场景一 查询Employee的同时查询员工对应的部门 EmployeeDepartment 一个员工有与之对应的部门信息 Employee表: Department表#xff1a; public interface DepartmentMapper {public Department getDeptById(Integer id);}public interface EmployeeMap…association分步查询
场景一 查询Employee的同时查询员工对应的部门 EmployeeDepartment 一个员工有与之对应的部门信息 Employee表: Department表 public interface DepartmentMapper {public Department getDeptById(Integer id);}public interface EmployeeMapperPlus {public Employee getEmpByIdStep(Integer id);}!-- public Department getDeptById(Integer id);
--select idgetDeptById resultTypecom.atguigu.mybatis.bean.Departmentselect id,dept_name departmentName from tb1_dept where id #{id}/selectresultMap idMyEmpStep typecom.atguigu.mybatis.bean.Employee!-- 1.先按照员工id查询员工信息--!-- 2.根据查询员工信息中的d_id值去部门表查出部门信息--!-- 3.部门设置到员工中--id columnid propertyid/idresult columnlast_name propertylastName/resultresult columnemail propertyemail/resultresult columngender propertygender/result!-- association定义关联对象的封装规则select:表明当前属性是调用select指定的方法查出的结果column:指定将哪一列的值传给这个方法流程:使用select指定的方法(传入column指定的这列参数的值)查出对象并封装给property指定的属性--association propertydept selectcom.atguigu.mybatis.dao.DepartmentMapper.getDeptByIdcolumnd_id/association/resultMap!-- public Employee getEmpByIdStep(Integer id);--select id getEmpByIdStep resultMapMyEmpStepselect * from tb1_employee where id #{id}/selectTestpublic 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 settingssetting namelazyLoadingEnabled valuetrue/setting nameaggressiveLazyLoading valuefalse//settings场景二 查询部门的时候将部门对应的所有员工信息也查询出来 public class Department {private Integer id;private String departmentName;private ListEmployee emps;}public interface DepartmentMapper {public Department getDeptByIdPlus(Integer id);}resultMap idMyDept typecom.atguigu.mybatis.bean.Departmentid columndid propertyid/idresult columndept_name propertydepartmentName/result!--collection定义关联集合类型的属性的封装规则ofType:指定集合里面元素的类型--collection propertyemps ofTypecom.atguigu.mybatis.bean.Employee !--定义这个集合中元素的封装规则--id columneid propertyid/idresult columnlast_name propertylastName/resultresult columnemail propertyemail/resultresult columngender propertygender/result/collection/resultMap!-- public Department getDeptByIdPlus(Integer id);--select idgetDeptByIdPlus resultMap MyDeptSELECT 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}/selectTestpublic 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{key1column1,key2column2} fetchType“lazy”表示使用延迟加载 lazy延迟 eager立即