MyBatis学习笔记(四)--踩过的那些坑

一、映射文件被注册了两次

1、异常

org.apache.ibatis.exceptions.PersistenceException: 
### Error building SqlSession.
### The error may exist in com/jc/dao/OrderMapper.xml
### The error occurred while processing mapper_resultMap[UserOrder]_association[order]
### Cause: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: org.apache.ibatis.binding.BindingException: Type interface com.jc.dao.TeacherDao is already known to the MapperRegistry.

2、原因

在全局配置文件中批量注册的包中包含的TeacherMapper.xml动态绑定了TeacherDao.java,导致被重复注册了两次。

<!--指定映射文件位置-->
    <mappers>
        <!--单个相对路径加载-->
        <!--<mapper resource="StudentDao.xml"></mapper>-->
        <!--<mapper resource="StudentMapper.xml"></mapper>-->
        <!--<mapper resource="TeacherMapper.xml"></mapper>-->
        <!--批量加载,通过package元素将会把指定包下面的所有Mapper接口进行注册-->
        <package name="com.jc.dao"/>
    </mappers>

3、解决

批量加载通过package元素将会把指定包下面的所有Mapper接口进行注册,此时需要把单个相对路径加载的代码删去,如上图贴的代码即可

 

二、There is no setter for property named 'orderTime' in 'class com.jc.entity.Order'

1、异常

org.apache.ibatis.exceptions.PersistenceException: 
### Error querying database.  Cause: org.apache.ibatis.reflection.ReflectionException: Could not set property 'orderTime' of 'class com.jc.entity.Order' with value '2021-05-18 06:47:03.0' Cause: org.apache.ibatis.reflection.ReflectionException: There is no setter for property named 'orderTime' in 'class com.jc.entity.Order'
### The error may exist in com/jc/dao/OrderMapper.xml
### The error may involve com.jc.dao.OrderMapper.getUserOrder
### The error occurred while handling results
### SQL: select `user`.userID,`user`.userName,         t_order.oid,t_order.ordertime,t_order.total,t_order.state,t_order.address,t_order.receivename,t_order.phone,t_order.userID         FROM `user`,t_order         WHERE `user`.userID=t_order.userID
### Cause: org.apache.ibatis.reflection.ReflectionException: Could not set property 'orderTime' of 'class com.jc.entity.Order' with value '2021-05-18 06:47:03.0' Cause: org.apache.ibatis.reflection.ReflectionException: There is no setter for property named 'orderTime' in 'class com.jc.entity.Order'

2、原因

我寻思着我分明有get、set方法,不懂他为什么说没有,后面仔细查看发现我在映射文件中写的属性名(orderTime)和实体类(ordertime)中的不一致,这就导致找不到对应方法。

当时配置映射文件中resultMap属性名的时候,是看着数据库名称来的,没有注意到实体类和数据库的不一样。这就告诉我们以后实体类和数据库命名尽量一致,免得麻烦。

3、解决

将映射文件中配置的属性名改成和实体类一样就好了。

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值