Spring Boot with MyBatis Transaction Management Setup

  1. Maven Dependencies

Add the following dependencies to you're pom.xml:

<!-- MyBatis integration for Spring Boot -->
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>1.3.1</version>
</dependency>

<!-- MySQL JDBC driver -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>

<!-- Alibaba Druid connection pool -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid-spring-boot-starter</artifactId>
    <version>1.1.0</version>
</dependency>
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.0.11</version>
</dependency>

<!-- PageHelper for pagination -->
<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper-spring-boot-starter</artifactId>
    <version>1.1.2</version>
</dependency>

  1. Application Configuration (application.yml)

spring:
  datasource:
    name: sp
    url: jdbc:mysql://192.168.2.120:3306/sp?characterEncoding=utf8&useSSL=false
    username: root
    password: 123456
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.jdbc.Driver
    filters: stat
    maxActive: 20
    initialSize: 1
    maxWait: 60000
    minIdle: 1
    timeBetweenEvictionRunsMillis: 60000
    minEvictableIdleTimeMillis: 300000
    validationQuery: select 'x'
    testWhileIdle: true
    testOnBorrow: false
    testOnReturn: false
    poolPreparedStatements: true
    maxOpenPreparedStatements: 20

mybatis:
  mapper-locations: classpath:mapper/*
  config-location: classpath:mybatis/mybatis-config.xml

pagehelper:
  helperDialect: mysql
  reasonable: true
  supportMethodsArguments: true
  params: count=countSql

  1. MyBatis Configuration File

Create src/main/resources/mybatis/mybatis-config.xml as follows:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <!-- Custom MyBatis settings can be added here if needed -->
</configuration>

  1. Main Application Class

@SpringBootApplication(scanBasePackages = {"com.wuxi"})
@MapperScan("com.wuxi.dao")
public class MyApplication {
    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }
}

  1. Enabling Transaction Management

To enable declarative transaction management, annotaet service methods or classes with @Transactional:

@Service
public class UserService {
    @Transactional
    public void updateUserProfile(User user) {
        // database operations
    }
}

Tags: Spring Boot MyBatis druid pagehelper MySQL

Posted on Sun, 07 Jun 2026 16:41:13 +0000 by frost