Mybatis:ReflectionException: There is no getter for property named 'productName' in 'class java.lang
来源:互联网 发布:周扬青 淘宝 编辑:程序博客网 时间:2024/05/16 13:57
在使用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
阅读全文
0 0
- Mybatis:ReflectionException: There is no getter for property named 'productName' in 'class java.lang
- org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'funcSetId' in 'class java.lang.Integer'
- 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.String'
- Mybatis-There is no getter for property named 'XXX' 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 'xxx' in 'class java.lang.String'
- Mybatis-There is no getter for property named 'id' 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.String'
- Mybatis中传参包There is no getter for property named 'XXX' 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 'xx' in 'class java.lang.String
- mybatis 异常:There is no getter for property named 'classId' in 'class java.lang.Integer'
- Mybatis中传参包There is no getter for property named 'XXX' 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 'tj' in 'class java.lang.String'
- mybatis报错 There is no getter for property named 'xx' in 'class java.lang.String
- 2016 Pacific Northwest Region Programming Contest—Division 2 Problem R — limit 1 second Equality
- Intel P6以来核心架构及对应型号、芯片组一览表
- Fragment replace的使用
- 【UOJ261 BZOJ 】天天爱跑步(线段树合并)
- leetcode -- 345. Reverse Vowels of a String 【双指针 + 逆序的变形】
- Mybatis:ReflectionException: There is no getter for property named 'productName' in 'class java.lang
- 搜索 棋盘问题
- 内存的几个小问题
- codeforces 821d Okabe and City
- 安装Python3-ipython
- NKOJ 3252 (CQOI 2015) 多项式(数学,高精度)
- 暑期集训之最大公约数问题
- jdbc 工具类的抽取
- 测试用例编写考虑的细节