Mybatis:CannotGetJdbcConnectionException 即使连接已配置并在 Intellij 中工作

问题描述

我正在尝试在 Spring Boot 中使用 MyBatis 连接到 MysqL 数据库

首先我创建了数据库并使用 Intellij IDEA 成功连接到它。 然后我尝试仅在 application.properties 中配置 Mybatis 但它没有工作,因为没有检测到 DataSource 和 sqlSessionFactory。 接下来 - 我已经在 J​​ava MyBatisConfig 类中创建了 DataSource 和 sqlSessionFactory 但应用​​程序无法启动,所以我已经将配置添加到 mybatis-config 中。 最后应用程序开始工作,但在尝试使用映射器时抛出此异常:

### Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is com.MysqL.jdbc.exceptions.jdbc4.MysqLNonTransientConnectionException: Could not create connection to database server.

应用属性

## Spring DATASOURCE (DataSourceAutoConfiguration & DataSourceProperties)
spring.datasource.driver-class-name=com.MysqL.jdbc.Driver
spring.datasource.url = jdbc:MysqL://localhost:3306/bgiledatabase
spring.datasource.username = root
spring.datasource.password = root

#Mybatis
mybatis.type-aliases-package=com.bajorek.bgile.entity
mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.configuration.map-underscore-to-camel-case=true

## Config
test.datasource.url=jdbc:MysqL://localhost:3306/bgiledatabase
test.datasource.username=root
test.datasource.password=root
test.datasource.driverClassName=com.MysqL.jdbc.Driver

这是我的 MyBatisConfigClass:

@Configuration
@MapperScan("com.bajorek.bgile.mapper")
public class MyBatisConfig
{
    @Value("${test.datasource.url}")
    private String url;

    @Value("${test.datasource.username}")
    private String user;

    @Value("${test.datasource.password}")
    private String password;

    @Value("${test.datasource.driverClassName}")
    private String driverClass;

    @Bean(name = "dataSource")
    public DataSource dataSource()
    {
        PooledDataSource dataSource = new PooledDataSource();
        dataSource.setDriver(driverClass);
        dataSource.setUrl(url);
        dataSource.setUsername(user);
        dataSource.setPassword(password);
        return dataSource;
    }

    @Bean
    public sqlSessionfactorybean sqlSessionfactorybean(final DataSource dataSource)
    {
        sqlSessionfactorybean sqlSessionfactorybean = new sqlSessionfactorybean();
        sqlSessionfactorybean.setDataSource(dataSource);
        sqlSessionfactorybean.setConfigLocation(new PathMatchingResourcePatternResolver()
                .getResource("classpath:mybatis-config.xml"));
        sqlSessionfactorybean.setTypeAliasesPackage("com.bajorek.bgile.mapper");
        return sqlSessionfactorybean;
    }
}

还有 mybatis-config.xml 文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <settings>
        <setting name="aggressiveLazyLoading" value="false"/>
        <setting name="lazyLoadingEnabled" value="false"/>
        <setting name="mapUnderscoreToCamelCase" value="true"/>
        <setting name="logImpl" value="SLF4J"/>
        <setting name="jdbcTypeForNull" value="NULL"/>
        <setting name="callSettersOnNulls" value="true"/>
    </settings>
    <typeAliases>
        <!-- Data Transfer Objects -->
    </typeAliases>
    <typeHandlers>
        <!--        <typeHandler handler="org.apache.ibatis.type.LocalDateTimeTypeHandler"/>-->
    </typeHandlers>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.MysqL.jdbc.Driver"/>
                <property name="url" value="jdbc:MysqL://localhost:3306/bgiledatabase"/>
                <property name="username" value="root"/>
                <property name="password" value="root"/>
            </dataSource>
        </environment>
    </environments>
    <!-- explicit inclusion of reusable elements -->
    <mappers>
        <mapper resource="mapper/UserMapper.xml"/>
    </mappers>
</configuration>

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...