mybatis:cannotgetjdbcconnectionexception,即使在intellij中配置并运行了连接

6pp0gazn  于 2021-07-26  发布在  Java
关注(0)|答案(0)|浏览(327)

我正在尝试在springboot中使用mybatis连接mysql数据库。
首先,我创建了数据库并使用intellij idea成功地连接到它。然后我试着只在application.properties中配置mybatis,但由于没有检测到datasource和sqlsessionfactory,它没有工作。接下来-我在java mybatisconfig类中创建了datasource和sqlsessionfactory,但应用程序无法启动,所以我将配置添加到mybatis config中。最后,应用程序开始工作,但在尝试使用Map器时引发以下异常:


### 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>

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题