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

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

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

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

相关推荐

LangChain4j如何自定义文档转换器实现数据清洗?

LangChain4j提供了3种RAG(Retrieval-AugmentedGeneration,检索增强生成)实现,我们通常在原生或高级的RAG实现中,要对数据进行清洗,也就是将外接...

Java 8 Stream API 详解(java stream.)

Java8StreamAPI详解一、概述在Java8中,StreamAPI是一个重要的新特性。它为处理集合(如List、Set等)中的元素提供了一种高效且富有表现力的方式。Str...

Java修炼终极指南:185 使用 Stream 过滤嵌套集合

这是面试中的一个经典问题,通常从一个模型开始,如下所示(我们假设集合是一个List):publicclassAuthor{privatefinalStringname;pri...

java8的stream使用小示例(java stream())

据JetBrains发布的2021年开发者生态系统调查,Java8在java使用的版本中仍然是当前最流行的版本。72%的专业开发人员使用Java8作为其在java开发中主要编程语言版本。现...

Node.js Stream - 实战篇(node.js in action)

本文转自“美团点评技术团队”http://tech.meituan.com/stream-in-action.html背景前面两篇(基础篇和进阶篇)主要介绍流的基本用法和原理,本篇从应用的角度,介...

Java Stream:集合处理的api(java 集合操作)

JavaStream流:高效集合处理的函数式编程利器一、什么是JavaStream?Java8引入的StreamAPI是一套用于处理集合数据的流式编程接口,通过函数式风格(无副作用的...

去除 List 中的重复元素,你知道几种实现方法?

去除List中重复元素,这在实际编程或面试中经常遇到,每个人都有习惯的写法吧,这里抛砖引玉,汇总了一些实现方案,开拓思路。准备数据假设数组中有10个数据,可能有重复,需要将重复的数据从数组中去掉。pu...

Java开发者必看!Stream流式编程10个爆款技巧,让你代码优雅飞起

为什么你的Java代码总像拧巴的麻绳?掌握这10个Stream实战技巧,代码效率与优雅度将产生质的飞跃。以下案例均来自真实电商系统场景,带你感受流式编程的降维打击!一、过滤与映射组合拳(Filter...

leetcode每日一题之存在重复元素(存在重复元素 iii)

题:给定一个整数数组,判断是否存在重复元素。如果存在一值在数组中出现至少两次,函数返回true。如果数组中每个元素都不相同,则返回false。比如:输入:[1,2,3,1]输出:true...

告别for循环!揭秘Stream API如何让你的代码简洁度提升300%

一、当传统循环遇上现代需求真实场景复现:某电商平台需要处理10万条订单数据,要求:筛选出金额>500的订单提取用户ID并去重统计VIP用户数量传统实现方案://常规写法Set<Long...

Java中List去重的N种方法:从基础到优雅

Java中List去重的N种方法:从基础到优雅在日常的Java开发中,我们经常会遇到需要对List集合去重的情况。无论是为了清理重复的数据,还是为了优化算法性能,掌握多种去重方式都是一项非常实用的技能...

Java Stream流没用过?常用高频方法

概念Stream流是Java8添加的以一种链式调用的方法处理数据,主要侧重于计算。具有以下相关特点代码简洁链式调用Stream常用方法1.将数组变为当作List操作String[]strArr=...

核医学专业名词索引(M-R)(核医学重点归纳)

M吗啡(morphia)埋藏式心律转复除颤器(implantablecardioverterdefibrillator,ICD)麦角骨化醇(VD2,calciferol)脉冲堆积(pulsepi...

CodeMeter 新版发布(codesigner下载)

威步于2022年8月4日发布CodeMeter7.50及CodeMeter软件保护套装11.10,以下为新版内容。CodeMeterRuntime7.50StreamingSIMDExten...

世界上最小的五轴铣床Pocket NC(最小的五轴加工中心)

PocketNC,由MIT学生研制,还有说法是这款产品的设计者是来自美国蒙大拿州的一对极客夫妻。目前主要有两款产品:PocketNCV2-50,9000美元;PocketNCV2-10,60...