springboot整合mybatis&jdbc报错
新手小白初始建立一个简单的spring boot项目整合jdbc和mybatis后的调试出现几个错误。接下来以个人笔记的形式总结报错。
1、关于datasource的bean装载问题
报错如下所示:
java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'jdbcDao': Unsatisfied dependency expressed through field 'jdbcTemplate'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration$JdbcTemplateConfiguration': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception; nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Failed to determine a suitable driver class
这里的问题在于我的Datasource这里并没有配置好,那么自然而然就要想到这里的问题。
即设置datasource。
2、关于url no set问题
这里如果使用DruidDataSource数据库包要记得看清楚util包下的属性名是jdbcUrl,在配置dataSource很容易出现url no set
的错误,所以这里我是手动输入更加保险。以下就是出现url路径发生的报错:
org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is java.sql.SQLException: url not set
我这里可以通过手动设置datasource来完成。
@Bean
@ConfigurationProperties(prefix = "jdbc")
public DataSource dataSource() {
DataSourceBuilder dataSourceBuilder = DataSourceBuilder.create();
dataSourceBuilder.driverClassName("com.mysql.cj.jdbc.Driver");
dataSourceBuilder.url("jdbc:mysql://localhost:3306/user?useSSL=false&serverTimezone=UTC&characterEncoding=utf-8");
dataSourceBuilder.username("root");
dataSourceBuilder.password("root");
return dataSourceBuilder.build();
}
最后完成后截图所示:
以上暂时是我目前遇到的问题,以此作为记录谢谢大家阅读和指正。