您好,登錄后才能下訂單哦!
在Spring Boot 2中,推薦使用HikariCP作為MyBatis的連接池。HikariCP是一個(gè)高性能、輕量級(jí)的連接池庫(kù),已經(jīng)成為了Spring Boot的默認(rèn)連接池。
要在Spring Boot 2中使用HikariCP和MyBatis,請(qǐng)按照以下步驟操作:
在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>
在application.properties
或application.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
在application.properties
或application.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
在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方法
}
在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>
在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ù)操作了。
免責(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)容。