Mybatis:ReflectionException: There is no getter for property named 'productName' in 'class java.lang

在使用Mybatis的时候,报如下错误:

org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'productName' in 'class java.lang.String'

对应的mapper.xml以及dao如下:

<?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.test.dao.IpListDao">
    <resultMap id="ipResultMap" type="com.test.po.IpList">
        <!-- 将pojo的属性与数据库表字段对应 -->
        <id property="id" column="id"/>
        <result property="productName" column="productName"/>
        <result property="ips" column="ips"/>
        <result property="remark" column="remark"/>
    </resultMap>
    <!-- 创建sql子句方便使用 -->
    <sql id="table">
        TB_IP_LIST
    </sql>
    <sql id="from">
        from
        <include refid="table"/>
    </sql>
    <sql id="selectTable">
        select *
        <include refid="from"/>
    </sql>
    <select id="getIpList" resultMap="ipResultMap" parameterType="java.lang.String">
        <include refid="selectTable"/>
        <where>
            <if test="productName!=null">productName=#{productName}</if>
        </where>
    </select>
</mapper>

IpListDao中方法:

    public IpList getIpList(String productName){
        return sqlSessionTemplate.selectOne(classStr+".getIpListByProductName",productName);
    }

原因是在<select><if>判断中因为参数类型为java.lang.String,Mybatis默认采用ONGL解析参数,所以会自动采用对象树的形式取string.num值,引起报错,可修改如下:
1. 使用_parameter仅仅将mapper.xml文件部分修改如下(测试可行):

    <select id="getIpList" resultMap="ipResultMap" parameterType="java.lang.String">
        <include refid="selectTable"/>
        <where>
            <if test="_parameter!=null">productName=#{productName}</if>
        </where>
    </select>

2.或者现在对应的dao方法中修改,设置名字(这种方法我试了还是一样的错误,不知道为什么。。。)

    public IpList getIpList(@Param("productName") String productName){
        return sqlSessionTemplate.selectOne(classStr+".getIpList",productName);
    }
    <select id="getIpList" resultMap="ipResultMap" parameterType="java.lang.String">
        <include refid="selectTable"/>
        <where>
            <if test="productName!=null">productName=#{productName}</if>
        </where>
    </select>

上面这种方法还是报同样的错,有待研究

总结:问过同事,mapper.xml里面的参数最好是map类型
方案如下:
在dao中将参数设置为Map类型,如图所示

    /**
     * 参数最好是Map类型,以对应mapper.xml中参数
     * */
    public IpList getIpList(Map<String,Object> productMap){
        return sqlSessionTemplate.selectOne(classStr+".getIpList",productMap);
    }

假设表中的字段名叫 "productName",那么mapper.xml如下编写(不需要使用_parameter了):

    <select id="getIpList" resultMap="ipResultMap" parameterType="map">
        <include refid="selectTable"/>
        <where>
            <if test="productName!=null">productName=#{productName}</if>
        </where>
    </select>

在调用dao中方法传入map参数的时候,一定要往map中放入key为"productName"的数据,如下调用:

        Map<String,Object> map=new HashMap<>();
        map.put("productName",productName); //key为“productName”
        IpList ipList=ipListDao.getIpList(map);

参考链接:
1. http://blog.csdn.net/u014476019/article/details/45878771
2. http://woshixy.blog.51cto.com/5637578/1180914

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zlp1992

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值