微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决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.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?