mybatis中代码复用问题resultMap、sql、include标签

xml映射文件

对于重复出现的sql 片段可以使用sql标签提取出来,在使用的地方使用include标签引用即可
MyBatis之ResultMap标签

ResultMap标签基本作用:建立SQL查询结果字段与实体属性的映射关系信息

在深入ResultMap标签前,我们需要了解从SQL查询结果集到JavaBean或POJO实体的过程。

  1. 通过JDBC查询得到ResultSet对象

  2. 遍历ResultSet对象并将每行数据暂存到HashMap实例中,以结果集的字段名或字段别名为键,以字段值为值

  3. 根据ResultMap标签的type属性通过反射实例化领域模型

  4. 根据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" />
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值