1)查询单行数据返回单个对象 public Employee getEmployeeById(Integer id );
2)查询多行数据返回对象的集合 public List getAllEmps();
3)查询单行数据返回Map集合 public Map<String,Object> getEmployeeByIdReturnMap(Integer id );
4)查询多行数据返回Map集合 @MapKey(“id”) // 指定使用对象的哪个属性来充当map的key(对象的属性,而不是数据库的列) public Map<Integer,Employee> getAllEmpsReturnMap();
1)autoMappingBehavior默认是PARTIAL,开启自动映射的功能。唯一的要求是列名和javaBean属性名一致 2)如果autoMappingBehavior设置为null则会取消自动映射 3)数据库字段命名规范,POJO属性符合驼峰命名法,如A_COLUMNaColumn,我们可以开启自动驼峰命名规则映射功能,mapUnderscoreToCamelCase=true
缺点:多表查询 完成不了
1)自定义resultMap,实现高级结果集映射 2)id :用于完成主键值的映射 3)result :用于完成普通列的映射 4)association :一个复杂的类型关联;许多结果将包成这种类型 5)collection : 复杂类型的集
<select id="getEmployeeById" resultMap="myEmp"> select id, last_name,email, gender from tbl_employee where id =#{id} </select> <resultMap type="com.atguigu.mybatis.beans.Employee" id="myEmp"> <id column="id" property="id" /> <result column="last_name" property="lastName"/> <result column="email" property="email"/> <result column="gender" property="gender"/> </resultMap>1)POJO中的属性可能会是一个对象,我们可以使用联合查询,并以级联属性的方式封装对象.使用association标签定义对象的封装规则
public class Department { private Integer id ; private String departmentName ; // 省略 get/set方法 } public class Employee { private Integer id ; private String lastName; private String email ; private String gender ; private Department dept ; // 省略 get/set方法 }2)使用级联的方式:
<select id="getEmployeeAndDept" resultMap="myEmpAndDept" > SELECT e.id eid, e.last_name, e.email,e.gender ,d.id did, d.dept_name FROM tbl_employee e , tbl_dept d WHERE e.d_id = d.id AND e.id = #{id} </select> <resultMap type="com.atguigu.mybatis.beans.Employee" id="myEmpAndDept"> <id column="eid" property="id"/> <result column="last_name" property="lastName"/> <result column="email" property="email"/> <result column="gender" property="gender"/> <!-- 级联的方式 --> <result column="did" property="dept.id"/> <result column="dept_name" property="dept.departmentName"/> </resultMap>3)Association
<resultMap type="com.atguigu.mybatis.beans.Employee" id="myEmpAndDept"> <id column="eid" property="id"/> <result column="last_name" property="lastName"/> <result column="email" property="email"/> <result column="gender" property="gender"/> <association property="dept" javaType="com.atguigu.mybatis.beans.Department"> <id column="did" property="id"/> <result column="dept_name" property="departmentName"/> </association> </resultMap>association 分步查询 1)实际的开发中,对于每个实体类都应该有具体的增删改查方法,也就是DAO层, 因此 对于查询员工信息并且将对应的部门信息也查询出来的需求,就可以通过分步的方式 完成查询。 ①先通过员工的id查询员工信息 ②再通过查询出来的员工信息中的外键(部门id)查询对应的部门信息.
<select id="getEmployeeAndDeptStep" resultMap="myEmpAndDeptStep"> select id, last_name, email,gender,d_id from tbl_employee where id =#{id} </select> <resultMap type="com.atguigu.mybatis.beans.Employee" id="myEmpAndDeptStep"> <id column="id" property="id" /> <result column="last_name" property="lastName"/> <result column="email" property="email"/> <result column="gender" property="gender"/> <association property="dept" select="com.atguigu.mybatis.dao.DepartmentMapper.getDeptById" column="d_id" fetchType="eager"> </association> </resultMap>association 分步查询使用延迟加载 1)在分步查询的基础上,可以使用延迟加载来提升查询的效率,只需要在全局的 Settings中进行如下的配置:
<!-- 开启延迟加载 --> <setting name="lazyLoadingEnabled" value="true"/> <!-- 设置加载的数据是按需还是全部 --> <setting name="aggressiveLazyLoading" value="false"/>collection 1)POJO中的属性可能会是一个集合对象,我们可以使用联合查询,并以级联属性的方式封装对象.使用collection标签定义对象的封装规则
<select id="getDeptAndEmpsById" resultMap="myDeptAndEmps"> SELECT d.id did, d.dept_name ,e.id eid ,e.last_name ,e.email,e.gender FROM tbl_dept d LEFT OUTER JOIN tbl_employee e ON d.id = e.d_id WHERE d.id = #{id} </select> <resultMap type="com.atguigu.mybatis.beans.Department" id="myDeptAndEmps"> <id column="did" property="id"/> <result column="dept_name" property="departmentName"/> <!-- property: 关联的属性名 ofType: 集合中元素的类型 --> <collection property="emps" ofType="com.atguigu.mybatis.beans.Employee"> <id column="eid" property="id"/> <result column="last_name" property="lastName"/> <result column="email" property="email"/> <result column="gender" property="gender"/> </collection> </resultMap>collection 分步查询
1)实际的开发中,对于每个实体类都应该有具体的增删改查方法,也就是DAO层, 因此 对于查询部门信息并且将对应的所有的员工信息也查询出来的需求,就可以通过分步的方式完成查询。 ③先通过部门的id查询部门信息 ④再通过部门id作为员工的外键查询对应的部门信息.
<select id="getDeptAndEmpsByIdStep" resultMap="myDeptAndEmpsStep"> select id ,dept_name from tbl_dept where id = #{id} </select> <resultMap type="com.atguigu.mybatis.beans.Department" id="myDeptAndEmpsStep"> <id column="id" property="id"/> <result column="dept_name" property="departmentName"/> <collection property="emps" select="com.atguigu.mybatis.dao.EmployeeMapper.getEmpsByDid" column="id"> </collection> </resultMap>collection 分步查询使用延迟加载 扩展: 分步查询多列值的传递 1)如果分步查询时,需要传递给调用的查询中多个参数,则需要将多个参数封装成 Map来进行传递,语法如下: {k1=v1, k2=v2…} 2)在所调用的查询方,取值时就要参考Map的取值方式,需要严格的按照封装map 时所用的key来取值.
1)在 和标签中都可以设置fetchType,指定本次查询是否要使用延迟加载。默认为 fetchType=”lazy” ,如果本次的查询不想使用延迟加载,则可设置为 fetchType=”eager”. 2)fetchType可以灵活的设置查询是否需要使用延迟加载,而不需要因为某个查询不想使用延迟加载将全局的延迟加载设置关闭.