我正在学习 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 数据源和连接池。
当我将Spring Boot与Mybatis一起使用时,指定数据库连接+连接池的正确方法是什么?
目前我还将连接参数添加到 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>
最佳答案
如果您使用mybatis-spring-boot-started
,则不需要使用mybatis.xml
来指定数据源参数。它不用于从 spring 发起的数据库访问。
为了添加连接池,您需要添加对任何连接池 supported 的依赖项通过 Spring 启动。最简单的方法是添加对 org.springframework.boot:spring-boot-starter-jdbc 的依赖关系。如果你有它,连接已经被池化了。
关于java - 如何使用Spring Boot + Mybatis连接池,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49833126/