【问题标题】:How to use connection pool with Spring Boot + MybatisSpring Boot + Mybatis 如何使用连接池
【发布时间】: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.propertiesmybatis.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>

【问题讨论】:

标签: java spring spring-boot mybatis


【解决方案1】:

如果您使用mybatis-spring-boot-started,则无需使用mybatis.xml 来指定数据源参数。它不用于从 spring 启动的数据库访问。

为了添加连接池,您需要通过 spring-boot 将依赖项添加到任何连接池supported。向org.springframework.boot:spring-boot-starter-jdbc 添加依赖项的最简单方法。如果你有它,则连接已经被池化。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-15
    • 2018-10-05
    • 2016-08-02
    • 2017-03-15
    • 1970-01-01
    • 2022-06-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多