Mybatis:ReflectionException: There is no getter for property named 'productName' in 'class java.lang
2017-07-21 20:32
721 查看
在使用Mybatis的时候,报如下错误:
对应的mapper.xml以及dao如下:
原因是在
1. 使用
2.或者现在对应的dao方法中修改,设置名字(这种方法我试了还是一样的错误,不知道为什么。。。)
上面这种方法还是报同样的错,有待研究
总结:问过同事,mapper.xml里面的参数最好是
方案如下:
在dao中将参数设置为
假设表中的字段名叫
在调用dao中方法传入map参数的时候,一定要往map中放入key为
参考链接:
1. http://blog.csdn.net/u014476019/article/details/45878771
2. http://woshixy.blog.51cto.com/5637578/1180914
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报There is no getter for property named 'name' in 'class java.lang.String'错误
- Mybatis问题-----There is no getter for property named 'productCode' in 'class java.lang.Strin g'
- mybatis查询报错There is no getter for property named 'xx' in 'class java.lang.**
- Mybatis 报错 There is no getter for property named '***' in 'class java.lang.String'
- Mybatis问题:There is no getter for property named 'VALUE' in 'class java.lang.String'
- There is no getter for property named 'name' in 'class java.lang.Integer'
- mybatis中出现There is no getter for property named 'depIds' in 'class java.lang.String'的错误
- mybatis:There is no getter for property named 'xxx' in 'class java.lang.String'
- Mybatis问题:There is no getter for property named 'Id' in 'class java.lang.String'
- There is no getter for property named 'fieldName' in 'class java.lang.String'
- Mybatis 对单独传入参数string进行判断时报There is no getter for property named '**' in 'class java.lang.String解决办法
- mybatis使用动态sql时报错:There is no getter for property named '*' in 'class java.lang.String
- mybatis报错 There is no getter for property named 'xx' in 'class java.lang.String
- Mybatis There is no getter for property named 'XXX' in 'class java.lang.XXX
- mybatis There is no getter for property named 'xx' in 'class java.lang.String
- mybatis出现There is no getter for property named 'Id' in 'class java.lang.Intege
- There is no getter for property named 'username' in 'class java.lang.String错误!
- Mybatis异常There is no getter for property named 'XXX' in 'class java.lang.String'
- Mybatis找不到参数错误:There is no getter for property named 'categoryId' in 'class java.lang.Integer'。
- mybatis加Spring项目: 解决There is no getter for property named '***' in 'class java.lang.String'问题