mybatis中子查询对象及对象集合子查询中带多参数

12次阅读

共计 3634 个字符,预计需要花费 10 分钟才能阅读完成。

就国内来说项目中 mybatis 使用的较多,因为方便灵活,上手快,会写 sql 就能用好 mybatis,另外 sql 优化等简单易做,遇到慢 sql 了比 hibernate 更好排查。除了一大痛点,因为项目短平快,所以开发过程中大概率会出现发现某个表要加字段要删字段要改字段等头疼的事,你以为我会说,这么麻烦不想调么,不,不存在的。
言归正传,在查询里使用子查询是比较频繁的。这文章的下半部分就是要解决子查询一个对象,一个对象列表的问题,并且子查询中的条件除了关联字段还有其他的参数需要传入。
本示例中要查询的表结构如下:

create table if not exists tb_station(
    id bigint not null constraint pk_tb_station primary key,
    code varchar(18),
    geo_id bigint,  // 分组 id
    label_id bigint, // 标牌 id
    employee_id bigint, // 员工 id
    status integer
);

需要获取的结果对象为
public class StationVO {
    private Long id;// 主键
    private String code;// 工位编号
    private Long stationGeoId;//StationGeoVO,所属工位分区对象
    private Long labelId;// 工位绑定的标牌 id
    private String labelCode;// 标牌 code
    private String eslCode;// 标牌 eslcode
    private EmployeeVO employee;//EmployeeVO,分配的员工对象
    private String status;// 工位状态(0:空闲,1:正在使用)private List<Biz> bizList;// 工位分组推送 vo
}
biz 对象说明:biz 对象对应的表里有一个 geo_id 字段进行关联

此处的需求是要根据分组 id(即 geoId)查询 stationVO 对象

mybatis 中对应的映射及代码如下:

  <resultMap id="StationVOResultMap" type="org.sit.cto.smmrepository.vo.StationVO">
    <id column="id" jdbcType="BIGINT" property="id" />
    <result column="code" jdbcType="VARCHAR" property="code" />
    <result column="status" jdbcType="INTEGER" property="status" />
    <result column="label_id" jdbcType="BIGINT" property="labelId" />
    <result column="esl_code1" jdbcType="VARCHAR" property="eslCode" />
    <result column="lcode" jdbcType="VARCHAR" property="labelCode" />
    <result column="geo_id" jdbcType="BIGINT" property="stationGeoId" />
    <association property="employee" column="employee_id" select="selectEmployeeByEmployeeId"/>
    <collection property="bizList" column="{geoId=geo_id,eslCode=esl_code1}" ofType="map" select="selectBizVOByStationId"/>
  </resultMap>
  
    <resultMap id="EmployeeResultMap" type="org.sit.cto.smmrepository.model.Employee">
    <id column="id" jdbcType="BIGINT" property="id" />
    <result column="code" jdbcType="VARCHAR" property="code" />
    <result column="real_name" jdbcType="VARCHAR" property="realName" />
    <result column="company" jdbcType="VARCHAR" property="company" />
    <result column="dept_name" jdbcType="VARCHAR" property="deptName" />
    <result column="position" jdbcType="VARCHAR" property="position" />
    <result column="phone" jdbcType="VARCHAR" property="phone" />
    <result column="status" jdbcType="INTEGER" property="status" />
  </resultMap>
  
    <resultMap id="BizVOResultMap" type="org.sit.cto.smmrepository.vo.BizVO">
    <result column="esl_code" jdbcType="VARCHAR" property="eslCode" />
    <result column="temp_id" jdbcType="BIGINT" property="labelTempId" />
    <result column="key" jdbcType="VARCHAR" property="key" />
    <result column="value" jdbcType="VARCHAR" property="value" />
  </resultMap>
  
    <select id="selectEmployeeByEmployeeId" resultMap="EmployeeResultMap">
    select
    id, 
    code, 
    real_name, 
    company, 
    dept_name, 
    position, 
    phone, 
    status
    from tb_employee
    where id=#{employeeId}
  </select>
  
   <select id="selectBizVOByStationId" parameterType="java.util.Map" resultMap="BizVOResultMap">
    select
     esl_code, 
     temp_id, 
     key, 
     value
     from tb_biz
     where associated_id=#{geoId}
     and esl_code=#{eslCode}
     and biz_type=2
  </select>
  

    <select id="getStationVOListByGeoId" resultMap="StationVOResultMap">
    SELECT
        ts.ID,
        ts.code,
        ts.geo_id,
        ts.label_id,
        ts.employee_id,
        ts.status,
        tl.code lcode,
        tl.esl_code1
        FROM
        tb_station ts
        LEFT JOIN tb_label tl ON ts.label_id = tl.ID
        WHERE
        ts.geo_id =#{geoId}
    ORDER BY
    ts.code
  </select>

子查询获取一个关联对象
例子中的 employee

  <association property="employee" column="employee_id" select="selectEmployeeByEmployeeId"/>
子查询对象,标签使用  <association  />
property 的内容对应目标对象中的属性名 
column 对应表中的关联字段的名字
select 对应获取关联对象子查询的查询名

子查询获取一个关联对象的集合
示例中 Biz 对象的集合

<collection property="bizList" column="{geoId=geo_id,eslCode=esl_code1}" ofType="map" select="selectBizVOByStationId"/>
子查询,标签使用 <collection />
property 的内容对应目标对象中的属性名
column 对应表中的关联字段的名字
select 对应获取对象子查询的查询名
ofType 此处子查询需要传入其他参数,故用 map 传入,若无其他参数则该标签中不需要此属性,column 处直接填写关联字段名(如 column="geo_id")
               需要传入其他参数时,写法如上例,在对应的子查询的 #{}占位符处需要与该 map 中的 key 对应

规范定义好 resultmap,能让 sql 更有条理更美观

正文完
 0