前面两篇文章我们介绍了如何快速创建一个 Spring Boot 工程《Spring Boot(一): 快速开始》 https://www.geekdigging.com/2019/09/11/2531363117/ 和在 Spring Boot 中如何使用模版引擎 Thymeleaf 渲染一个 web 页面《Spring Boot (二): 模版引擎 Thymeleaf 渲染 Web 页面》 https://www.geekdigging.com/2019/09/16/2755709569/ , 本篇文章我们继续介绍在 Spring Boot 中如何使用数据库.
1. 概述
数据库方面我们选用 MySQL , Spring Boot 提供了直接使用 JDBC 的方式连接数据库, 毕竟使用 JDBC 并不是很方便, 需要我们自己写更多的代码才能使用, 一般而言在 Spring Boot 中我们常用的 ORM 框架有 JPA 和 Mybaties , 本篇文章我们要介绍的就是 JPA 的使用姿势.
说道使用 ORM 框架, 就不得不顺便聊一下连接池, 市面上很多成熟的数据库连接池, 如 C3P0 , Tomcat 连接池, BoneCP 等等很多产品, 但是我们为什么要介绍 Hikari ? 这个要从 BoneCP 说起.
因为, 传说中 BoneCP 在快速这个特点上做到了极致, 官方数据是 C3P0 等的 25 倍左右. 不相信? 其实我也不怎么信. 可是, 有图有真相啊, 传说图片来源于官网, 然而笔者在官网并没有找到, 大家看一下:
看起来是不是完全吊打, 但是当 HikariCP 横空出世以后, 这个局面就被完全改写了, BoneCP 被 HikariCP 完全吊打, 看了一下 BoneCP GitHub 上面的版本更新, 发现在 2013 年 10 月 23 日以后就再也没有更新过了, 包括在仓库介绍上面都写着建议大家使用 HikariCP , 看来作者已经完全心灰意冷了.
- public interface UserRepository extends JpaRepository<UserModel, Long> {
- UserModel getByIdIs(Long id);
- UserModel findByNickName(String nickName);
- int countByAge(int age);
- List<UserModel> findByNickNameLike(String nickName);
- }
- ***
- <?xml version="1.0" encoding="UTF-8"?>
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>2.1.8.RELEASE</version>
- <relativePath/> <!-- lookup parent from repository -->
- </parent>
- <groupId>com.springboot</groupId>
- <artifactId>spring-boot-jpa-hikari</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <name>spring-boot-jpa-hikari</name>
- <description>spring-boot-jpa-hikari</description>
- <properties>
- <java.version>1.8</java.version>
- </properties>
- <dependencies>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-data-jpa</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-Web</artifactId>
- </dependency>
- <dependency>
- <groupId>MySQL</groupId>
- <artifactId>MySQL-connector-java</artifactId>
- <scope>runtime</scope>
- </dependency>
- <dependency>
- <groupId>org.projectlombok</groupId>
- <artifactId>lombok</artifactId>
- <optional>true</optional>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-test</artifactId>
- <scope>test</scope>
- </dependency>
- </dependencies>
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- </plugin>
- </plugins>
- </build>
- </project>
- ***
- server:
- port: 8080
- spring:
- application:
- name: spring-boot-jpa-hikari
- jpa:
- database: MySQL
- show-sql: true
- generate-ddl: true
- database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
- hibernate:
- ddl-auto: update
- datasource:
- url: jdbc:MySQL://192.168.0.128:3306/test?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8&useSSL=false
- username: root
- password: 123456
- driver-class-name: com.MySQL.cj.jdbc.Driver
- type: com.zaxxer.hikari.HikariDataSource
- hikari:
- auto-commit: true
- minimum-idle: 2
- idle-timeout: 60000
- connection-timeout: 30000
- max-lifetime: 1800000
- pool-name: DatebookHikariCP
- maximum-pool-size: 5
- ***
- @Entity
- @Data
- @Table(name = "user")
- public class UserModel {
- @Id
- @GeneratedValue(generator = "paymentableGenerator")
- @GenericGenerator(name = "paymentableGenerator", strategy = "uuid")
- @Column(name ="ID",nullable=false,length=36)
- private String id;
- @Column(nullable = true, unique = true)
- private String nickName;
- @Column(nullable = false)
- private int age;
- }
- ***
- public interface UserRepository extends JpaRepository<UserModel, Long> {
- UserModel getByIdIs(Long id);
- UserModel findByNickName(String nickName);
- int countByAge(int age);
- List<UserModel> findByNickNameLike(String nickName);
- }
- ***
- @RestController
- public class UserController {
- @Autowired
- UserRepository userRepository;
- /**
- * 查询用户列表
- * @return
- */
- @GetMapping("/user")
- public List<UserModel> user() {
- return userRepository.findAll(Sort.by("id").descending());
- }
- /**
- * 新增或更新用户信息
- * @param user
- * @return
- */
- @PostMapping("/user")
- public UserModel user(UserModel user) {
- return userRepository.save(user);
- }
- /**
- * 根据 id 删除用户
- * @param id
- * @return
- */
- @DeleteMapping("/user")
- public String deleteUserById(Long id) {
- userRepository.deleteById(id);
- return "success";
- }
- }
- https://github.com/brettwooldridge/HikariCP
- https://docs.spring.io/spring-data/jpa/docs/current/reference/html/
- http://www.ityouknow.com/springboot/2016/08/20/spring-boot-jpa.html
来源: https://www.cnblogs.com/babycomeon/p/11565843.html