xml映射文件
对于重复出现的sql 片段可以使用sql标签提取出来,在使用的地方使用include标签引用即可
MyBatis之ResultMap标签
ResultMap标签基本作用:建立SQL查询结果字段与实体属性的映射关系信息
在深入ResultMap标签前,我们需要了解从SQL查询结果集到JavaBean或POJO实体的过程。
通过JDBC查询得到ResultSet对象
遍历ResultSet对象并将每行数据暂存到HashMap实例中,以结果集的字段名或字段别名为键,以字段值为值
根据ResultMap标签的type属性通过反射实例化领域模型
根据ResultMap标签的type属性和id、result等标签信息将HashMap中的键值对,填充到领域模型实例中并返回
具体用法如下:
<resultMap id="BaseResultMap" type="com.uddtrip.base.worksheet.model.Settings">
<id column="id" property="id" jdbcType="INTEGER" />
<result column="is_open_delete" property="isOpenDelete" jdbcType="BIT" />
<result column="delete_cycle" property="deleteCycle" jdbcType="INTEGER" />
<result column="add_user_id" property="addUserId" jdbcType="VARCHAR" />
<result column="add_user_name" property="addUserName" jdbcType="VARCHAR" />
<result column="add_time" property="addTime" jdbcType="BIGINT" />
<result column="update_user_id" property="updateUserId" jdbcType="VARCHAR" />
<result column="update_user_name" property="updateUserName" jdbcType="VARCHAR" />
<result column="update_time" property="updateTime" jdbcType="BIGINT" />
</resultMap>
<sql id="Base_Column_List">
id, is_open_delete, delete_cycle, add_user_id, add_user_name, add_time, update_user_id,update_user_name, update_time
</sql>
<select id="selectSetting" resultMap="BaseResultMap">
SELECT
<include refid="Base_Column_List" />
FROM ORDER BY update_time DESC,add_time DESC LIMIT 1
</select>
resultMap 标签说明:
主标签
id:该resultMap的标志
type:返回值的类名,此例中返回Settings类
子标签:
id:用于设置主键字段与领域模型属性的映射关系,此处主键为ID,对应id。
result:用于设置普通字段与领域模型属性的映射关系
sql include 标签说明:
会自动把id=”Base_Column_List”的sql标签内的代码贴过来
对于多个xml文件需要同时引用一段相同的 可以在某个xml 中定义这个 sql 代码片段,在需要引用的地方使用全称引用即可
具体用法如下:
RecordAttachment.xml:
<mapper namespace="com.uddtrip.base.worksheet.dao.RecordAttachmentDao">
<resultMap id="BaseResultMap"
type="com.uddtrip.base.worksheet.model.RecordAttachment">
<id column="a_id" property="id" jdbcType="BIGINT" />
<result column="op_id" property="opId" jdbcType="BIGINT" />
<result column="name" property="name" jdbcType="VARCHAR" />
<result column="url" property="url" jdbcType="VARCHAR" />
</resultMap>
<sql id="Base_Column_List">
tbws_record_attach.id as a_id, tbws_record_attach.op_id,
tbws_record_attach.name, tbws_record_attach.url
</sql>
Record.xml
<mapper namespace="com.uddtrip.base.worksheet.dao.RecordDao">
<resultMap id="BaseResultMap" type="com.uddtrip.base.worksheet.model.Record">
<id column="r_id" property="id" jdbcType="BIGINT" />
<result column="ws_id" property="wsId" jdbcType="BIGINT" />
<result column="operate_type" property="operateType" jdbcType="INTEGER" />
<result column="operator_id" property="operatorId" jdbcType="VARCHAR" />
<result column="operator_name" property="operatorName"
jdbcType="VARCHAR" />
<result column="group_id" property="groupId" jdbcType="VARCHAR" />
<result column="group_name" property="groupName" jdbcType="VARCHAR" />
<result column="add_time" property="addTime" jdbcType="BIGINT" />
<result column="explain" property="explain" jdbcType="VARCHAR" />
</resultMap>
<!-- 操作日志+附件集合封装类 -->
<resultMap id="DtoBaseResultMap" type="com.uddtrip.base.worksheet.dto.RecordDto"
extends="BaseResultMap">
<collection property="recordAttachments"
ofType="com.uddtrip.base.worksheet.model.RecordAttachment"
resultMap="com.uddtrip.base.worksheet.dao.RecordAttachmentDao.BaseResultMap"></collection>
</resultMap>
<sql id="Base_Column_List">
tbws_record.id as r_id, tbws_record.ws_id,
tbws_record.operate_type, tbws_record.operator_id,
tbws_record.operator_name, tbws_record.group_id,
tbws_record.group_name, tbws_record.add_time,
tbws_record.explain
</sql>
<select id="selectByWsId" resultMap="DtoBaseResultMap"
parameterType="java.lang.Long">
SELECT
<include refid="Base_Column_List" />
,
<include
refid="com.uddtrip.base.worksheet.dao.RecordAttachmentDao.Base_Column_List" />
FROM tbws_record
LEFT JOIN tbws_record_attach ON tbws_record.id =
tbws_record_attach.op_id
WHERE tbws_record.ws_id =
#{wsId,jdbcType=BIGINT}
</select>
在Record.xml中引用RecordAttachment.xml中id=”Base_Column_List”的sql片段:
<include refid="com.uddtrip.base.worksheet.dao.RecordAttachmentDao.Base_Column_List" />