mybatis学习笔记--常见的错误
原文来自:《mybatis学习笔记--常见的错误》
昨天刚学了下mybatis,用的是3.2.2的版本,在使用过程中遇到了些小问题,现总结如下,会不断更新.
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配置文件
plain copy
- <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="ResultParameter", 否则万一提示信息中没有显示出来, 要费些时间才能找出来.
6. 标签顺序写错
错误:
The content of element type "resultMap" must match "(constructor?,id*,result*,association*,collection*,discriminator?)".
下午的时候无意中遇到这个错误, 下面是错误代码:
- <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 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);
- <!-- 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>
- <if test="year != null and year != ''">
- <!-- 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 的开发环境搭建,选择: eclipse j2ee 版本,mysql 5.1 ,jdk 1.7,mybatis3.2.0.jar包.这些软件工具均可以到各自的官方网站上下载 ...
- MyBatis:学习笔记(1)——基础知识
MyBatis:学习笔记(1)--基础知识 引入MyBatis JDBC编程的问题及解决设想 ☐ 数据库连接使用时创建,不使用时就释放,频繁开启和关闭,造成数据库资源浪费,影响数据库性能. ☐ 使用数 ...
- mybatis学习笔记(五) -- maven+spring+mybatis从零开始搭建整合详细过程(附demo和搭建过程遇到的问题解决方法)
文章介绍结构一览 一.使用maven创建web项目 1.新建maven项目 2.修改jre版本 3.修改Project Facts,生成WebContent文件夾 4.将WebContent下的两个文 ...
- mybatis学习笔记(五):mybatis 逆向工程
mybatis学习笔记(五):mybatis 逆向工程 在日常开发中,如果数据库中存在多张表,自己手动创建 多个pojo 类和编写 SQL 语法配置文件,未免太过繁琐,mybatis 也提供了一键式生 ...
- mybatis 学习笔记(二):mybatis SQL注入问题
mybatis 学习笔记(二):mybatis SQL注入问题 SQL 注入攻击 首先了解下概念,什么叫SQL 注入: SQL注入攻击,简称SQL攻击或注入攻击,是发生于应用程序之数据库层的安全漏洞. ...
- 【MyBatis学习笔记】
[MyBatis学习笔记]系列之预备篇一:ant的下载与安装 [MyBatis学习笔记]系列之预备篇二:ant入门示例 [MyBatis学习笔记]系列之一:MyBatis入门示例 [MyBatis学习 ...
- MyBatis:学习笔记(3)——关联查询
MyBatis:学习笔记(3)--关联查询 关联查询 理解联结 SQL最强大的功能之一在于我们可以在数据查询的执行中可以使用联结,来将多个表中的数据作为整体进行筛选. 模拟一个简单的在线商品购物系统, ...
- mybatis学习笔记(四)-- 为实体类定义别名两种方法(基于xml映射)
下面示例在mybatis学习笔记(二)-- 使用mybatisUtil工具类体验基于xml和注解实现 Demo的基础上进行优化 以新增一个用户为例子,原UserMapper.xml配置如下: < ...
- mybatis学习笔记(二)-- 使用mybatisUtil工具类体验基于xml和注解实现
项目结构 基础入门可参考:mybatis学习笔记(一)-- 简单入门(附测试Demo详细过程) 开始体验 1.新建项目,新建类MybatisUtil.java,路径:src/util/Mybatis ...
随机推荐
- 第一节,TensorFlow基本用法
一 TensorFlow安装 TensorFlow是谷歌基于DistBelief进行研发的第二代人工智能学习系统,其命名来源于本身的运行原理.Tsnsor(张量)意味着N维数组,Flow(流)意味着基 ...
- Cotex-M4简介
ARM Cortex™-M4 处理器是由 ARM 专门开发的最新嵌入式处理器,用以满足需要有效且易于使用的控制和信号处理功能混合的数字信号控制市场. 高效的信号处理功能与 Cortex-M 处理器系列 ...
- python---一个简单的socket
server端: 1 创建socket对象.调用socket构造函数.如: socket = socket.socket( family, type ) #family参数代表地址家族,可为 ...
- 设置MyBatis在控制台打印SQL语句
在调试阶段,打印SQL会极大方便开发者.MyBatis有提供配置,只需要在MyBatis的配置文件mybatis-config.xml中<configuration>节点下,添加如下配置: ...
- Ant基础知识1
1.Ant简介 Apache Ant是一个将软件编译/测试/部署等步骤联系在一起加以优化的一个构建工具,常用于java环境中的软件开发.Ant的默认配置文件是build.xml. 对java语言的支持 ...
- 虚拟机下Linux(终端)配置网络的方法
这几天在虚拟机vmware上部署centos系统,想通过内部联网用yum命令安装必需的软件,但是一直不能静态地址联网,今天终于找到一个方法centos内部设置IP,对外联网.设置过程如下: .首先是网 ...
- Java Web 自定义标签
1. 自定义标签 由于在JSP页面中直接嵌入Java代码会导致页面开起来非常混乱,不方便和美工等配合工作,为此,JSP提供了自定义标签技术,可以代替直接嵌入Java代码的方式提供动态逻辑,但自定义 ...
- Bleve代码阅读(二)——Index Mapping
引言 Bleve是Golang实现的一个全文检索库,类似Lucene之于Java.在这里通过阅读其代码,来学习如何使用及定制检索功能.也是为了通过阅读代码,学习在具体环境下Golang的一些使用方式. ...
- IO流总结笔记一
IO流继承关系图 IO概述 IO流是用来处理设备上数据的输入输出. 具体设备有:硬盘,内存,键盘录入等等. IO流的具体分类: 1,根据处理的数据类型不同分为:字节流和字符流,字节流读取的最小单位 ...
- html 替换元素
参考博客:http://www.cnblogs.com/wkylin/archive/2011/05/12/2044328.html 可替换元素也是行内元素 替换元素是浏览器根据其标签的元素与属性来判 ...