1.从SQL查询结果到领域模型实体
我们需要了解从SQL查询结果集到JavaBean或某个实体的过程
- 通过JDBC查询得到ResultSet对象
- 遍历ResultSet对象并将每行数据暂存到HashMap实例中,以结果集的字段名或字段别名为键,以字段值为值
- 根据ResultMap标签的type属性通过反射实例化领域模型
- 根据ResultMap标签的type属性和id、result等标签信息将HashMap中的键值对,填充到领域模型实例中并返回
2.ResultMap标签
- 属性说明
1.1id
属性 ,resultMap标签的标识,dao层接口方法名。
1.2type
属性 ,返回值的全限定类名,或类型别名。
1.3autoMapping
属性 ,值范围true(默认值)|false, 设置是否启动自动映射功能,自动映射功能就是自动查找与字段名小写同名的属性名,并调用setter方法。而设置为false后,则需要在resultMap
内明确注明映射关系才会调用对应的setter方法。
<resultMap id="gradeAndClasses" type="com.aaa.mb.entity.Grade" autoMapping="true">
</resultMap>
- 子元素说明:
id
元素 ,用于设置主键字段与领域模型属性的映射关系
result
元素 ,用于设置普通字段与领域模型属性的映射关系
<!--一对多 一个年级对应多个班级 高级映射 collection-->
<resultMap id="gradeAndClasses" type="com.aaa.mb.entity.Grade" >
<id column="gradeid" property="gradeId" jdbcType="INTEGER" javaType="java.lang.Integer"></id>
<result column="gradename" property="gradeName"></result>
<result column="addtime" property="addTime"></result>
<!-- private List<ClassInfo> classInfoList; -->
<collection property="classInfoList" ofType="com.aaa.mb.entity.ClassInfo">
<id column="classid" property="classId"></id>
<result column="classnum" property="classNum"></result>
<result column="classname" property="className"></result>
<result column="begintime" property="beginTime" ></result>
<result column="endtime" property="endTime" ></result>
</collection>
</resultMap>
<!-- 多对一 多个员工对应一个部门 用association -->
<select id="getEmpsAndDept" resultMap="empsAndDept">
select e.*,d.deptno as dno,d.dname,d.loc from emp e join dept d on e.deptno=d.deptno
</select>
<resultMap id="empsAndDept" type="Emp">
<id column="empno" property="empNo"></id>
<result column="ename" property="ename"></result>
<result column="job" property="job"></result>
<result column="sal" property="salary"></result>
<result column="comm" property="comm"></result>
<result column="hiredate" property="hireDate"></result>
<association property="dept" javaType="dept">
<id column="dno" property="deptNo"></id>
<result column="dname" property="dname"></result>
<result column="loc" property="loc"></result>
</association>
</resultMap>
3.简单点说resultMap它可以将查询到的复杂数据(比如查询到几个表中数据)映射到一个结果集当中。
<!--column不做限制,可以为任意表的字段,而property须为type 定义的pojo属性-->
<resultMap id="唯一的标识" type="映射的pojo对象">
<id column="表的主键字段,或者可以为查询语句中的别名字段" jdbcType="字段类型" property="映射pojo对象的主键属性" />
<result column="表的一个字段(可以为任意表的一个字段)" jdbcType="字段类型" property="映射到pojo对象的一个属性(须为type定义的pojo对象中的一个属性)"/>
<association property="pojo的一个对象属性" javaType="pojo关联的pojo对象">
<id column="关联pojo对象对应表的主键字段" jdbcType="字段类型" property="关联pojo对象的主席属性"/>
<result column="任意表的字段" jdbcType="字段类型" property="关联pojo对象的属性"/>
</association>
<!-- 集合中的property须为oftype定义的pojo对象的属性-->
<collection property="pojo的集合属性" ofType="集合中的pojo对象">
<id column="集合中pojo对象对应的表的主键字段" jdbcType="字段类型" property="集合中pojo对象的主键属性" />
<result column="可以为任意表的字段" jdbcType="字段类型" property="集合中的pojo对象的属性" />
</collection>
</resultMap>
如果collection标签是使用嵌套查询,格式如下:
<collection column="传递给嵌套查询语句的字段参数" property="pojo对象中集合属性" ofType="集合属性中的pojo对象" select="嵌套的查询语句" >
</collection>