resultMap_关联查询_collection 使用规则
Posted Dong诗原
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了resultMap_关联查询_collection 使用规则相关的知识,希望对你有一定的参考价值。
1、项目结构
2、Department.java
package com.atguigu.mybatis.bean; import java.util.List; public class Department { private Integer id; private String departmentName; private List<Employee> emps; public List<Employee> getEmps() { return emps; } public void setEmps(List<Employee> emps) { this.emps = emps; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getDepartmentName() { return departmentName; } public void setDepartmentName(String departmentName) { this.departmentName = departmentName; } @Override public String toString() { return "Department [id=" + id + ", departmentName=" + departmentName + ", emps=" + emps + "]"; } }
3、Employee.java
package com.atguigu.mybatis.bean; public class Employee { private Integer id; private String lastName; private String email; private String gender; private Department dept; public Department getDept() { return dept; } public void setDept(Department dept) { this.dept = dept; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } @Override public String toString() { return "Employee [id=" + id + ", lastName=" + lastName + ", email=" + email + ", gender=" + gender + "]"; } public Employee(Integer id, String lastName, String email, String gender) { super(); this.id = id; this.lastName = lastName; this.email = email; this.gender = gender; } public Employee() { super(); // TODO Auto-generated constructor stub } }
4、DepartmentMapper.java
package com.atguigu.mybatis.dao; import com.atguigu.mybatis.bean.Department; public interface DepartmentMapper { public Department getDeptById(Integer id); public Department getDeptByIdPlus(Integer id); }
5、EmployeeMapperPlus.java
package com.atguigu.mybatis.dao; import com.atguigu.mybatis.bean.Employee; public interface EmployeeMapperPlus { public Employee getEmpById(Integer id); public Employee getEmpAndDept(Integer id) ; public Employee getEmpByIdStep(Integer id) ; }
6、MybatisTest.java
package com.atguigu.mybatis.test; import java.io.IOException; import java.io.InputStream; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import org.junit.Test; import com.atguigu.mybatis.bean.Department; import com.atguigu.mybatis.bean.Employee; import com.atguigu.mybatis.dao.DepartmentMapper; import com.atguigu.mybatis.dao.EmployeeMapperPlus; public class MybatisTest { private SqlSessionFactory getSqlSessionFactory() throws IOException { String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); return new SqlSessionFactoryBuilder().build(inputStream); } @Test public void test3() throws IOException { SqlSessionFactory sqlSessionFactory =getSqlSessionFactory(); //1、获取到的SqlSession不会自动提交 SqlSession openSession= sqlSessionFactory.openSession(); try { DepartmentMapper mapper=openSession.getMapper(DepartmentMapper.class); Department department=mapper.getDeptByIdPlus(1); System.out.println(department); System.out.println(department.getEmps()); //3、手动提交 openSession.commit(); } finally { openSession.close(); } } }
7、DepartmentMapper.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.atguigu.mybatis.dao.DepartmentMapper"> <!-- public Department getDeptById(Integer id); --> <select id="getDeptById" resultType="com.atguigu.mybatis.bean.Department"> select id,dept_name departmentName from tbl_dept where id=#{id} </select> <!-- private Integer id; private String departmentName; private List<Employee> emps; public Department getDeptByIdPlus(Integer id); --> <resultMap type="com.atguigu.mybatis.bean.Department" id="Mydept"> <id column="did" property="id"/> <result column="dept_name" property="departmentName"/> <!-- collection定义关联集合类型的属性的封装规则 指定集合里元素类型 --> <collection property="emps" ofType="com.atguigu.mybatis.bean.Employee"> <id column="eid" property="id"/> <result column="last_name" property="lastName"/> <result column="email" property="email"/> <result column="gender" property="gender"/> </collection> </resultMap> <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 gender FROM tbl_dept d LEFT JOIN tbl_employee e ON d.id=e.d_id WHERE d.id =#{id} </select> </mapper>
注:collection定义关联集合类型的属性的封装规则
ofType:指定集合里元素类型,其他与association一样
<collection property="emps" ofType="com.atguigu.mybatis.bean.Employee"> <id column="eid" property="id"/> <result column="last_name" property="lastName"/> <result column="email" property="email"/> <result column="gender" property="gender"/> </collection>
8、EmployeeMapperPlus.xml(此项目中没用到)
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.atguigu.mybatis.dao.EmployeeMapperPlus"> <resultMap type="com.atguigu.mybatis.bean.Employee" id="MySimpleEmp"> <id column="id" property="id"/> <result column="last_name" property="lastName"/> </resultMap> <!-- public Employee getEmpById(Integer id); --> <select id="getEmpById" resultMap="MySimpleEmp"> select * from tbl_employee where id=#{id} </select> <!-- 场景一: 查询employee的同时查询员工对应的部门 SELECT e.id id,e.last_name lastName,e.gender gender,e.d_id d_id, d.id did,d.dept_name dept_name FROM tbl_employee e,tbl_dept d WHERE e.d_id=d.id AND e.id=2 --> <!-- 联合查询:级联属性封装结构集 --> <resultMap type="com.atguigu.mybatis.bean.Employee" id="MyDifEmp"> <id column="id" property="id"/> <result column="email" property="email"/> <result column="last_name" property="lastName"/> <result column="gender" property="gender"/> <result column="did" property="dept.id"/> <result column="dept_name" property="dept.departmentName"/> </resultMap> <resultMap type="com.atguigu.mybatis.bean.Employee" id="MyDifEmp2"> <id column="id" property="id"/> <result column="email" property="email"/> <result column="last_name" property="lastName"/> <result column="gender" property="gender"/> <!-- association可以指定联合的JavaBean对象 property=“dept”:指定哪个属性是联合的对象 JavaType:指定这个属性的类型 --> <association property="dept" javaType="com.atguigu.mybatis.bean.Department"> <id column="did" property="id"/> <result column="dept_name" property="departmentName"/> </association> </resultMap> <!-- public Employee getEmpAndDept(Integer id) ; --> <select id="getEmpAndDept" resultMap="MyDifEmp2"> SELECT e.id id,e.email email,e.last_name last_name,e.gender gender,e.d_id d_id, d.id did,d.dept_name dept_name FROM tbl_employee e,tbl_dept d WHERE e.d_id=d.id AND e.id=#{id} </select> <!-- 使用association分步查询: 1、先按照员工id查询员工信息 2、根据查询员工信息的d_id值去部门部门表查出部门信息 3、部门设置到员工中 --> <resultMap type="com.atguigu.mybatis.bean.Employee" id="MyEmpStep"> <id column="id" property="id"/> <result column="last_name" property="lastName"/> <result column="email" property="email"/> <result column="gender" property="gender"/> <!-- 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 tbl_employee where id=#{id} </select> <!-- =================================association=================================================== --> <!-- 场景二: DepartmentMapper.xml中 --> </mapper>
9、mybatis-config.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <!-- <settings> <setting name="lazyLoadingEnabled" value="true"/> <setting name="aggressiveLazyLoading" value="false"/> </settings> --> <environments default="development"> <environment id="development"> <transactionManager type="JDBC" /> <!-- 配置数据库连接信息 --> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/mybatis" /> <property name="username" value="root" /> <property name="password" value="111111" /> </dataSource> </environment> </environments> <mappers> <mapper resource="com/atguigu/mybatis/dao/EmployeeMapperPlus.xml"/> <mapper resource="com/atguigu/mybatis/dao/DepartmentMapper.xml"/> </mappers> </configuration>
以上是关于resultMap_关联查询_collection 使用规则的主要内容,如果未能解决你的问题,请参考以下文章
resultMap2_关联查询collection分步查询&延迟加载