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、解决
将映射文件中配置的属性名改成和实体类一样就好了。
上一篇:
IDEA上Java项目控制台中文乱码