温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

SpringBoot2 MyBatis连接池选择

发布时间:2024-11-05 14:25:19 来源:亿速云 阅读:79 作者:小樊 栏目:编程语言

在Spring Boot 2中,推荐使用HikariCP作为MyBatis的连接池。HikariCP是一个高性能、轻量级的连接池库,已经成为了Spring Boot的默认连接池。

要在Spring Boot 2中使用HikariCP和MyBatis,请按照以下步骤操作:

  1. 添加依赖

pom.xml文件中添加以下依赖:

<dependencies>
    <!-- Spring Boot MyBatis Starter -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-mybatis</artifactId>
    </dependency>
    <!-- HikariCP Connector -->
    <dependency>
        <groupId>com.zaxxer</groupId>
        <artifactId>HikariCP</artifactId>
    </dependency>
    <!-- MySQL Connector -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>
</dependencies>
  1. 配置数据源

application.propertiesapplication.yml文件中配置数据源信息:

# application.properties
spring.datasource.hikari.maximum-pool-size=10
spring.datasource.hikari.minimum-idle=2
spring.datasource.hikari.idle-timeout=600000
spring.datasource.hikari.max-lifetime=1800000
spring.datasource.hikari.connection-timeout=30000
spring.datasource.url=jdbc:mysql://localhost:3306/your_database?useSSL=false&serverTimezone=UTC
spring.datasource.username=your_username
spring.datasource.password=your_password

# application.yml
spring:
  datasource:
    hikari:
      maximum-pool-size: 10
      minimum-idle: 2
      idle-timeout: 600000
      max-lifetime: 1800000
      connection-timeout: 30000
    url: jdbc:mysql://localhost:3306/your_database?useSSL=false&serverTimezone=UTC
    username: your_username
    password: your_password
  1. 配置MyBatis

application.propertiesapplication.yml文件中配置MyBatis信息:

# application.properties
mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.type-aliases-package=com.example.your_project.domain

# application.yml
mybatis:
  mapper-locations: classpath:mapper/*.xml
  type-aliases-package: com.example.your_project.domain
  1. 创建实体类

src/main/java/com/example/your_project/domain目录下创建实体类,例如User.java

package com.example.your_project.domain;

public class User {
    private Long id;
    private String name;
    private Integer age;

    // 省略getter和setter方法
}
  1. 创建Mapper接口和映射文件

src/main/java/com/example/your_project/mapper目录下创建Mapper接口,例如UserMapper.java

package com.example.your_project.mapper;

import com.example.your_project.domain.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface UserMapper {
    @Select("SELECT * FROM user")
    List<User> findAll();
}

src/main/resources/mapper目录下创建映射文件UserMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.your_project.mapper.UserMapper">
    <select id="findAll" resultType="com.example.your_project.domain.User">
        SELECT * FROM user
    </select>
</mapper>
  1. 在Service或Controller中使用Mapper

在Service或Controller中注入UserMapper,然后调用其方法进行数据库操作。

package com.example.your_project.controller;

import com.example.your_project.domain.User;
import com.example.your_project.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class UserController {
    @Autowired
    private UserMapper userMapper;

    @GetMapping("/users")
    public List<User> findAll() {
        return userMapper.findAll();
    }
}

现在,你已经成功配置了HikariCP连接池和MyBatis,并可以在你的项目中进行数据库操作了。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI