mybatis參數使用的一次排錯及總結

現象

Spring Boot + Mybatis調試接口時,出現以下問題:html

org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'type' in 'class java.lang.String'

曬代碼

mapper接口代碼

package com.xxx.xxx.dao;

import com.xxx.xxx.core.Mapper;
import com.xxx.xxx.model.RmsLabel;
import org.apache.ibatis.annotations.Param;

import java.util.List;

public interface RmsLabelMapper extends Mapper<RmsLabel> {

    // 根據類型獲取標籤
    List<RmsLabel> selectLabelByType(String type);
}

xml配置SQL

<sql id="selectLabelByTypeField">${alias}.LABEL_ID, ${alias}.LABEL_NAME</sql>
    <select id="selectLabelByType" parameterType="java.lang.String" resultMap="ResultMap">
      select
      <include refid="selectLabelByTypeField">
        <property name="alias" value="t1"></property>
      </include>
      from RMS_LABEL t1
      where
      <if test="type != null and type neq 00">
        t1.LABEL_TYP = '${type}'
      </if>
      <if test="type != null and type eq 00">
        t1.LABEL_TYP BETWEEN '01' AND '04'
      </if>

      order by t1.ORDER_VAL
    </select>

解決方案及總結

一個參數

須要在mapper方法中加入註解@Param("type")java

方案一:增長註解,其餘不變

mapper接口:spring

package com.xxx.xxx.dao;

import com.xxx.xxx.core.Mapper;
import com.xxx.xxx.model.RmsLabel;
import org.apache.ibatis.annotations.Param;

import java.util.List;

public interface RmsLabelMapper extends Mapper<RmsLabel> {

    // 根據類型獲取標籤
    List<RmsLabel> selectLabelByType(@Param("type") String type);
}

xml文件不變:sql

<sql id="selectLabelByTypeField">${alias}.LABEL_ID, ${alias}.LABEL_NAME</sql>
    <select id="selectLabelByType" parameterType="java.lang.String" resultMap="ResultMap">
      select
      <include refid="selectLabelByTypeField">
        <property name="alias" value="t1"></property>
      </include>
      from RMS_LABEL t1
      where
      <if test="type != null and type neq 00">
        t1.LABEL_TYP = '${type}'
      </if>
      <if test="type != null and type eq 00">
        t1.LABEL_TYP BETWEEN '01' AND '04'
      </if>

      order by t1.ORDER_VAL
    </select>

方案二:使用內置參數 _parameter

mapper接口加註解:apache

package com.xxx.xxx.dao;

import com.xxx.xxx.core.Mapper;
import com.xxx.xxx.model.RmsLabel;
import org.apache.ibatis.annotations.Param;

import java.util.List;

public interface RmsLabelMapper extends Mapper<RmsLabel> {

    // 根據類型獲取標籤
    List<RmsLabel> selectLabelByType(@Param("type") String type);
}

xml中使用內置參數 _paramtermybatis

<sql id="selectLabelByTypeField">${alias}.LABEL_ID, ${alias}.LABEL_NAME</sql>
    <select id="selectLabelByType" parameterType="java.lang.String" resultMap="ResultMap">
      select
      <include refid="selectLabelByTypeField">
        <property name="alias" value="t1"></property>
      </include>
      from RMS_LABEL t1
      where
      <if test="_parameter != null">
        t1.LABEL_TYP = '${type}'
      </if>
      order by t1.ORDER_VAL
    </select>

注意:if標籤裏面的內容和上文已經不同,此處使用 「_parameter」時,如仍使用上面的形式app

<if test="_parameter != null and _parameter neq 00">

會直接報錯。spring-boot

兩個參數

方案一

mapper接口:調試

package com.xxx.xxx.dao;

import com.xxx.xxx.core.Mapper;
import com.xxx.xxx.model.WmsBranchBank;

import java.util.List;

public interface WmsBranchBankMapper extends Mapper<WmsBranchBank> {


    List<WmsBranchBank> selectBranchByKey(String bankName, String childBankName);
}

xml文件:code

<sql id="selectBranchByKeyField">${alias}.WMS_BRANCH_BANK_NAME,${alias}.UNIONPAY_NUMBER</sql>
    <select id="selectBranchByKey" resultMap="ResultMap">
        select
        <include refid="selectBranchByKeyField">
          <property name="alias" value="t1"></property>
        </include>
        from WMS_BRANCH_BANK t1
        where t1.CATEGORY_NUMBER = #{param1}
        and t1.WMS_BRANCH_BANK_NAME like '%${param2}%'
        order by t1.SID
    </select>

注意不能使用bankName、childBankName,須使用param一、param2。

方案二

mapper接口:

package com.xxx.xxx.dao;

import com.xxx.xxx.core.Mapper;
import com.xxx.xxx.model.WmsBranchBank;

import java.util.List;

public interface WmsBranchBankMapper extends Mapper<WmsBranchBank> {


    List<WmsBranchBank> selectBranchByKey(String bankName, String childBankName);
}

xml文件:

<sql id="selectBranchByKeyField">${alias}.WMS_BRANCH_BANK_NAME,${alias}.UNIONPAY_NUMBER</sql>
    <select id="selectBranchByKey" resultMap="ResultMap">
        select
        <include refid="selectBranchByKeyField">
          <property name="alias" value="t1"></property>
        </include>
        from WMS_BRANCH_BANK t1
        where t1.CATEGORY_NUMBER = #{arg0}
        and t1.WMS_BRANCH_BANK_NAME like '%${arg1}%'
        order by t1.SID
    </select>

注意不能使用bankName、childBankName,須使用arg0、arg1。

多個參數

使用對象

mapper接口:

// 查詢個人掛牌數據產品列表
    List<DataProductListDTO> selectDataProdMineList(DataProductMineListVO vo);

xml文件:

<sql id="selectDataProdMineListField"> ${alias}.DATA_ID,${alias}.NAME,${alias}.SH_IMG,${alias}.TYP PRODUCT_TYP,${alias}.INDUSTRY,${alias}.RANGE,${alias}.OWNERSHIP,${alias}.TRADING,${alias}.LABELS,${alias}.LINK_MAN,${alias}.LINK_PHONE,${alias}.PRICE,${alias}.HAS_SPEC,${alias}.SPEC,${alias}.WDESC,${alias}.PUB_TIME,${alias}.COMMENT_SCORE,${alias}.SALED_NUM,${alias}.VIEW_NUM,${alias}.FAVORITY_NUM,${alias}.WSTATE,${alias}.MBR_ID,${alias}.DEFAULT_WI  </sql>
    <select id="selectDataProdMineList" parameterType="com.xxx.xxx.model.DataProductMineListVO" resultType="com.xxx.xxx.model.DataProductListDTO" useCache="false">
        select
        <include refid="selectDataProdMineListField"><property name="alias" value="t1"/></include>
        from PSM_DATA_BASE t1
        where
        t1.NAME like '%${name}%'
        <if test="type != null and type neq 0">
            AND t1.TYP = '${type}'
        </if>
        <if test="state != null and state neq 0">
            AND t1.WSTATE = '${state}'
        </if>
            AND t1.MBR_ID = '${mbrId}'
        order by
            t1.PUB_TIME desc
    </select>

實體類VO:

package com.xxx.xxx.model;

import lombok.Data;

/**
 * @Author:
 * @Date:
 * @Description:
 */
@Data
public class DataProductMineListVO {

    private Integer page = 0;

    private Integer size = 0;

    // 關鍵字
    private String name;

    // 類型
    private String type;

    // 狀態
    private String state;

    // 會員ID
    private String mbrId;
}

使用環境說明

Spring Boot + Mybatis版本

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.13.RELEASE</version>
    </parent>
	
	<properties>
        <java.version>1.8</java.version>
    </properties>
	
	<dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.3.1</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.5</version>
        </dependency>
        <dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper</artifactId>
            <version>3.4.2</version>
        </dependency>

Spring Boot1.五、JDK1.8及Mybatis3.4.5。

其餘參考

https://www.cnblogs.com/straybirds/p/9085414.html

相關文章
相關標籤/搜索