栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springboot整合jdbc错误笔记

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springboot整合jdbc错误笔记

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();
    }

最后完成后截图所示:


以上暂时是我目前遇到的问题,以此作为记录谢谢大家阅读和指正。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/315342.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号