Spring Boot,HikariCP和mybatis集成

305 阅读1分钟

我们可以在Spring Boot项目中轻松集成和配置HikariCP来作为数据源。以下是具体的步骤:

1. 添加依赖

确保在你的pom.xml中包含必要的依赖:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>2.1.4</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>com.zaxxer</groupId>
        <artifactId>HikariCP</artifactId>
    </dependency>
</dependencies>

2. 配置数据源

application.propertiesapplication.yml文件中配置HikariCP相关属性。

使用application.properties

# 数据源配置
spring.datasource.url=jdbc:mysql://localhost:3306/yourdatabase
spring.datasource.username=root
spring.datasource.password=yourpassword
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

# HikariCP配置
spring.datasource.hikari.connection-timeout=20000  # 连接超时时间(毫秒)
spring.datasource.hikari.maximum-pool-size=10      # 连接池中最大的连接数

# 启用PreparedStatement缓存
spring.datasource.hikari.data-source-properties.cachePrepStmts=true
# 设置PreparedStatement缓存的大小,推荐值250
spring.datasource.hikari.data-source-properties.prepStmtCacheSize=250
# 设置可以缓存的SQL语句的最大长度,推荐值2048
spring.datasource.hikari.data-source-properties.prepStmtCacheSqlLimit=2048

# MyBatis配置
mybatis.mapper-locations=classpath:mappers/*.xml

使用application.yml

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/yourdatabase
    username: root
    password: yourpassword
    driver-class-name: com.mysql.cj.jdbc.Driver
    hikari:
      connection-timeout: 20000
      maximum-pool-size: 10

mybatis:
  mapper-locations: classpath:mappers/*.xml

3. 创建Mapper接口并使用注解

创建一个Mapper接口,使用注解来定义SQL映射。例如,创建一个UserMapper接口:

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

@Mapper
public interface UserMapper {
    @Select("SELECT * FROM users WHERE id = #{id}")
    User selectUser(@Param("id") int id);

    @Select("SELECT * FROM users WHERE ${columnName} = #{value}")
    User selectByDynamicColumn(@Param("columnName") String columnName, @Param("value") String value);
}

4. 配置Spring Boot应用类

在Spring Boot应用类中启用MyBatis的Mapper扫描:

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

@SpringBootApplication
@MapperScan("com.example.mapper")
public class MyBatisApplication {
    public static void main(String[] args) {
        SpringApplication.run(MyBatisApplication.class, args);
    }
}

5. 使用Mapper

在Service类中注入并使用Mapper。例如:

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

@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;

    public User getUserById(int id) {
        return userMapper.selectUser(id);
    }

    public User getUserByDynamicColumn(String columnName, String value) {
        return userMapper.selectByDynamicColumn(columnName, value);
    }
}