【发布时间】:2018-09-24 18:22:30
【问题描述】:
我正在学习 Spring Boot,并创建了一个简单的应用程序。
我使用以下 maven 依赖项:
- spring-boot-starter-parent:2.0.1.RELEASE
- mybatis-spring-boot-starter:1.3.2
我想使用连接池来提高我的应用程序的速度,但我不确定如何为这个用例配置 Spring Boot 和 MyBatis。
在此之前,我使用 Java EE + 应用服务器,并通过 mybatis.xml 文件配置 mybatis 以使用 JNDI 数据源。连接池由应用服务器提供。
但是现在,我有点困惑将数据库连接参数放在哪里,因为有两个可能的候选者:application.properties 和mybatis.xml。
这两个配置文件在这个环境中有什么联系?
我知道 Spring Boot 应用程序在 Tomcat Web 容器上运行,因此我无法在其中创建 JNDI 数据源和连接池。
在Mybatis使用Spring Boot时,指定数据库连接+连接池的正确方法是什么?
目前我也在mybatis.xml和application.xml文件中添加了连接参数:
application.properties
spring.datasource.url=...
spring.datasource.username=...
spring.datasource.password=...
spring.datasource.driver-class-name=...
mybatis.xml
<configuration>
<environments default="jdbc">
<environment id="jdbc">
<transactionManager type="JDBC" />
<datasource type="POOLED">
<property name="driver" value="..." />
<property name="url" value="..." />
<property name="username" value="..." />
<property name="password" value="..." />
</environment>
<environments>
</configuration>
【问题讨论】:
-
这是一个老例子。我使用最新的 Spring Boot,其中有新的注释和类。例如。我不需要使用构造函数注入。
标签: java spring spring-boot mybatis