Mybatis异常“Cannot enable lazy loading because CGLIB is not available. Add CGLIB to your classpath.”

一、现象描述

在使用MyBatis的延迟加载功能时候,可能会报如下错误:

org.apache.ibatis.exceptions.PersistenceException: 
### Error querying database.  Cause: java.lang.IllegalStateException: Cannot enable lazy loading because CGLIB is not available. Add CGLIB to your classpath.
### The error may exist in UserMapper.xml
### The error may involve com.queen.mybatis.mapper.UserMapper.getUserByIdStep
### The error occurred while handling results
### SQL: select id, loginId, userName, roleId, note from t_user where id=?
### Cause: java.lang.IllegalStateException: Cannot enable lazy loading because CGLIB is not available. Add CGLIB to your classpath.
	at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:26)
	at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:111)
	at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:102)
	at org.apache.ibatis.session.defaults.DefaultSqlSession.selectOne(DefaultSqlSession.java:66)
	at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:68)
	at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:52)
	at com.sun.proxy.$Proxy2.getUserByIdStep(Unknown Source)
	at com.queen.mybatis.MyBatisTest.testGetUserByAssocicationStep(MyBatisTest.java:50)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:606)

这个报错信息很明显说CGLIB包缺少,导致报错了

二、解决办法

缺少CGLIB,那我们就添加依赖到工程中就好了,打开Pom文件,添加依赖

<dependency>
	<groupId>cglib</groupId>
	<artifactId>cglib</artifactId>
	<version>3.2.5</version>
</dependency>

添加成功后,她会导入其他相关依赖的jar文件,如下:


这样就能解决您遇到的问题了。


=======欢迎大家拍砖,小手一抖,多多点赞哟!=======

版权声明:本文为博主原创文章,允许转载,但转载必须标明出处。


  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
这个错误是由于无法找到指定路径下的配置文件导致的。根据引用中的描述,当运行打包完成的jar文件时,会报错java.io.FileNotFoundException: class path resource [mybatis/mybatis-config.xml cannot be opened because it does not exist。问题出在配置文件路径的大小写问题上。在application.yaml文件中配置的路径是mybatis/mybatis-config.xml,而实际上配置文件的路径是mybatis/mybatis-confiy.xml。 解决这个问题的方法是将路径中的小写的m改成大写的M,即将mybatis-config.xml改成mybatis-confiy.xml。然后使用maven clean并package项目,问题就会解决。这个错误比较隐蔽,因为在编译和调试时是可以通过的,只有在运行打包后的jar文件时才会出现问题。 需要注意的是,在编写配置文件路径时,不仅要确保单词拼写正确,还要注意大小写。在application.yaml中写配置文件路径时,大小写不能写错。这是因为编译和调试过程中不会发现大小写错误,只有在运行打包后的jar文件时才会出现问题。 总结起来,解决这个错误的关键是检查配置文件路径的大小写拼写是否正确,并确保在打包后的jar文件中能正确找到配置文件。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [运行jar包时报错:[mybatis-config.xml] cannot be opened because it does not exist](https://blog.csdn.net/weixin_44239774/article/details/127832230)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [【springboot】class path resource [mybatis/mybatis-config.xml] cannot be opened because it does not ...](https://blog.csdn.net/Mrerlou/article/details/120723471)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值