在java

时间:2017-01-31 08:15:47

标签: java mybatis ibatis

我在mybatis中有一个现有查询,如下所示。

    <select id="getVessslScore" parameterType = "com.dpworld.mpc.camel.cxfrs.model.FilterCriteria" resultMap="vesselDetailResultMap">

    select SUM(msm.MSM_SCORE) Score 
    FROM mpc_score_master msm 
    WHERE msm.MSM_SCORE_TYPE='MPCSCR' AND
     (
     msm.IS_VALID='1' 
     )
    AND 
    (
    msm.MSM_CRIT_TYPE='VSLTYP' AND msm.MSM_CRIT_VAL= #{scoreAttrVslType} 
            OR 
     msm.MSM_CRIT_TYPE='SRVTYP' AND msm.MSM_CRIT_VAL = #{scoreAttrSrvType}
            OR 
     msm.MSM_CRIT_TYPE='SLATYP' AND msm.MSM_CRIT_VAL = #{scoreAttrSlaType}
            OR 
     msm.MSM_CRIT_TYPE='PRFTYP' AND msm.MSM_CRIT_VAL = #{scoreAttrPrfmType}
    )       
</select>

查询中使用的模型类(FilterCriteria)如下:

public class FilterCriteria implements Serializable {

  private static final long serialVersionUID = 5466202311142514198L;

  private String rotationNumbers;
  private String terminalId;
  private String vesselName;
  private String imoCode;
  private String pNam;
  private List<String> listRotations;
  private List<String> listTerminals;
  private List<String> vesselList ;
  private String vsrc; 
  private String veta; 
  private String scoreAttrVslType;
  private String scoreAttrSrvType;
  private String scoreAttrSlaType;
  private String scoreAttrPrfmType;
  private String scoreAttrCurrStatus;
  private String proforma;


  public String getTerminalId() {
    return terminalId;
  }

  public void setTerminalId(String terminalId) {
    this.terminalId = terminalId;
  }

  public String getRotationNumbers() {
    return rotationNumbers;
  }

  public void setRotationNumbers(String rotationNumbers) {
    this.rotationNumbers = rotationNumbers;
  }

  public String getVesselName() {
    return vesselName;
  }

  public void setVesselName(String vesselName) {
    this.vesselName = vesselName;
  }

  public String getImoCode() {
    return imoCode;
  }

  public void setImoCode(String imoCode) {
    this.imoCode = imoCode;
  }

  public String getpNam() {
    return pNam;
  }

  public void setpNam(String pNam) {
    this.pNam = pNam;
  }

  public List<String> getListRotations() {
    return listRotations;
  }

  public void setListRotations(List<String> listRotations) {
    this.listRotations = listRotations;
  }

  public List<String> getListTerminals() {
    return listTerminals;
  }

  public void setListTerminals(List<String> listTerminals) {
    this.listTerminals = listTerminals;
  }

  public List<String> getVesselList() {
    return vesselList;
  }

  public void setVesselList(List<String> vesselList) {
    this.vesselList = vesselList;
  }

  public String getVsrc() {
    return vsrc;
  }

  public void setVsrc(String vsrc) {
    this.vsrc = vsrc;
  }

  public String getVeta() {
    return veta;
  }

  public void setVeta(String veta) {
    this.veta = veta;
  }


  public String getScoreAttrVslType() {
    return scoreAttrVslType;
  }

  public void setScoreAttrVslType(String scoreAttrVslType) {
    this.scoreAttrVslType = scoreAttrVslType;
  }

  public String getScoreAttrSrvType() {
    return scoreAttrSrvType;
  }

  public void setScoreAttrSrvType(String scoreAttrSrvType) {
    this.scoreAttrSrvType = scoreAttrSrvType;
  }

  public String getScoreAttrSlaType() {
    return scoreAttrSlaType;
  }

  public void setScoreAttrSlaType(String scoreAttrSlaType) {
    this.scoreAttrSlaType = scoreAttrSlaType;
  }

  public String getScoreAttrPrfmType() {
    return scoreAttrPrfmType;
  }

  public void setScoreAttrPrfmType(String scoreAttrPrfmType) {
    this.scoreAttrPrfmType = scoreAttrPrfmType;
  }


  public String getScoreAttrCurrStatus() {
    return scoreAttrCurrStatus;
  }

  public void setScoreAttrCurrStatus(String scoreAttrCurrStatus) {
    this.scoreAttrCurrStatus = scoreAttrCurrStatus;
  }

  public String getProforma() {
    return proforma;
  }

  public void setProforma(String proforma) {
    this.proforma = proforma;
  }

}

目前其工作单过滤条件对象。现在我需要更改查询以接受过滤条件对象列表。

我正在尝试使用以下查询,但它不起作用。请提供一些指示来解决问题。

          <select id="getVessslScoreQuery" parameterClass = "java.util.List" >

          select SUM(msm.MSM_SCORE) Score 
          FROM mpc_score_master msm 
          WHERE msm.MSM_SCORE_TYPE='MPCSCR' AND
          (
          msm.IS_VALID='1' 
           )
          AND 
          ( msm.MSM_CRIT_TYPE, msm.MSM_CRIT_VAL ) in 
    <iterate open="(" close=")" conjunction="," >
            ( 'VSLTYP' , #[].scoreAttrVslType# )
             ,
              ( 'SRVTYP' , #[].scoreAttrSrvType# )
              ,
               ( 'SLATYP' , #[].scoreAttrSlaType# )
               ,
                ( 'PRFTYP' , #[].scoreAttrPrfmType# )
    </iterate>         
   </select>

1 个答案:

答案 0 :(得分:0)

您使用的语法,例如<iterate>标记适用于Ibatis,我认为它不适用于MyBatis

MyBatis3的参考文档位于下一个网站:

否则,Ibatis参考文档:

如果您使用的是MyBatis,则需要使用<foreach>标记,请使用下一个内容:

select SUM(msm.MSM_SCORE) Score 
FROM 
mpc_score_master msm 
WHERE 
msm.MSM_SCORE_TYPE='MPCSCR' 
AND
msm.IS_VALID='1' 
AND 
(
   (msm.MSM_CRIT_TYPE='VSLTYP' and msm.MSM_CRIT_VAL in
   <foreach item="item"  collection="list" open="(" separator="and" close=")">
      #{item.scoreAttrVslType} 
   </foreach>)
   OR
   (msm.MSM_CRIT_TYPE='SRVTYP' and msm.MSM_CRIT_VAL in
   <foreach item="item"  collection="list" open="(" separator="and" close=")">
      #{item.scoreAttrSrvType} 
   </foreach>)
   OR
   (msm.MSM_CRIT_TYPE='SLATYP' and msm.MSM_CRIT_VAL in
   <foreach item="item"  collection="list" open="(" separator="and" close=")">
      #{item.scoreAttrSlaType} 
   </foreach>)
   OR
   (msm.MSM_CRIT_TYPE='PRFTYP' and msm.MSM_CRIT_VAL in
   <foreach item="item"  collection="list" open="(" separator="and" close=")">
      #{item.scoreAttrPrfmType} 
   </foreach>)
 )

使用它,简单地说你正在使用下面的东西:

  ....
  (msm.MSM_CRIT_TYPE='SLATYP' and msm.MSM_CRIT_VAL in('one_value', 'other_value'))
  or
  (msm.MSM_CRIT_TYPE='PRFTYP' and msm.MSM_CRIT_VAL in('one_value', 'other_value'))

其他选项:使用Java API

否则,如果你能做到的话。您可以使用Java Api

 @SelectProvider(type = FilterProvider.class, method = "filterCriteria Sql")
 public VessslScore getVessslScore(List<FilterCriteria> criterias);

然后在你的类FilterProvider中,只需创建sql。