在学习JavaWeb的Mybatis章节时,使用的是Mybatis的Mapper代理开发,在练习时出现了下面这种错误

先介绍下整体代码:(如果很熟悉代码,直接跳到最后看报错原因分析)

利用Mybatis的动态SQL实现单个条件的查询。即实现以下效果。

关键原始代码目录如下:

先给代码贴出来:(主要错误在BrandMapper,xml文件里)

首先是Brand类:

package com.itheima.pojo;/*** 品牌** alt + 鼠标左键:整列编辑** 在实体类中,基本数据类型建议使用其对应的包装类型*/public class Brand {// id 主键private Integer id;// 品牌名称private String brandName;// 企业名称private String companyName;// 排序字段private Integer ordered;// 描述信息private String description;// 状态:0:禁用  1:启用private Integer status;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getBrandName() {return brandName;}public void setBrandName(String brandName) {this.brandName = brandName;}public String getCompanyName() {return companyName;}public void setCompanyName(String companyName) {this.companyName = companyName;}public Integer getOrdered() {return ordered;}public void setOrdered(Integer ordered) {this.ordered = ordered;}public String getDescription() {return description;}public void setDescription(String description) {this.description = description;}public Integer getStatus() {return status;}public void setStatus(Integer status) {this.status = status;}@Overridepublic String toString() {return "Brand{" +"id=" + id +", brandName='" + brandName + '\'' +", companyName='" + companyName + '\'' +", ordered=" + ordered +", description='" + description + '\'' +", status=" + status +'}';}
}

然后是BrandMapper.java

package com.itheima.mapper;import com.itheima.pojo.Brand;
import org.apache.ibatis.annotations.Param;import java.util.List;
import java.util.Map;public interface BrandMapper {List<Brand> selectByConditionSingle(Brand brand);
}

然后是BrandMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--namespace:名称空间,后面的值随便写resultType:返回结果的类型
--><mapper namespace="com.itheima.mapper.BrandMapper"><resultMap id="brandResultMap" type="brand"><!--id:完成主键字段的映射column:表的列名property:实体类的属性名result:完成一般字段的映射column:表的列名property:实体类的属性名--><result column="brand_name" property="brandName"/><result column="company_name" property="companyName"/></resultMap><!--单条件动态查询--><select id="selectByConditionSingle" resultMap="brandResultMap">select *from tb_brand<where><choose><when test="status != null">status = #{status}</when><when test="company_name != null and company_name != ''">company_name like #{companyName}</when><when test="brand_name != null and brand_name != ''">brand_name like #{brandName};</when></choose></where></select></mapper>

最后是MybatisTest.java

package com.itheima.test;
import ...
public class MybatisTest {/** 单条件查询* */@Testpublic void testSelectByConditionSingle() throws IOException {//接收用户参数,该id之后需要从前端传递过来int status = 1;String companyName = "华为";String brandName = "华为";//处理参数companyName = "%" + companyName + "%";brandName = "%" + brandName + "%";//封装对象Brand brand = new Brand();//brand.setStatus(status);brand.setCompanyName(companyName);//brand.setBrandName(brandName);//1.获取SqlSeSessionFactoryString resource = "mybatis-config.xml";InputStream inputStream = Resources.getResourceAsStream(resource);SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);//2.获取SqlSessionSqlSession sqlSession = sqlSessionFactory.openSession();//3.获取Mapper接口的代理对象BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);List<Brand> brands = brandMapper.selectByConditionSingle(brand);System.out.println(brands);//5.释放资源sqlSession.close();}
}

下面,重点来了。

我们再来看看报错原因:

org.apache.ibatis.exceptions.PersistenceException:

Error querying database. Cause: org.apache.ibatis.reflection.ReflectionException: There is no getter for property named ‘company_name’ in ‘class com.itheima.pojo.Brand’

Cause: org.apache.ibatis.reflection.ReflectionException: There is no getter for property named ‘company_name’ in ‘class com.itheima.pojo.Brand’

翻译过来就是:

在Brand类中找不到company_name这个属性名。

因为在数据库中,我们的字段名为company_name,而在Brand类中,我们定义的属性名为companyName。

而在BrandMapper.xml文件中,我们又利用resultMap给company_name设置了一个映射,将其映射为companyName这一属性。

这个时候,如果我们在下面sql语句的when标签中的test属性里的值出现了company_name,就相当于我们没有使用resultMap里的映射,从而编译器抛出异常。所以只要我们将company_name改成companyName,就会去使用这个映射,从而将数据库中的字段名和Brand类中的属性名对应起来。

以上分析是我的个人理解,如有纰漏,欢迎评论区讨论交流!

【JavaWeb学习报错集(一)】ReflectionException:There is no getter for property named XXX相关推荐

  1. org.apache.ibatis.reflection.ReflectionException: There is no getter for property named XXX

    org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.reflection.Reflecti ...

  2. Mybatis占位符问题—ReflectionException: There is no getter for property named xxx

    使用${}占位符时(PS:一般都使用#{},不建议使用${}),发现xml文件中的SQL语句使用了$后会报错:具体情况如下: 解决方案如下:在参数前加上@Param("id")注解

  3. mybatis 报错There is no getter for property named 'XXX' in 'class com.xx.xx'

    mybatis报错信息记录: ### Error querying database.  Cause: org.apache.ibatis.reflection.ReflectionException ...

  4. FAQ(31):org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'ues

    进行Spring整合Mybatis时,发生的Bug: 看Log: org.mybatis.spring.MyBatisSystemException: nested exception is org. ...

  5. SSM框架报错分析(一)——There is no getter for property named 'XXX' in 'class java.lang.String'...

    一.发现问题<select id="queryStudentByNum" resultType="student" parameterType=" ...

  6. 使用mybatis-plus时mybatis报错There is no getter for property named ‘xxx‘ in ‘class com.xxx.xxx.xxxMybatis

    今天给项目的数据字典查询添加通用方法,发现里边已经有了一个查询所有数据字典的方法 List<Dict> selectDictList(); 但我想设置的方法是根据数据字典的code查询出所 ...

  7. Mybatis报错:There is no getter for property named 'xxxx' in 'class xxxx

    There is no getter for property named 'xxxx' in 'class java.lang.Integer 这是在百度上查到的,出现多的问题.不是Integer就 ...

  8. 解决 There is no getter for property named ‘null‘ in ‘class 报错

    报错内容:   Resolved [org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.r ...

  9. Mybatis报错There is no getter for property named 'id' in 'class java.lang.String'

    昨天运行公司项目的一个模块时,后台报了一个错: Caused by: org.apache.ibatis.reflection.ReflectionException: There is no get ...

最新文章

  1. HDOJ_2010_大二写_水仙花数
  2. LeetCode-Majority Element II
  3. PandasSQL语法归纳总结,真的太全了
  4. Mysql(7)——auto_increment简介
  5. Ajax跨域提交JSON和JSONP
  6. 备战美赛,这些你应该知道的知识点
  7. 运河杯交通违章 运行不起来
  8. IOS UISwitch 组件的使用
  9. 拥抱变化——从Atlas到ASP.NET AJAX(4):大大简化的了的Extender扩展器控件
  10. Git--分布式版本控制系统
  11. 物联网领域不断扩展,ATT很“兴奋”
  12. 10分钟部署一套开源表单系统
  13. feign Ambiguous mapping 解决方式
  14. OpenCv图像处理之颜色通道分离与多通道融合、图像线性融合
  15. 解决python使用gmail smtp服务发邮件报错smtplib.smtpauthentic
  16. marve register license
  17. php挂载webdav,PHP上传文件到WebDav
  18. 本征半导体的导电机制 空穴的概念
  19. Linux虚拟网络基础——veth pair
  20. uniapp页面不能触发onReachBottom事件

热门文章

  1. Docker 实践 - 使用docker搭建一个个人博客
  2. 我是怎么戒掉看玄幻小说的瘾
  3. python学习与疑问_1
  4. 美国人发明电子计算机是哪一年,电子计算机是哪一年发明的_是谁发明的
  5. 2000年-2020年全球人口密度格点数据集(不同时间空间分辨率)
  6. 1.36亿元转让部分股本,启迪国际“放弃”苏州智华控制权
  7. 常用电脑硬件技术术语集锦
  8. html登陆ajax全局拦截,jq阻止ajax进行屡次提交
  9. oracle数据库自动断开链接,ORACLE自动断开数据库连接解决办法
  10. 中国监管的出拳,矿工们何去何从?