SpringBoot整合Mybatis出现的异常

https://blog.csdn.net/zhaomengxia123/article/details/98634945

文章中一.springboot+mybatis+maven+mysql+swagger结束之后

生成我所连数据库中的student,teacher,course三张表如图所示

此时我在测试类中编写如下测试

启动我本项目的启动类报错信息如下

我启动我刚才编写的单元测试报错信息如下

2019-09-02 14:25:36.848  WARN 19392 --- [           main] o.s.w.c.s.GenericWebApplicationContext   : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'testController': Unsatisfied dependency expressed through field 'studentService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'studentService': Unsatisfied dependency expressed through field 'studentMapper'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.springboot_mybatis_module.springboot_mybatis_module.dao.StudentMapper' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
2019-09-02 14:25:36.857  INFO 19392 --- [           main] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2019-09-02 14:25:36.965 ERROR 19392 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Field studentMapper in com.springboot_mybatis_module.springboot_mybatis_module.service.StudentService required a bean of type 'com.springboot_mybatis_module.springboot_mybatis_module.dao.StudentMapper' that could not be found.

The injection point has the following annotations:
	- @org.springframework.beans.factory.annotation.Autowired(required=true)


Action:

Consider defining a bean of type 'com.springboot_mybatis_module.springboot_mybatis_module.dao.StudentMapper' in your configuration.

2019-09-02 14:25:36.969 ERROR 19392 --- [           main] o.s.test.context.TestContextManager      : Caught exception while allowing TestExecutionListener [org.springframework.test.context.web.ServletTestExecutionListener@3427b02d] to prepare test instance [com.springboot_mybatis_module.springboot_mybatis_module.SpringbootMybatisMod
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SpringBoot整合MyBatis可以使用Spring的事务管理机制来实现事务的回滚和提交。 在SpringBoot中,我们可以通过在方法上使用`@Transactional`注解来声明事务,用于标识需要进行事务管理的方法。当方法执行时,如果出现异常导致方法终止,则事务管理器会自动回滚事务,将所有数据库操作都撤销到事务开始前的状态。如果方法执行成功,则事务管理器会将所有数据库操作统一提交。 事务的回滚和提交是由Spring的事务管理器来实现的。Spring提供了几种常用的事务管理器实现,可以根据具体的项目需求选择合适的事务管理器。 其中,如果要使用MyBatis的事务管理器,需要在SpringBoot的配置文件(application.properties或application.yml)中进行配置。具体配置项如下: ``` # 指定事务管理器类型为JDBC spring.datasource.type=org.apache.tomcat.jdbc.pool.DataSource # 开启事务支持 spring.datasource.tomcat.initSQL=getLockTimeout spring.datasource.url=jdbc:mysql://localhost:3306/mydb spring.datasource.username=root spring.datasource.password=root # 配置MyBatis的Mapper接口 mybatis.type-aliases-package=com.example.model mybatis.mapper-locations=classpath:mapper/*.xml # 配置事务管理器 mybatis.mapper-locations=classpath:mapper/*.xml ``` 配置完成后,就可以在需要进行事务管理的方法上添加`@Transactional`注解,即可实现事务的自动回滚和提交。 需要注意的是,事务的回滚和提交是根据方法的异常情况来决定的,只有在方法中抛出运行时异常或Error时才会触发事务的回滚。如果在方法中抛出的是受检异常(如IOException),则不会触发事务的回滚。如果需要手动触发事务的回滚,可以在方法中捕获异常,并调用`TransactionAspectSupport.currentTransactionStatus().setRollbackOnly()`方法来设置事务的回滚状态。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值