百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 热门文章 > 正文

mybatis学习笔记——常见的错误(mybatis常见问题)

bigegpt 2024-08-01 11:48 5 浏览

昨天刚学了下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呢? 答案尚在查找中, 有了会第一时间贴上来的.

相关推荐

得物可观测平台架构升级:基于GreptimeDB的全新监控体系实践

一、摘要在前端可观测分析场景中,需要实时观测并处理多地、多环境的运行情况,以保障Web应用和移动端的可用性与性能。传统方案往往依赖代理Agent→消息队列→流计算引擎→OLAP存储...

warm-flow新春版:网关直连和流程图重构

本期主要解决了网关直连和流程图重构,可以自此之后可支持各种复杂的网关混合、多网关直连使用。-新增Ruoyi-Vue-Plus优秀开源集成案例更新日志[feat]导入、导出和保存等新增json格式支持...

扣子空间体验报告

在数字化时代,智能工具的应用正不断拓展到我们工作和生活的各个角落。从任务规划到项目执行,再到任务管理,作者深入探讨了这款工具在不同场景下的表现和潜力。通过具体的应用实例,文章展示了扣子空间如何帮助用户...

spider-flow:开源的可视化方式定义爬虫方案

spider-flow简介spider-flow是一个爬虫平台,以可视化推拽方式定义爬取流程,无需代码即可实现一个爬虫服务。spider-flow特性支持css选择器、正则提取支持JSON/XML格式...

solon-flow 你好世界!

solon-flow是一个基础级的流处理引擎(可用于业务规则、决策处理、计算编排、流程审批等......)。提供有“开放式”驱动定制支持,像jdbc有mysql或pgsql等驱动,可...

新一代开源爬虫平台:SpiderFlow

SpiderFlow:新一代爬虫平台,以图形化方式定义爬虫流程,不写代码即可完成爬虫。-精选真开源,释放新价值。概览Spider-Flow是一个开源的、面向所有用户的Web端爬虫构建平台,它使用Ja...

通过 SQL 训练机器学习模型的引擎

关注薪资待遇的同学应该知道,机器学习相关的岗位工资普遍偏高啊。同时随着各种通用机器学习框架的出现,机器学习的门槛也在逐渐降低,训练一个简单的机器学习模型变得不那么难。但是不得不承认对于一些数据相关的工...

鼠须管输入法rime for Mac

鼠须管输入法forMac是一款十分新颖的跨平台输入法软件,全名是中州韵输入法引擎,鼠须管输入法mac版不仅仅是一个输入法,而是一个输入法算法框架。Rime的基础架构十分精良,一套算法支持了拼音、...

Go语言 1.20 版本正式发布:新版详细介绍

Go1.20简介最新的Go版本1.20在Go1.19发布六个月后发布。它的大部分更改都在工具链、运行时和库的实现中。一如既往,该版本保持了Go1的兼容性承诺。我们期望几乎所...

iOS 10平台SpriteKit新特性之Tile Maps(上)

简介苹果公司在WWDC2016大会上向人们展示了一大批新的好东西。其中之一就是SpriteKitTileEditor。这款工具易于上手,而且看起来速度特别快。在本教程中,你将了解关于TileE...

程序员简历例句—范例Java、Python、C++模板

个人简介通用简介:有良好的代码风格,通过添加注释提高代码可读性,注重代码质量,研读过XXX,XXX等多个开源项目源码从而学习增强代码的健壮性与扩展性。具备良好的代码编程习惯及文档编写能力,参与多个高...

Telerik UI for iOS Q3 2015正式发布

近日,TelerikUIforiOS正式发布了Q32015。新版本新增对XCode7、Swift2.0和iOS9的支持,同时还新增了对数轴、不连续的日期时间轴等;改进TKDataPoin...

ios使用ijkplayer+nginx进行视频直播

上两节,我们讲到使用nginx和ngixn的rtmp模块搭建直播的服务器,接着我们讲解了在Android使用ijkplayer来作为我们的视频直播播放器,整个过程中,需要注意的就是ijlplayer编...

IOS技术分享|iOS快速生成开发文档(一)

前言对于开发人员而言,文档的作用不言而喻。文档不仅可以提高软件开发效率,还能便于以后的软件开发、使用和维护。本文主要讲述Objective-C快速生成开发文档工具appledoc。简介apple...

macOS下配置VS Code C++开发环境

本文介绍在苹果macOS操作系统下,配置VisualStudioCode的C/C++开发环境的过程,本环境使用Clang/LLVM编译器和调试器。一、前置条件本文默认前置条件是,您的开发设备已...