mybatis 關聯子查詢 association

<resultMap id="ProjectApprovalResultMap" type="com.hhwy.fm.projectapproval.domain.ProjectApproval" >
   <id column="id" property="id" jdbcType="VARCHAR" />
   <result column="prj_code" property="prjCode" jdbcType="VARCHAR" />
   <result column="prj_status" property="prjStatus" jdbcType="VARCHAR" />
</resultMap>

<resultMap id="testPositionResultMap" extends="ProjectApprovalResultMap" type="com.hhwy.fm.projectapproval.domain.ProjectApproval">
   <association property="prjStatusName" column="prj_status" select="projectapproval.sql.selectPrjStatusName" />
   <association property="prjName" column="prj_code" select="projectapproval.sql.selectPrjName" />
</resultMap>

<select id="testPositionSelect" resultMap="testPositionResultMap" parameterType="string">
   select
      a.id,
      a.prj_code,
      a.prj_status
   from
      t_project_approval a
   WHERE a.id = #{id}
</select>

<select id="selectPrjStatusName" resultType="string" parameterType="string">
   select
      c.item_name
   FROM
      t_sys_dictitem c
   WHERE
      c.dict_code = 'prj_status'
      and c.item_code = #{prjStatus}
</select>

<select id="selectPrjName" resultType="string" parameterType="string">
   select
      b.prj_name
   from
      t_letter_trial b
   WHERE b.prj_code = #{prjCode}
</select>
package com.hhwy.fm.projectapproval.domain;

import java.util.UUID;
import javax.persistence.*;
import com.hhwy.framework.annotation.PropertyDesc;
import com.hhwy.framework.persistent.entity.Domain;

/**
 * <br>描 述:項目立項(項目表)
 * <br>創 建 人:Jinzhaoqiang
 * <br>建立時間:
 * <br>修改備註:無
 * <br>版本:1.0.0
 */
@Entity(name="ProjectApproval")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@Table(name="t_project_approval")
public class ProjectApproval extends Domain{

   private static final long serialVersionUID = 7119079399916219284L;

   @Id
   @Column(
         name = "id",
         length = 32
   )
   private String id = UUID.randomUUID().toString().replace("-", "");
   
    @Column(name="prj_code", columnDefinition="varchar(15) comment '項目編號'")
    private String prjCode;
    
   @Column(name="prj_status", columnDefinition="varchar(2) comment '狀態:1待儲備、2儲備中、3已儲備、4形式內審中、5待函審、6項目函審中、7待立項、8終止中、9已終止、10立項中、11已立項'")
   private String prjStatus;

    
    @PropertyDesc("項目狀態名稱")
    @Transient
    private String prjStatusName;
    
    @PropertyDesc("工程名稱")
    @Transient
    private String prjName;

   @Override
   public String getId() {
      return id;
   }

   @Override
   public void setId(String id) {
      this.id = id;
   }

   public String getPrjCode() {
      return prjCode;
   }

   public void setPrjCode(String prjCode) {
      this.prjCode = prjCode;
   }

   public String getPrjStatus() {
      return prjStatus;
   }

   public void setPrjStatus(String prjStatus) {
      this.prjStatus = prjStatus;
   }

   public String getPrjStatusName() {
      return prjStatusName;
   }

   public void setPrjStatusName(String prjStatusName) {
      this.prjStatusName = prjStatusName;
   }

   public String getPrjName() {
      return prjName;
   }

   public void setPrjName(String prjName) {
      this.prjName = prjName;
   }
}
ProjectApproval projectApproval = (ProjectApproval)dao.getOneBySQL("projectapproval.sql.testPositionSelect",id);

多個參數

<resultMap id="ResultMap" type="com.hhwy.fm.projecttaskstatement.domain.ProjectTaskStatementResult" >
   <id column="id" property="id" jdbcType="VARCHAR" />
   <result column="create_time" property="createTime" jdbcType="TIMESTAMP" />
   <result column="create_user" property="createUser" jdbcType="VARCHAR" />
   <result column="update_time" property="updateTime" jdbcType="TIMESTAMP" />
   <result column="update_user" property="updateUser" jdbcType="VARCHAR" />
   <result column="age" property="age" jdbcType="INTEGER" />
   <result column="child_classification" property="childClassification" jdbcType="CHAR" />
   <result column="company" property="company" jdbcType="VARCHAR" />
   <result column="del_flag" property="delFlag" jdbcType="CHAR" />
   <result column="name" property="name" jdbcType="VARCHAR" />
   <result column="project_task_statement_id" property="projectTaskStatementId" jdbcType="VARCHAR" />
   <result column="result_classification" property="resultClassification" jdbcType="CHAR" />
   <result column="prj_division" property="prjDivision" jdbcType="LONGVARCHAR" />
   <association property="childClassificationName" column="{resultClassification=result_classification,childClassification=child_classification}" select="projecttaskstatement.sql.selectChildClassificationName" />
</resultMap>
<sql id="Result_Column_List" >
   tptsr.id, tptsr.child_classification,
       tptsr.`name`, tptsr.project_task_statement_id,
       tptsr.result_classification, tptsr.number,
       tptsr.comments, tptsr.unit
</sql>
<!-- 預期成果 -->
<select id="getResultList" resultMap="ResultMap" parameterType="java.util.Map">
   select <include refid="Result_Column_List" />, tsd.item_name  resultClassificationName
   from t_project_task_statement_result tptsr
   LEFT JOIN t_sys_dictitem tsd ON tptsr.result_classification = tsd.item_code AND tsd.dict_code = 'result_classification'
   where tptsr.del_flag = 1
   <if test="projectTaskStatementId != null and  projectTaskStatementId != ''">
      and tptsr.project_task_statement_id = #{projectTaskStatementId}
   </if>
</select>

<select id="selectChildClassificationName" resultType="string" parameterType="java.util.Map">
   select
      c.item_name
   FROM
      t_sys_dictitem c
   WHERE
      c.item_code = #{childClassification}
   <if test="resultClassification == 1">
      and c.dict_code = 'patent'
   </if>
   <if test="resultClassification == 4">
      and c.dict_code = 'working_method'
   </if>
   <if test="resultClassification == 5">
      and c.dict_code = 'standard'
   </if>
   <if test="resultClassification == 6">
      and c.dict_code = 'thesis'
   </if>
   <if test="resultClassification == 7">
      and c.dict_code = 'fingerpost'
   </if>
   <if test="resultClassification == 9">
      and c.dict_code = 'other'
   </if>
</select>
相關文章
相關標籤/搜索