您的位置:首页 > 编程语言 > Java开发

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

2017-07-21 20:32 721 查看
在使用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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  mybatis
相关文章推荐