mybatis--常见的错误
1.没有在configuration.xml配置对应的sql配置文件
错误:
Error updating database. Cause: java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for ***Mapper.*** Cause: java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for ***Mapper.***
解决方法:
在configuration.xml配置文件中引用对应的sql配置文件
- <mappers>
- <mapper resource="esd/db/mapper/ResumeMapper.xml" />
- </mappers>
2.同一sql配置文件中id重复
错误:
Cause: org.apache.ibatis.builder.BuilderException: Error
parsing SQL Mapper Configuration. Cause: java.lang.RuntimeException:
Error parsing Mapper XML. Cause: java.lang.IllegalArgumentException:
Mapped Statements collection already
contains value for GeographyMapper.getByCode
两个sql语句的id重复了,就会报这个错.
- <!-- 按code码得到一个对象-->
- <select id="getByCode" resultType="Geography"
- parameterType="java.lang.String">
- select * from Geography where code = ${code}
- </select>
- <!-- 按地名getByName得到一个对象-->
- <select id="getByCode" resultType="Geography"
- parameterType="java.lang.String">
- select * from Geography where fullName = ${fullName}
- </select>
解决方法:
修改其中的一个id就ok了.
3.mapper配置文件中为bean类配置的属性没有在对应的bean类中找到对应的字段
错误:
Error updating database. Cause: org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'education' in 'class esd.bean.Personal'
Cause: org.apache.ibatis.reflection.ReflectionException: There is no
getter for property named 'education' in 'class esd.bean.Personal'
- <!-- 这个属性是多余的,在对应的bean类中没有对应的字段,删除后OK -->
- <if test="education != null and education != ''">
- education = #{education},
- </if>
解决方法:
在mapper配置文件中删除多配置的属性/或则在自己的bean类中补全该字段
4.<if>标签中不支持 && 符号
错误:
The entity name must immediately follow the '&' in the entity reference
解析不了 && 是什么东东
- <update id="update" parameterType="Geography">
- update Geography
- <trim prefix="set" suffixOverrides=",">
- <if test="code != null && code !=''">
- code=#{code},
- </if>
- <if test="name != null && name != ''">
- name=#{name},
- </if>
- </trim>
- where id= #{id}
- </update>
解决方法:使用正确的语法咯,别用&&,用and
- <!-- update -->
- <update id="update" parameterType="Geography">
- update Geography
- <trim prefix="set" suffixOverrides=",">
- <if test="code != null and code !=''">
- code=#{code},
- </if>
- <if test="name != null and name != ''">
- name=#{name},
- </if>
- </trim>
- where id= #{id}
- </update>
5.返回结果类型写错
由于我的项目配置原因, debug 输出信息中没有报错, 但是项目就是启动不起来, 所以没能截到错误信息提示, 以后遇到会补上
具体说就是: 如果自定义了返回的结果集, 返回的类型一定要是resultMap类型,(下面是错误示例) 如下
- <!-- 查询Area 地名 -->
- <resultMap id="ResultArea" type="Area">
- <id column="a_code" property="code" />
- <result column="a_name" property="name" />
- <result column="a_pyName" property="pyName" />
- <result column="a_abbr" property="abbr" />
- <result column="a_mark" property="mark" />
- </resultMap>
- <!-- 查询Parameter 地名 -->
- <resultMap id="ResultParameter" type="Parameter">
- <id column="p_id" property="id" />
- <result column="p_name" property="name" />
- <result column="p_value" property="value" />
- <result column="p_type" property="type" />
- <result column="p_mark" property="mark" />
- <association property="area" javaType="Parameter" resultMap="ResultArea" />
- </resultMap>
- <!-- get by id -->
- <select id="getById" resultType="ResultParameter" parameterType="int">
- select p.id as p_id, p.name as p_name, p.value as p_value, p.type as p_type, p.mark as p_mark,
- a.code as a_code, a.name as a_name, a.pyname as a_pyName, a.abbr as a_abbr, a.mark as a_mark
- from parameter as p, area as a
- where p.acode = a.code and id = #{id}
- </select>
常用在表连接查询中.
如上例中, 我定义了'ResultParameter'的结果集, 那么下面表连接查询的时候,如果返回的是表连接查询结果, 那么一定要使用
- <resultMap id="ResultMenu" type="com.esd.hesf.model.Menu">
- <id column="id" property="id" jdbcType="CHAR" />
- <result column="text" property="text" jdbcType="VARCHAR" />
- <result column="iconcls" property="iconcls" jdbcType="VARCHAR" />
- <association property="permissionType" javaType="com.esd.hesf.model.PermissionType" resultMap="ResultPermissionType"/>
- <result column="update_time" property="updateTime" jdbcType="TIMESTAMP" />
- </resultMap>
在<resultMap 处画红线, 原因是: 里面的<association />标签, 一定要放在这组标签最下面. 如下:
- <resultMap id="ResultMenu" type="com.esd.hesf.model.Menu">
- <id column="id" property="id" jdbcType="CHAR" />
- <result column="text" property="text" jdbcType="VARCHAR" />
- <result column="iconcls" property="iconcls" jdbcType="VARCHAR" />
- <result column="update_time" property="updateTime" jdbcType="TIMESTAMP" />
- <association property="permissionType" javaType="com.esd.hesf.model.PermissionType" resultMap="ResultPermissionType"/>
- </resultMap>
7.多写了if判断
错误:
org.apache.ibatis.reflection.ReflectionException:
There is no getter for property named 'year' in 'class java.lang.String'
下面是代码:
dao层方法
- /**
- * 查询审核报表情况--按公司类型查
- * @return
- */
- List<ReportViewModel> retrieveReportByCompanyType(String year);
mapper配置文件
- <!-- retrieve report statistics by company type -->
- <select id="retrieveReportByCompanyType" parameterType="java.lang.String" resultMap="ReportViewModel">
- select
- <include refid="report_column_list"/>
- from audit_company_view
- <where>
- <if test="year != null and year != ''">
- au_year = #{year,jdbcType=CHAR}
- </if>
- </where>
- group by t_id
- </select>
一般这个错是指参数parameterType类型的字段 没有get, set方法, 我只是传个String类型的参数, so 刚遇到这个问题的时候, 有点摸不着头脑, 怎么会报这个错呢? 原来问题出现在配置文件中:
- <if test="year != null and year != ''">
mybatis自动将if判断中的字段默认为传进来的parameterType类型的字段了... so....只要将这个if判断去掉即可.
- <!-- retrieve report statistics by company type -->
- <select id="retrieveReportByCompanyType" parameterType="java.lang.String" resultMap="ReportViewModel">
- select
- <include refid="report_column_list"/>
- from audit_company_view
- <where>
- au_year = #{year,jdbcType=CHAR}
- </where>
- group by t_id
- </select>
那问题出来了, 我怎么判断传进来的单个字符串是否为空值呢? 这是否是mybatis的一个小bug呢? 答案尚在查找中, 有了会第一时间贴上来的.
mybatis--常见的错误的更多相关文章
- Mybatis常见配置错误总结
Mybatis常见配置错误总结 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionF ...
- mybatis常见问题和错误
1. jdbc java type 映射关系 1) mysql的text 在mybatis中使用varchar类型 2. mybatis常见的错误 3.There is no getter for p ...
- mybatis学习笔记--常见的错误
原文来自:<mybatis学习笔记--常见的错误> 昨天刚学了下mybatis,用的是3.2.2的版本,在使用过程中遇到了些小问题,现总结如下,会不断更新. 1.没有在configurat ...
- 新手学习Python时常见的错误
最近学习Python,现在把一些常见的错误总结如下: 1)忘记在 if , elif , else , for , while , class ,def 声明末尾添加 :(导致 "Synta ...
- Android自动化压力测试之Monkey Test Android常见的错误类型及黑白名单的使用方法(四)
Android常见的错误类型有两种 1.ANR类型 1)在5秒内没有响应输入的事件(例如,按键按下,屏幕触摸) 2)BroadcastReceiver在10秒内没有执行完毕 2.Crash类型 1)异 ...
- php常见细节错误
PHP编程中10个最常见的错误 PHP是一种非常流行的开源服务器端脚本语言,你在万维网看到的大多数网站都是使用php开发的.本篇经将为大家介绍PHP开发中10个最常见的问题,希望能够对朋友有所帮助. ...
- TCP/IP 某些最常见的错误原因码 (errno)列表
对于在基于 UNIX 的环境中的 TCP/IP 用户,下表列出了某些最常见的错误原因码 (errno).它不是完整的错误列表.可以在文件 /usr/include/sys/errno.h 中找到 Er ...
- VC6.0常见编译错误提示
原文:http://c.biancheng.net/cpp/html/746.html 1) error C2001: newline in constant 编号:C2001 直译:在常量中出现了换 ...
- HTML5几种常见的错误写法
本文介绍了HTML5常见的6种错误写法,包括:1.不要使用section作为div的替代品 2.只在需要的时候使用header和hgroup 3.不要把所有列表式的链接放在nav里 4.figure元 ...
- 初学JSP+Servlet常见的错误
web编程中常见的错误: 一.404(要访问的资源没有找到) 1.web程序有没有部署(将项目到tomcat中) 2.url有没有写错(包括大小写,包括项目有没有重命名) 3.有没有将jsp/html ...
随机推荐
- nginx之fastcgi
fastcgi的应用程序就是一个while循环在,不停的accept,如果收到相应的服务请求则负责服务并将结果返回. 在fastcgi的进程环境中,标准输入与标准输出已经被重定向到了监听的socket ...
- HDU1114--Piggy-Bank(完全背包变形)
Piggy-Bank Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...
- VNC轻松连接远程Linux桌面
VNC连接Linux桌面,要想连接Linux远程桌面,按照下面的步骤,非常简单.快速,Linux配置VNC(以RedHat.CentOS.Fedora系列为例). 工具/原料 Linux平台安装VNC ...
- POJ2096 概率dp 入门
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=118282#problem/B 挺好的一个题目: 不过刚开始嘛,看别人题解长知识.这个人写 ...
- XML 从基础到精通
1.简介 XML(可扩展标记语言)语言是一种数据交换标准,用于存储数据:关键词是标记: XML具有以下优点: (1) 方便的穿过防火墙,在不同的操作系统之间通信,跨语言,跨平台.数据共享非常方便.(J ...
- 转: OGG Checkpoint 详解
1. OGG Checkpoint 详解 定位中断的位置,下次启动从中断的位置开始恢复. 1.target 端配置: 2.一条记录对应一个replicat 一. Extract Checkpoints ...
- 第2课 Linux操作系统简介
1. Linux操作系统的构成 (1)内核(kernel) ①操作系统的核心,负责管理系统的进程.内存.设备驱动程序.文件和网络系统. ②控制系统和硬件之间的相互通信. ③决定着系统的性能和稳定性. ...
- sql server 日期转换函数 convert()
--内容来自:http://hi.baidu.com/muqingz/item/8fb7b3ca8a485b0cac092f7b Select CONVERT(varchar(100), GETDAT ...
- zencart的modules下数据库操作templates排版和common首页引用
把这个学会,zencart的数据库操作,以及各种函数的调用基本会了 这个东西非常有用,你需要认真看一下,不要闲代码多. 如何在数据库中调出自己想要的产品,让它显示在首页. 据我本人不科学的理解,在in ...
- Chapter 1 First Sight——7
Eventually we made it to Charlie's. 最终我们到了查理斯的家. He still lived in the small,two-bedroom house that ...