记录一下Spring Boot使用MyBatis时项目启动报错问题

 

 

记录一下Spring Boot使用MyBatis时项目启动报错问题

 

刚开始启动项目时,报错如下:

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-10-06 23:26:55.263 ERROR 29072 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

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

Description:

Failed to configure a DataSource: no embedded datasource could be configured.

Reason: Failed to determine a suitable driver class


Action:

Consider the following:
	If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
	If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).


Process finished with exit code 1

 后面在网上查找了一些方法:

1.@SpringBootApplication后面加(scanBasePackages = "com.myBatisDemo.myuBatis")

2.加上@MapperScan(value = "com.myBatisDemo.myuBatis.UserMapper")

但是后面仍然报错:

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-10-06 23:22:28.590 ERROR 22096 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

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

Description:

Field userMapper in com.myBatisDemo.myuBatis.MainController required a bean of type 'com.myBatisDemo.myuBatis.UserMapper' 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.myBatisDemo.myuBatis.UserMapper' in your configuration.


Process finished with exit code 1

后面因为找了很多方法都不能解决,所以我重新创建了一个项目,按照之前能够运行成功的一个项目进行配置,最后找出了我的问题是在application.properties的数据库连接的配置中:

 运行失败的application.properties:

#1.项目启动的端口号
server.port=8088

spring.datasource.url=jdbc.url=jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC
spring.datasource.username=xxx
spring.datasource.password=xxx

 运行成功的application.properties: 

#1.项目启动的端口号
server.port=8088

spring.datasource.url=jdbc:mysql://localhost:3306/mybatis?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
spring.datasource.username=xxx
spring.datasource.password=xxx

最后终于启动成功,所以出现这样的问题有可能是application.properties里面配置的参数有问题。这是我自己遇到的问题,但是并不是所以的这类问题都是这样解决,如果你的问题和我的一样,希望能够帮助到你。

 

另外在解决这个问题后,我还遇到了一个MyBatis的问题:org.apache.ibatis.binding.BindingException: Invalid bound statement (not found),也是尝试了各种解决办法都没有解决。

刚开始我的UserMapper接口和UserMapper.xml的目录结构如下:

后面改成了下面,成功解决问题。所以Mapper接口和resource下的Mapper.xml对应的包名一定要相同,否则很可能找不到对应的Mapper.xml文件。

另外在创建 com.mybatisDemo.myuBatis文件要注意,要按下面的形式创建:

最后完美运行项目:

希望能帮助大家解决跟我一样的问题。 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值