• MyBatis实操进阶版(一)


      MyBatis实操进阶版(一)

      目前而言,持久层框架中,在业务实现灵活性上,无可出MyBatis之右者.具体原因,后续将逐步展开

      ResultMap元素的设置

      配置文件中,ResultMap元素的作用临时存储sql执行结果集与Java对象之间的映射,用以实现数据的持久性.基于此,sql返回结果集更加多样/不必局限于表关系,同时接收对象更加灵活多变,可以自由根据数据需求,设置接收字段,搭配association和collection标签,可以灵活设置数据子集的配置,而且进一步降低了业务层与数据库间的交互次数,尤其是在处理复杂业务逻辑sql设置的时候,完成这一套配置后,唯一的感觉--爽.

      示例 :  业务逻辑省略,直接上代码

        Java对象设置: -- 包含集合

    public class CameraTrackInfo {
    
        private Integer personId;
        private String faviconUrl;
        private String name;
        private Long startTime;
        private Long endTime;
        private String cameraId;
        private List<CameraTrack> cameraTracks;
    
        ... ...
    
    }

        xml配置(resultMap & sql)

      resultMap设置,其中包含子元素集获取多参传输的问题,博客后半部分详解

    <resultMap id="rightListMap" type="com.rosetta.face.entity.CameraTrackInfo">
        <result column="person_id" property="personId" jdbcType="INTEGER" />
        <result column="favicon_url" property="faviconUrl" jdbcType="VARCHAR" />
        <result column="name" property="name" jdbcType="VARCHAR" />
        <result column="start_time" property="startTime" jdbcType="BIGINT" />
        <result column="end_time" property="endTime" jdbcType="BIGINT" />
        <result column="camera_id" property="cameraId" jdbcType="VARCHAR" />
        <collection property="cameraTracks" column="{startTime=start_time,endTime=end_time,cameraId=camera_id,personId=person_id}" select="selectTracksById" />
      </resultMap>

      子结果集获取

    <select id="selectTracksById" parameterType="java.util.Map" resultMap="BaseResultMap">
        SELECT c.id,c.camera_id,c.person_id,c.now_timestamp,c.url
        FROM camera_track c
        <where>
          <if test="personId != null">
            AND c.person_id = #{personId}
          </if>
          <if test="startTime != null and endTime != null">
            AND c.now_timestamp &gt;= #{startTime} AND c.now_timestamp &lt;= #{endTime}
          </if>
          <if test="cameraId != null">
            AND c.camera_id = #{cameraId}
          </if>
        </where>
        ORDER BY c.now_timestamp
      </select>

      结果集获取,此处包含参数传递的设置,下文详解

    <select id="queryBossCameraTracks" resultMap="rightListMap" parameterType="java.util.Map">
        SELECT DISTINCT c.person_id,p.favicon_url,p.name,#{params.startTime} AS start_time,#{params.endTime} AS end_time,#{params.area} AS camera_id
        FROM camera_track c
        LEFT OUTER JOIN person p ON c.person_id = p.person_id
        <where>
          <if test="params.personId != null">
            AND c.person_id = #{params.personId}
          </if>
          <if test="params.personName != null">
            AND p.name=#{params.personName}
          </if>
          <if test="params.startTime != null and params.endTime != null">
            AND c.now_timestamp &gt;= #{params.startTime} AND c.now_timestamp &lt;= #{params.endTime}
          </if>
          <if test="params.area != null">
            AND c.camera_id = #{params.area}
          </if>
        </where>
        ORDER BY c.now_timestamp
      </select>

      Mapper接口实现,同上,下文详解

    List<CameraTrackInfo> queryBossCameraTracks(@Param("params")Map<String,Object> map);

      概述,上述代码用以实现结果集中某字段为条件进一步获取子结果集,组合获取最终结果集的过程.

      MyBatis参数设置为Map

      实际开发中,不可能因为请求参数多个就随意新增实体类,Map的使用尤其关键,这里不同于表现层Map参数获取运行期报错,MyBatis支持Map传参.

      1) 使用 @Param注解,并设置唯一性key,故Map可以是多个,用key加以区分即可

      2) xml中,key.column的形式传递参数

      ResultMap子结果集获取多参传递

      子结果集的获取单个和多个出现的频率都很高,同时,有些参数并不在原有的结果集中就有,需要更上一级来传递,这时候就需要在select的返回结果中做文章,这就是前文代码的逻辑.

      1) select 'param' as column from dual . 自定义设置返回结果,设置结果中字段column的值为param

      2)<collection column="{aa=bb,cc=dd....}">,aa/cc为自定义key,并根据结果集字段<result>中的 column属性设置value(bb/dd).

      3)子结果集查询sql在接收参数时,无需设置key,直接引用字段即可.

  • 相关阅读:
    Codeforces Round #425 (Div. 2) Problem A Sasha and Sticks (Codeforces 832A)
    bzoj 2301 Problem b
    bzoj 1101 [POI2007]Zap
    bzoj 2005 能量采集
    bzoj 2527 Meteors
    bzoj 2724 [Violet 6]蒲公英
    回顾树状数组
    bzoj 3237 连通图
    bzoj 2733 永无乡
    Codeforces 817C Really Big Numbers
  • 原文地址:https://www.cnblogs.com/nyatom/p/9503793.html
Copyright © 2020-2023  润新知