在Spring Boot 2中,推荐使用HikariCP作为MyBatis的连接池。HikariCP是一个高性能、轻量级的连接池库,已经成为了Spring Boot的默认连接池。
要在Spring Boot 2中使用HikariCP和MyBatis,请按照以下步骤操作:
在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
文件中配置数据源信息:
# 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
目录下创建实体类,例如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
目录下创建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>
在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,并可以在你的项目中进行数据库操作了。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。