本文共 5193 字,大约阅读时间需要 17 分钟。
1.错误提示
: Error creating bean with name 'userServiceImpl': Unsatisfied dependency expressed through field 'userMapper'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userMapper' defined in file [F:\Maven练习项目\SpringBoot\springboot-07-shiro\kuang\target\classes\com\kuang\mapper\UserMapper.class]: Unsatisfied dependency expressed through bean property 'sqlSessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [org/mybatis/spring/boot/autoconfigure/MybatisAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.ibatis.session.SqlSessionFactory]: Factory method 'sqlSessionFactory' threw exception; nested exception is org.springframework.core.NestedIOException: Failed to parse mapping resource: 'file [F:\Maven练习项目\SpringBoot\springboot-07-shiro\kuang\target\classes\mapper\UserMapper.xml]'; nested exception is org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. The XML location is 'file [F:\Maven练习项目\SpringBoot\springboot-07-shiro\kuang\target\classes\mapper\UserMapper.xml]'. Cause: org.apache.ibatis.builder.BuilderException: Error resolving class. Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias 'User'. Cause: java.lang.ClassNotFoundException: Cannot find class: User
在application.properties中,设置了mybatis的别名。
#注意,这里不能使用别名包扫描mybatis.type-aliases-package=con.kuang.pojomybatis.mapper-locations=classpath:mapper/*.xml
在UserMapper.xml中,设置使用其别名。
测试
@Autowired UserServiceImpl userService;// @Autowired// DataSource dataSource; @Test void contextLoads() throws SQLException { User admin = userService.queryUserByName("admin"); System.out.println(admin);
就报错了。
根据错误,分析原因是没有找到UserMapper.xml中的User是那个。但是明明设置了别名。就不知道为什么了。在SSM框架整合时,是可以在mybatis-config.xml中配置扫描包名来设置其别名的。
解决方法
1.注释掉application.properties的扫描包名的方式。
在UserMapper.xml中,使用原始的地址到类名,设置其返回类似。
结果:
2021-04-11 15:11:55.926 INFO 516 — [ main] com.kuang.KuangApplicationTests : Starting KuangApplicationTests using Java 1.8.0_131 on DESKTOP-5HL8I0C with PID 516 (started by admin in F:\Maven练习项目\SpringBoot\springboot-07-shiro\kuang)
2021-04-11 15:11:55.927 INFO 516 — [ main] com.kuang.KuangApplicationTests : No active profile set, falling back to default profiles: default 2021-04-11 15:11:56.454 INFO 516 — [ main] trationDelegate B e a n P o s t P r o c e s s o r C h e c k e r : B e a n ′ s h i r o C o n f i g ′ o f t y p e [ c o m . k u a n g . c o n f i g . S h i r o C o n f i g BeanPostProcessorChecker : Bean 'shiroConfig' of type [com.kuang.config.ShiroConfig BeanPostProcessorChecker:Bean′shiroConfig′oftype[com.kuang.config.ShiroConfig E n h a n c e r B y S p r i n g C G L I B EnhancerBySpringCGLIB EnhancerBySpringCGLIB c 8 c 2943 e ] i s n o t e l i g i b l e f o r g e t t i n g p r o c e s s e d b y a l l B e a n P o s t P r o c e s s o r s ( f o r e x a m p l e : n o t e l i g i b l e f o r a u t o − p r o x y i n g ) 2021 − 04 − 1115 : 11 : 56.495 I N F O 516 − − − [ m a i n ] t r a t i o n D e l e g a t e c8c2943e] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying) 2021-04-11 15:11:56.495 INFO 516 --- [ main] trationDelegate c8c2943e]isnoteligibleforgettingprocessedbyallBeanPostProcessors(forexample:noteligibleforauto−proxying)2021−04−1115:11:56.495INFO516−−−[main]trationDelegateBeanPostProcessorChecker : Bean ‘userRealm’ of type [com.kuang.config.UserRealm] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying) 2021-04-11 15:11:56.707 INFO 516 — [ main] trationDelegate$BeanPostProcessorChecker : Bean ‘getDefaultWebSecurityManager’ of type [org.apache.shiro.web.mgt.DefaultWebSecurityManager] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying) 2021-04-11 15:11:57.205 INFO 516 — [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService ‘applicationTaskExecutor’ 2021-04-11 15:11:57.277 INFO 516 — [ main] o.s.b.a.w.s.WelcomePageHandlerMapping : Adding welcome page template: index 2021-04-11 15:11:57.472 INFO 516 — [ main] com.kuang.KuangApplicationTests : Started KuangApplicationTests in 1.745 seconds (JVM running for 2.438)2021-04-11 15:11:57.673 INFO 516 — [ main] com.alibaba.druid.pool.DruidDataSource : {dataSource-1} inited
User(id=1, name=admin, pwd=123456)2021-04-11 15:11:57.862 INFO 516 — [extShutdownHook] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService ‘applicationTaskExecutor’
2021-04-11 15:11:57.862 INFO 516 — [extShutdownHook] com.alibaba.druid.pool.DruidDataSource : {dataSource-1} closing … 2021-04-11 15:11:57.864 INFO 516 — [extShutdownHook] com.alibaba.druid.pool.DruidDataSource : {dataSource-1} closed完美解决。
转载地址:http://gatgz.baihongyu.com/