提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
文章目录
Result Maps 是什么
ResultMap 属性
一、Java数据访问接口
二、XML实现
1、*分步查询 + 延迟加载
三、Test
1.读入数据
总结
Result Maps 是什么
resultMap 元素是 MyBatis 中最重要最强大的元素。它可以让你从 90% 的 JDBC ResultSets 数据提取代码中解放出来, 并在一些情形下允许你做一些 JDBC 不支持的事情。 实际上,在对复杂语句进行联合映射的时候,它很可能可以代替数千行的同等功能的代码。 ResultMap 的设计思想是,简单的语句不需要明确的结果映射,而复杂一点的语句只需要描述它们的关系就行了。
ResultMap 属性
- constructor - 用于在实例化类时,注入结果到构造方法中 idArg - ID 参数;标记出作为 ID 的结果可以帮助提高整体性能 arg - 将被注入到构造方法的一个普通结果
- id – 一个 ID 结果;标记出作为 ID 的结果可以帮助提高整体性能
- result – 注入到字段或 JavaBean 属性的普通结果
- association – 一个复杂类型的关联;许多结果将包装成这种类型 嵌套结果映射 – 关联可以指定为一个 resultMap 元素,或者引用一个
- collection – 一个复杂类型的集合 嵌套结果映射 – 集合可以指定为一个 resultMap 元素,或者引用一个
- discriminator – 使用结果值来决定使用哪个 resultMap
- case – 基于某些值的结果映射 嵌套结果映射
- – 一个 case 也是一个映射它本身的结果,因此可以包含很多相 同的元素,或者它可以参照一个外部的 resultMap。
提示:以下是本篇文章正文内容,下面案例可供参考
一、Java数据访问接口
ublic interface EmployeeDao {/*** 使用as别名的方式查询所有员工*/public List<Employee> selectAll();/*** 使用resultMap的方式*/public List<Employee> selectAll2();/*** 关联查询*/public List<Employee> selectAll3();/*** 关联查询:使用association 标签来指定关联的javaBean的封装方式*/public List<Employee> selectAll4();/*** 分步查询方法*/public Employee selectStep(int eid);}
二、XML实现
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.etc.dao.EmployeeDao"><cache></cache><select id="selectAll" resultType="com.etc.bean.Employee" useCache="true">SELECT eid AS id,ename AS name,eaddr AS address FROM employee</select><!--创建resultMap column属性:数据库中的名称 property:实体类的名称--><resultMap id="resultMap1" type="com.etc.bean.Employee"><id column="eid" property="id"></id><result column="ename" property="name"></result><result column="eaddr" property="address"></result></resultMap><select id="selectAll2" resultMap="resultMap1">select * from employee</select><!-- 联表查询,使用resultMap--><resultMap id="resultMap2" type="com.etc.bean.Employee"><id column="eid" property="id"></id><result column="ename" property="name"></result><result column="eaddr" property="address"></result><result column="d_did" property="dept.id"></result><result column="d_dname" property="dept.name"></result></resultMap><select id="selectAll3" resultMap="resultMap2">select e.eid,e.ename,e.eaddr,d.did d_did,d.dname d_name from employee e,department d where e.did = d.did</select><!-- 联表查询,使用association--><resultMap id="resultMap3" type="com.etc.bean.Employee"><id column="eid" property="id"></id><result column="ename" property="name"></result><result column="eaddr" property="address"></result><association property="dept" javaType="com.etc.bean.Department"><result column="d_did" property="id"></result><result column="d_dname" property="name"></result></association></resultMap><select id="selectAll4" resultMap="resultMap3">select e.eid,e.ename,e.eaddr,d.did d_did,d.dname d_name from employee e,department d where e.did = d.did</select><!-- 分步查询--><resultMap id="resultMap4" type="com.etc.bean.Employee"><id column="eid" property="id"></id><result column="ename" property="name"></result><result column="eaddr" property="address"></result><!-- 想要完成分步查询 还要使用association 里面不用再次设定(前提是你的实体类和数据库表字段一一对应) --><association property="dept" select="com.etc.dao.DepartmentDao.findById" column="did"></association></resultMap><select id="selectStep" resultMap="resultMap4" >select * from employee where eid = #{id}</select></mapper>
1、分步查询 + 延迟加载
- 配置延迟加载,在config.xml中设置为延迟加载,注意配置顺序
- 使用延迟加载方法,先去查询简单的sql(最好单表,也可以关联查询),再去按需要加载关联查询的其它信息。
三、Test
public class EmployeeDaoTest {EmployeeDao employeeDao = null;@Beforepublic void common(){try {String resources = "configuration.xml";InputStream inputStream = Resources.getResourceAsStream(resources);SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);SqlSession sqlSession = sqlSessionFactory.openSession(true);employeeDao = sqlSession.getMapper(EmployeeDao.class);} catch (IOException e) {e.printStackTrace();}}@Testpublic void selectAll(){List<Employee> list = employeeDao.selectAll();for (Employee employee:list) {System.out.println(employee);}}@Testpublic void selectAll2(){List<Employee> list = employeeDao.selectAll2();for (Employee employee:list) {System.out.println(employee);}}@Testpublic void selectAll3(){List<Employee> list = employeeDao.selectAll3();for (Employee employee:list) {System.out.println(employee);System.out.println("\t"+employee.getDept().getId()+"\t"+employee.getDept().getName());}}@Testpublic void selectAll4(){List<Employee> list = employeeDao.selectAll4();for (Employee employee:list) {System.out.println(employee);System.out.println("\t"+employee.getDept().getId()+"\t"+employee.getDept().getName());}}@Testpublic void selectStep(){Employee employee = employeeDao.selectStep(10008);System.out.println("resultMap4");System.out.println(employee.getName());System.out.println(employee.getDept().getName());}
}