如何解决Mybatis:CannotGetJdbcConnectionException 即使连接已配置并在 Intellij 中工作
我正在尝试在 Spring Boot 中使用 MyBatis 连接到 MysqL 数据库。
首先我创建了数据库并使用 Intellij IDEA 成功连接到它。 然后我尝试仅在 application.properties 中配置 Mybatis 但它没有工作,因为没有检测到 DataSource 和 sqlSessionFactory。 接下来 - 我已经在 Java 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;
}
}
<?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 举报,一经查实,本站将立刻删除。