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配置文件
- <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 处画红线, 原因是: 里面的<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呢? 答案尚在查找中, 有了会第一时间贴上来的.
转载于:https://www.cnblogs.com/ios9/p/7659483.html
mybatis学习笔记--常见的错误相关推荐
- mybatis学习笔记-04-常见错误排查
4.错误排查 1.标签问题 mapper.xml中的namespace,中间是用小圆点隔开的 resource绑定 mapper,需要使用路径!(使用/隔开) 3.程序配置文件必须符合要求 4.Nul ...
- Mybatis学习笔记(二) 之实现数据库的增删改查
开发环境搭建 mybatis 的开发环境搭建,选择: eclipse j2ee 版本,mysql 5.1 ,jdk 1.7,mybatis3.2.0.jar包.这些软件工具均可以到各自的官方网站上下载 ...
- MyBatis多参数传递之混合方式——MyBatis学习笔记之十五
在本系列文章的<MyBatis多参数传递之Map方式示例>一文中,网友mashiguang提问如下的方法如何传递参数:public List findStudents(Map condit ...
- MyBatis学习笔记2 ——第一个MyBatis程序
MyBatis学习笔记2 --第一个MyBatis程序 参考教程B站狂神https://www.bilibili.com/video/BV1NE411Q7Nx 环境搭建 建立一个mybatis数据库用 ...
- 超详细Mybatis学习笔记(可供下载)
文章目录 1.简介 2.第一个Mybatis程序 搭建环境 编写代码 测试 3.CRUD(增删改查) 3.1.几个属性 3.2.select 3.3.insert 3.4.delete 3.5.upd ...
- mybatis学习笔记(12)-多对多查询
mybatis学习笔记12-多对多查询 示例 多对多查询总结 resultMap总结 本文实现多对多查询,查询用户及用户购买商品信息. 示例 查询主表是:用户表 关联表:由于用户和商品没有直接关联,通 ...
- Mybatis学习笔记(二)【框架基础搭建】
Mybatis框架基础搭建 一.数据库搭建 二.创建一个maven工程 三.在pom.xml中导入依赖 四.创建一个mybatis的核心配置文件 配置连接数据库的字段值文件(如果使用方式一就不需要配置 ...
- MyBatis学习笔记(二)根据配置文件优化
上一篇:MyBatis学习笔记(一)完整查询数据库流程+增删改查 mybatis_config.xml文件中可以包含以下部分 本文接下来的优化都是针对于mybatis_config.xml文件,并且每 ...
- mybatis学习笔记(13)-延迟加载
2019独角兽企业重金招聘Python工程师标准>>> mybatis学习笔记(13)-延迟加载 标签: mybatis [TOC] resultMap可以实现高级映射(使用asso ...
最新文章
- 推荐一个非常好用的Chrome扩展应用,用于美化Json字符串
- nginx并发模型与traffic_server并发模型简单比较
- stanford coursera 机器学习编程作业 exercise 3(逻辑回归实现多分类问题)
- 图像聚类与检索的简单实现(一)
- javascript动态创建可拖动、最大化、最小化的层
- Java继承注意事项难点理解
- zabbix监控进程的CPU和内存占用量
- 我的编程学习日志(9)--交换A,B值得方法(相加,异或,swap函数)
- php有的图片显示不出来,图片显示不出来,但是数据库里有显示
- 国外支付(Paypal,Cybersource)
- 哪种款式的耳机不伤耳朵,五款不伤害耳朵听力的骨传导耳机推荐
- windows-sys17:windows10修改系统语言为中文
- 使用FTPClient封装FtpUtil
- 中南大学计算机系可以蹭课吗,在中南、师大上湖大的课,985/211就是会玩!
- maven 3.6.3 下载与详细配置图文教程(基于win10系统)
- 樊银亭 夏敏捷 编著第27本著作 数据可视化原理及应用
- 酒店预订app的前景优势
- [Windows] IDM下载(已激活)
- 使用云服务器创建网站(完整开发过程)
- PHP sg11加密解密 sg解密
热门文章
- shell脚本和常用命令
- 地宫取宝|2014年蓝桥杯B组题解析第九题-fishers
- Spring Boot log4j多环境日志级别的控制
- karaf中利用Bundle引入外部log4j配置文件
- 前端工程师都会喜欢的5个JavaScript库
- ios 6.1中 Release问题
- Ajax ControlToolkit - AnimationExtender Action语法(笔记)
- Django 数据库建表的时候 No migrations to apply原因出现和解决
- 数仓数据分层(ODS DWD DWS ADS)
- 记一次“Shiro+任务调度”开发过程中出现UnavailableSecurityManagerException解决思路