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配置文件

[html] view plain copy
  1. <mappers>  
  2.     <mapper resource="esd/db/mapper/ResumeMapper.xml" />  
  3. </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重复了,就会报这个错.
[html] view plain copy
  1.        <!-- 按code码得到一个对象-->  
  2. <select id="getByCode" resultType="Geography"  
  3.     parameterType="java.lang.String">  
  4.     select * from Geography where code = ${code}  
  5. </select>  
  6.   
  7. <!-- 按地名getByName得到一个对象-->  
  8. <select id="getByCode" resultType="Geography"  
  9.     parameterType="java.lang.String">  
  10.     select * from Geography where fullName = ${fullName}  
  11. </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'

[html] view plain copy
  1. <!-- 这个属性是多余的,在对应的bean类中没有对应的字段,删除后OK -->  
  2.             <if test="education != null and education != ''">  
  3.                 education = #{education},  
  4.             </if>  

解决方法:

在mapper配置文件中删除多配置的属性/或则在自己的bean类中补全该字段

4.<if>标签中不支持 && 符号

错误:

The entity name must immediately follow the '&' in the entity reference

解析不了 && 是什么东东

[html] view plain copy
  1. <update id="update" parameterType="Geography">  
  2.         update Geography  
  3.         <trim prefix="set" suffixOverrides=",">  
  4.             <if test="code != null && code !=''">  
  5.                 code=#{code},  
  6.             </if>  
  7.             <if test="name != null && name != ''">  
  8.                 name=#{name},  
  9.             </if>  
  10.         </trim>  
  11.         where id= #{id}  
  12.     </update>  

解决方法:使用正确的语法咯,别用&&,用and

[html] view plain copy
  1. <!-- update -->  
  2.     <update id="update" parameterType="Geography">  
  3.         update Geography  
  4.         <trim prefix="set" suffixOverrides=",">  
  5.             <if test="code != null and code !=''">  
  6.                 code=#{code},  
  7.             </if>  
  8.             <if test="name != null and name != ''">  
  9.                 name=#{name},  
  10.             </if>  
  11.         </trim>  
  12.         where id= #{id}  
  13.     </update>  

5.返回结果类型写错

由于我的项目配置原因, debug 输出信息中没有报错, 但是项目就是启动不起来, 所以没能截到错误信息提示, 以后遇到会补上

具体说就是: 如果自定义了返回的结果集, 返回的类型一定要是resultMap类型,(下面是错误示例) 如下

[html] view plain copy
  1. <!-- 查询Area 地名 -->  
  2.     <resultMap id="ResultArea" type="Area">  
  3.         <id column="a_code" property="code" />  
  4.         <result column="a_name" property="name" />  
  5.         <result column="a_pyName" property="pyName" />  
  6.         <result column="a_abbr" property="abbr" />  
  7.         <result column="a_mark" property="mark" />  
  8.     </resultMap>  
  9.       
  10.     <!-- 查询Parameter 地名 -->  
  11.     <resultMap id="ResultParameter" type="Parameter">  
  12.         <id column="p_id" property="id" />  
  13.         <result column="p_name" property="name" />  
  14.         <result column="p_value" property="value" />  
  15.         <result column="p_type" property="type" />  
  16.         <result column="p_mark" property="mark" />  
  17.         <association property="area" javaType="Parameter" resultMap="ResultArea" />  
  18.     </resultMap>  
  19.   
  20.     <!-- get by id -->  
  21.     <select id="getById" resultType="ResultParameter" parameterType="int">  
  22.         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,  
  23.                 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  
  24.         from parameter as p, area as a   
  25.         where p.acode = a.code and id = #{id}  
  26.     </select>  
常用在表连接查询中.

如上例中, 我定义了'ResultParameter'的结果集,  那么下面表连接查询的时候,如果返回的是表连接查询结果, 那么一定要使用 resultMap="ResultParameter", 否则万一提示信息中没有显示出来, 要费些时间才能找出来.

6. 标签顺序写错

错误:

The content of element type "resultMap" must match "(constructor?,id*,result*,association*,collection*,discriminator?)".

下午的时候无意中遇到这个错误, 下面是错误代码:

[html] view plain copy
  1. <resultMap id="ResultMenu" type="com.esd.hesf.model.Menu">  
  2.     <id column="id" property="id" jdbcType="CHAR" />  
  3.     <result column="text" property="text" jdbcType="VARCHAR" />  
  4.     <result column="iconcls" property="iconcls" jdbcType="VARCHAR" />  
  5.     <association property="permissionType" javaType="com.esd.hesf.model.PermissionType" resultMap="ResultPermissionType"/>  
  6.     <result column="update_time" property="updateTime" jdbcType="TIMESTAMP" />  
  7. </resultMap>  
在<resultMap 处画红线,   原因是: 里面的<association />标签, 一定要放在这组标签最下面.  如下:

[html] view plain copy
  1. <resultMap id="ResultMenu" type="com.esd.hesf.model.Menu">  
  2.     <id column="id" property="id" jdbcType="CHAR" />  
  3.     <result column="text" property="text" jdbcType="VARCHAR" />  
  4.     <result column="iconcls" property="iconcls" jdbcType="VARCHAR" />  
  5.     <result column="update_time" property="updateTime" jdbcType="TIMESTAMP" />  
  6.     <association property="permissionType" javaType="com.esd.hesf.model.PermissionType" resultMap="ResultPermissionType"/>  
  7. </resultMap>  

7.多写了if判断

错误:

org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'year' in 'class java.lang.String'

下面是代码:

dao层方法

[java] view plain copy
  1. /** 
  2.      * 查询审核报表情况--按公司类型查 
  3.      * @return 
  4.      */  
  5.     List<ReportViewModel> retrieveReportByCompanyType(String year);  
mapper配置文件
[html] view plain copy
  1. <!-- retrieve report statistics by company type -->  
  2.     <select id="retrieveReportByCompanyType" parameterType="java.lang.String" resultMap="ReportViewModel">  
  3.         select   
  4.             <include refid="report_column_list"/>  
  5.         from audit_company_view   
  6.         <where>  
  7.             <if test="year != null and year != ''">  
  8.                 au_year = #{year,jdbcType=CHAR}   
  9.             </if>  
  10.         </where>  
  11.         group by t_id  
  12.     </select>  
一般这个错是指参数parameterType类型的字段 没有get, set方法, 我只是传个String类型的参数, so 刚遇到这个问题的时候, 有点摸不着头脑, 怎么会报这个错呢? 原来问题出现在配置文件中:

[html] view plain copy
  1. <if test="year != null and year != ''">  
mybatis自动将if判断中的字段默认为传进来的parameterType类型的字段了... so....只要将这个if判断去掉即可.

[html] view plain copy
  1. <!-- retrieve report statistics by company type -->  
  2. <select id="retrieveReportByCompanyType" parameterType="java.lang.String" resultMap="ReportViewModel">  
  3.     select   
  4.         <include refid="report_column_list"/>  
  5.     from audit_company_view   
  6.     <where>  
  7.             au_year = #{year,jdbcType=CHAR}   
  8.     </where>  
  9.     group by t_id  
  10. </select>  

那问题出来了, 我怎么判断传进来的单个字符串是否为空值呢? 这是否是mybatis的一个小bug呢?  答案尚在查找中, 有了会第一时间贴上来的.

  1. <mappers> 
  2.     <mapper resource="esd/db/mapper/ResumeMapper.xml" /> 
  3. </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重复了,就会报这个错.
[html] view plain copy
  1.        <!-- 按code码得到一个对象--> 
  2. <select id="getByCode" resultType="Geography" 
  3.     parameterType="java.lang.String"> 
  4.     select * from Geography where code = ${code} 
  5. </select> 
  6.  
  7. <!-- 按地名getByName得到一个对象--> 
  8. <select id="getByCode" resultType="Geography" 
  9.     parameterType="java.lang.String"> 
  10.     select * from Geography where fullName = ${fullName} 
  11. </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'

[html] view plain copy
  1. <!-- 这个属性是多余的,在对应的bean类中没有对应的字段,删除后OK --> 
  2.             <if test="education != null and education != ''"> 
  3.                 education = #{education}, 
  4.             </if> 

解决方法:

在mapper配置文件中删除多配置的属性/或则在自己的bean类中补全该字段

4.<if>标签中不支持 && 符号

错误:

The entity name must immediately follow the '&' in the entity reference

解析不了 && 是什么东东

[html] view plain copy
  1. <update id="update" parameterType="Geography"> 
  2.         update Geography 
  3.         <trim prefix="set" suffixOverrides=","> 
  4.             <if test="code != null && code !=''"> 
  5.                 code=#{code}, 
  6.             </if> 
  7.             <if test="name != null && name != ''"> 
  8.                 name=#{name}, 
  9.             </if> 
  10.         </trim> 
  11.         where id= #{id} 
  12.     </update> 

解决方法:使用正确的语法咯,别用&&,用and

[html] view plain copy
  1. <!-- update --> 
  2.     <update id="update" parameterType="Geography"> 
  3.         update Geography 
  4.         <trim prefix="set" suffixOverrides=","> 
  5.             <if test="code != null and code !=''"> 
  6.                 code=#{code}, 
  7.             </if> 
  8.             <if test="name != null and name != ''"> 
  9.                 name=#{name}, 
  10.             </if> 
  11.         </trim> 
  12.         where id= #{id} 
  13.     </update> 

5.返回结果类型写错

由于我的项目配置原因, debug 输出信息中没有报错, 但是项目就是启动不起来, 所以没能截到错误信息提示, 以后遇到会补上

具体说就是: 如果自定义了返回的结果集, 返回的类型一定要是resultMap类型,(下面是错误示例) 如下

[html] view plain copy
  1. <!-- 查询Area 地名 --> 
  2.     <resultMap id="ResultArea" type="Area"> 
  3.         <id column="a_code" property="code" /> 
  4.         <result column="a_name" property="name" /> 
  5.         <result column="a_pyName" property="pyName" /> 
  6.         <result column="a_abbr" property="abbr" /> 
  7.         <result column="a_mark" property="mark" /> 
  8.     </resultMap> 
  9.      
  10.     <!-- 查询Parameter 地名 --> 
  11.     <resultMap id="ResultParameter" type="Parameter"> 
  12.         <id column="p_id" property="id" /> 
  13.         <result column="p_name" property="name" /> 
  14.         <result column="p_value" property="value" /> 
  15.         <result column="p_type" property="type" /> 
  16.         <result column="p_mark" property="mark" /> 
  17.         <association property="area" javaType="Parameter" resultMap="ResultArea" /> 
  18.     </resultMap> 
  19.  
  20.     <!-- get by id --> 
  21.     <select id="getById" resultType="ResultParameter" parameterType="int"> 
  22.         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, 
  23.                 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 
  24.         from parameter as p, area as a  
  25.         where p.acode = a.code and id = #{id} 
  26.     </select> 
常用在表连接查询中.

如上例中, 我定义了'ResultParameter'的结果集,  那么下面表连接查询的时候,如果返回的是表连接查询结果, 那么一定要使用 resultMap="ResultParameter", 否则万一提示信息中没有显示出来, 要费些时间才能找出来.

6. 标签顺序写错

错误:

The content of element type "resultMap" must match "(constructor?,id*,result*,association*,collection*,discriminator?)".

下午的时候无意中遇到这个错误, 下面是错误代码:

[html] view plain copy
  1. <resultMap id="ResultMenu" type="com.esd.hesf.model.Menu"> 
  2.     <id column="id" property="id" jdbcType="CHAR" /> 
  3.     <result column="text" property="text" jdbcType="VARCHAR" /> 
  4.     <result column="iconcls" property="iconcls" jdbcType="VARCHAR" /> 
  5.     <association property="permissionType" javaType="com.esd.hesf.model.PermissionType" resultMap="ResultPermissionType"/> 
  6.     <result column="update_time" property="updateTime" jdbcType="TIMESTAMP" /> 
  7. </resultMap> 
在<resultMap 处画红线,   原因是: 里面的<association />标签, 一定要放在这组标签最下面.  如下:

[html] view plain copy
  1. <resultMap id="ResultMenu" type="com.esd.hesf.model.Menu"> 
  2.     <id column="id" property="id" jdbcType="CHAR" /> 
  3.     <result column="text" property="text" jdbcType="VARCHAR" /> 
  4.     <result column="iconcls" property="iconcls" jdbcType="VARCHAR" /> 
  5.     <result column="update_time" property="updateTime" jdbcType="TIMESTAMP" /> 
  6.     <association property="permissionType" javaType="com.esd.hesf.model.PermissionType" resultMap="ResultPermissionType"/> 
  7. </resultMap> 

7.多写了if判断

错误:

org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'year' in 'class java.lang.String'

下面是代码:

dao层方法

[java] view plain copy
  1. /** 
  2.      * 查询审核报表情况--按公司类型查 
  3.      * @return 
  4.      */ 
  5.     List<ReportViewModel> retrieveReportByCompanyType(String year); 
mapper配置文件
[html] view plain copy
  1. <!-- retrieve report statistics by company type --> 
  2.     <select id="retrieveReportByCompanyType" parameterType="java.lang.String" resultMap="ReportViewModel"> 
  3.         select  
  4.             <include refid="report_column_list"/> 
  5.         from audit_company_view  
  6.         <where> 
  7.             <if test="year != null and year != ''"> 
  8.                 au_year = #{year,jdbcType=CHAR}  
  9.             </if> 
  10.         </where> 
  11.         group by t_id 
  12.     </select> 
一般这个错是指参数parameterType类型的字段 没有get, set方法, 我只是传个String类型的参数, so 刚遇到这个问题的时候, 有点摸不着头脑, 怎么会报这个错呢? 原来问题出现在配置文件中:

[html] view plain copy
  1. <if test="year != null and year != ''"> 
mybatis自动将if判断中的字段默认为传进来的parameterType类型的字段了... so....只要将这个if判断去掉即可.

[html] view plain copy
  1. <!-- retrieve report statistics by company type --> 
  2. <select id="retrieveReportByCompanyType" parameterType="java.lang.String" resultMap="ReportViewModel"> 
  3.     select  
  4.         <include refid="report_column_list"/> 
  5.     from audit_company_view  
  6.     <where> 
  7.             au_year = #{year,jdbcType=CHAR}  
  8.     </where> 
  9.     group by t_id 
  10. </select> 

那问题出来了, 我怎么判断传进来的单个字符串是否为空值呢? 这是否是mybatis的一个小bug呢?  答案尚在查找中, 有了会第一时间贴上来的.

【推广】 免费学中医,健康全家人
原文地址:https://www.cnblogs.com/ios9/p/7659483.html