You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot + MyBatis连接池配置方法(附Maven依赖场景)

Spring Boot + MyBatis 连接池配置指南(从Java EE/JNDI迁移)

Hey there! Since you're transitioning from Java EE + app server-managed JNDI data sources to Spring Boot with MyBatis, let's break down how to configure a high-performance connection pool step by step. Spring Boot makes this way simpler than the old XML-heavy JNDI setup, and we'll stick with its default (and highly optimized) HikariCP first, then touch on alternatives if you prefer.

1. 补充连接池依赖

First off, Spring Boot's default connection pool is HikariCP—it's fast, lightweight, and requires almost zero extra config if you're using the starter dependencies. Since you already have spring-boot-starter-parent and mybatis-spring-boot-starter, you might already have Hikari pulled in indirectly, but to be explicit, add this to your pom.xml (if it's not there):

<dependency>
    <groupId>com.zaxxer</groupId>
    <artifactId>HikariCP</artifactId>
</dependency>

If you'd rather use another pool like Druid or Tomcat JDBC, just replace the Hikari dependency with the corresponding starter (e.g., com.alibaba:druid-spring-boot-starter).

2. 配置数据源与连接池参数

Forget about JNDI lookups—Spring Boot lets you configure everything directly in application.properties (or application.yml for YAML fans). Here's a typical setup for HikariCP with MySQL (adjust for your database):

# 基础数据库连接信息
spring.datasource.url=jdbc:mysql://localhost:3306/your_db?useSSL=false&serverTimezone=UTC
spring.datasource.username=db_user
spring.datasource.password=db_pass
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

# HikariCP连接池优化参数(按需调整)
spring.datasource.hikari.maximum-pool-size=10 # 最大连接数,根据并发量调整
spring.datasource.hikari.minimum-idle=2 # 最小空闲连接
spring.datasource.hikari.connection-timeout=30000 # 连接超时时间(毫秒)
spring.datasource.hikari.idle-timeout=600000 # 空闲连接超时时间
spring.datasource.hikari.max-lifetime=1800000 # 连接最大生命周期

These settings ensure your pool runs efficiently—no more relying on the app server to manage connections.

3. 适配Spring Boot配置MyBatis

You don't need that old mybatis.xml file anymore (though you can still use it if you want). Spring Boot integrates MyBatis seamlessly with simple configurations:

方式1:用application.properties配置MyBatis

Add these lines to set up mapper locations, type aliases, and common behaviors:

# MyBatis核心配置
mybatis.mapper-locations=classpath:mappers/*.xml # 你的Mapper XML文件存放路径
mybatis.type-aliases-package=com.yourpackage.model # 实体类所在包,可省略全类名
mybatis.configuration.map-underscore-to-camel-case=true # 自动将下划线命名转为驼峰命名

方式2:保留原mybatis.xml(可选)

If you want to keep your existing mybatis.xml file, just add this line to tell Spring Boot where to find it:

mybatis.config-location=classpath:mybatis-config.xml

4. 让Spring扫描你的Mapper接口

Add the @MapperScan annotation to your Spring Boot main application class to automatically detect all MyBatis mapper interfaces:

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.yourpackage.mapper") // 替换为你的Mapper接口所在包
public class YourApplication {
    public static void main(String[] args) {
        SpringApplication.run(YourApplication.class, args);
    }
}

Alternatively, you can add @Mapper to each individual mapper interface, but @MapperScan is cleaner for larger projects.

对比原来的JNDI方式

In your old Java EE setup, the app server managed the connection pool and you looked it up via JNDI. With Spring Boot, the connection pool is embedded directly in your app—this makes your application self-contained, easier to deploy (no more app server config), and gives you full control over pool settings.

快速测试

Once everything is set up, you can inject your mapper into a service or controller like this:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class YourService {
    private final YourMapper yourMapper;

    @Autowired
    public YourService(YourMapper yourMapper) {
        this.yourMapper = yourMapper;
    }

    // Use yourMapper to execute queries—HikariCP will handle connection pooling automatically!
}

Spring will handle all connection management behind the scenes, so you don't have to worry about opening/closing connections manually.

内容的提问来源于stack exchange,提问作者zappee

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 03:50:17