Mybatis多表查询拓展

关于mybatis的多表查询,有时候关联其它表只需要取其中部分字段,其实可以直接在xml里面写SQL,在实体中声明对应字段去实现
 

首先是RepairPartsChangeExt 实体类

public class RepairPartsChangeExt extends RepairPartsChange {

    //来自其它表一些字段
    private String partsApplyCode;

    private String contractCode;

    private String jobCode;

    private String repairCode;

    private String repairPerson;

    //自行生成get,set方法

 来看下RepairPartsChangeExtMapper.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.yun.extmapper.rep.RepairPartsChangeExtMapper">
  <resultMap id="headerExtMap" type="com.yun.extmapper.rep.extentity.RepairPartsChangeExt">
    <id column="id" jdbcType="VARCHAR" property="id" />
    <result column="repair_code_id" jdbcType="VARCHAR" property="repairCodeId" />
    <result column="apply_date" jdbcType="DATE" property="applyDate" />
    <result column="parts_desc" jdbcType="VARCHAR" property="partsDesc" />
    <result column="picture_url" jdbcType="VARCHAR" property="pictureUrl" />
    <result column="need_quantity" jdbcType="DECIMAL" property="needQuantity" />
    <result column="skill_affirm" jdbcType="VARCHAR" property="skillAffirm" />
    <result column="parts_apply_status" jdbcType="VARCHAR" property="partsApplyStatus" />
    <result column="materiel_code" jdbcType="VARCHAR" property="materielCode" />
    <result column="to_materiel_code" jdbcType="VARCHAR" property="toMaterielCode" />
    <result column="create_by" jdbcType="VARCHAR" property="createBy" />
    <result column="create_date" jdbcType="TIMESTAMP" property="createDate" />
    <result column="update_by" jdbcType="VARCHAR" property="updateBy" />
    <result column="update_date" jdbcType="TIMESTAMP" property="updateDate" />
    <result column="delete_flag" jdbcType="CHAR" property="deleteFlag" />
    <result column="enabled" jdbcType="CHAR" property="enabled" />
    <result column="remark" jdbcType="VARCHAR" property="remark" />
    <result column="org_id" jdbcType="VARCHAR" property="orgId" />
    <result column="attribute1" jdbcType="VARCHAR" property="attribute1" />
    <result column="attribute2" jdbcType="VARCHAR" property="attribute2" />
    <result column="attribute3" jdbcType="VARCHAR" property="attribute3" />
    <result column="attribute4" jdbcType="VARCHAR" property="attribute4" />
    <result column="attribute5" jdbcType="VARCHAR" property="attribute5" />
    <!-- 实体中对应其它表的字段-->
    <result column="parts_apply_code" jdbcType="VARCHAR" property="partsApplyCode" />
    <result column="contract_code" jdbcType="VARCHAR" property="contractCode" />
    <result column="job_code" jdbcType="VARCHAR" property="jobCode" />
    <result column="repair_code" jdbcType="VARCHAR" property="repairCode" />
    <result column="repair_person" jdbcType="VARCHAR" property="repairPerson" />
  </resultMap>

  <select id="findRepairPartsChangeList"
          parameterType="com.yun.mapper.rep.entity.RepairPartsChange"
          resultMap="headerExtMap">
    select t1.*, t2.parts_apply_code, t2.contract_code, t2.job_code, t2.repair_code, t2.repair_person
    from repair_parts_change t1,repair_item t2
    where t1.repair_code_id = t2.id
      <if test="partsApplyCode !=null">
        and t2.parts_apply_code =#{partsApplyCode,jdbcType=VARCHAR}
      </if>
      <if test="contractCode !=null">
        and t2.contract_code like concat(concat('%',#{contractCode,jdbcType=VARCHAR}),'%')
      </if>
      <if test="jobCode !=null">
        and t2.job_code like concat(concat('%',#{jobCode,jdbcType=VARCHAR}),'%')
      </if>
      <if test="repairCode !=null">
        and t2.repair_code like concat(concat('%',#{repairCode,jdbcType=VARCHAR}),'%')
      </if>
      <if test="repairPerson !=null">
        and t2.repair_person like concat(concat('%',#{repairPerson,jdbcType=VARCHAR}),'%')
      </if>
      <if test="skillAffirm !=null">
        and t1.skill_affirm like concat(concat('%',#{skillAffirm,jdbcType=VARCHAR}),'%')
      </if>
      <if test="partsApplyStatus !=null">
        and t1.parts_apply_status = #{partsApplyStatus,jdbcType=VARCHAR}
      </if>
    ORDER BY t1.create_date desc
  </select>

</mapper>

最后在RepairPartsChangeExtMapper完成对xml中SQL的接口声明

@Repository
public interface RepairPartsChangeExtMapper {
    //对应xml中查询sql的id
    List<RepairPartsChangeExt> findRepairPartsChangeList(RepairPartsChange repairPartsChange);

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值