溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

SpringBoot2 MyBatis連接池選擇

發(fā)布時(shí)間:2024-11-05 14:25:19 來源:億速云 閱讀:79 作者:小樊 欄目:編程語言

在Spring Boot 2中,推薦使用HikariCP作為MyBatis的連接池。HikariCP是一個(gè)高性能、輕量級(jí)的連接池庫(kù),已經(jīng)成為了Spring Boot的默認(rèn)連接池。

要在Spring Boot 2中使用HikariCP和MyBatis,請(qǐng)按照以下步驟操作:

  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. 配置數(shù)據(jù)源

application.propertiesapplication.yml文件中配置數(shù)據(jù)源信息:

# 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. 創(chuàng)建實(shí)體類

src/main/java/com/example/your_project/domain目錄下創(chuàng)建實(shí)體類,例如User.java。

package com.example.your_project.domain;

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

    // 省略getter和setter方法
}
  1. 創(chuàng)建Mapper接口和映射文件

src/main/java/com/example/your_project/mapper目錄下創(chuàng)建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目錄下創(chuàng)建映射文件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,然后調(diào)用其方法進(jìn)行數(shù)據(jù)庫(kù)操作。

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();
    }
}

現(xiàn)在,你已經(jīng)成功配置了HikariCP連接池和MyBatis,并可以在你的項(xiàng)目中進(jìn)行數(shù)據(jù)庫(kù)操作了。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI