mybatis参数使用的一次排错及总结

现象

Spring Boot + Mybatis调试接口时,出现如下问题:

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")

方案一:增加注解,其他不变

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(@Param("type") String type);
}

xml文件不变:

<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接口加注解:

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中使用内置参数 _paramter

<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”时,如仍使用上面的形式

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

会直接报错。

两个参数

方案一

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 = #{param1}
        and t1.WMS_BRANCH_BANK_NAME like '%${param2}%'
        order by t1.SID
    </select>

注意不能使用bankName、childBankName,须使用param1、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.5、JDK1.8及Mybatis3.4.5。

其他参考

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

转载于:https://my.oschina.net/u/3193075/blog/3066891

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值