🚧 数据库相关功能
This commit is contained in:
88
README.md
88
README.md
@@ -1 +1,87 @@
|
|||||||
# Springboot3和JDK17的基础脚手架
|
# Springboot3和JDK17的基础脚手架 - 数据库分支
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
## 基础依赖
|
||||||
|
- MySQL
|
||||||
|
- MyBatis-Plus 3.5.15 这个版本是SpringBoot3支持的最高版本
|
||||||
|
- jsqlparser 在MyBatis-Plus 3.5.9以后的版本,插件部分开始修改为可选依赖,所以这里手动增加。
|
||||||
|
|
||||||
|
> 引入 MyBatis-Plus 之后请不要再次引入 MyBatis 以及 mybatis-spring-boot-starter和MyBatis-Spring,以避免因版本差异导致的问题。
|
||||||
|
|
||||||
|
```xml
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.mysql</groupId>
|
||||||
|
<artifactId>mysql-connector-j</artifactId>
|
||||||
|
<scope>runtime</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.baomidou</groupId>
|
||||||
|
<artifactId>mybatis-plus-spring-boot3-starter</artifactId>
|
||||||
|
<version>3.5.15</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.baomidou</groupId>
|
||||||
|
<artifactId>mybatis-plus-jsqlparser</artifactId>
|
||||||
|
<version>3.5.14</version>
|
||||||
|
<scope>compile</scope>
|
||||||
|
</dependency>
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
## Druid数据源配置详解
|
||||||
|
- [官方WIKI](https://github.com/alibaba/druid/wiki/%E9%A6%96%E9%A1%B5)
|
||||||
|
|
||||||
|
### Druid多数据源说明
|
||||||
|
添加配置
|
||||||
|
```properties
|
||||||
|
spring.datasource.url=
|
||||||
|
spring.datasource.username=
|
||||||
|
spring.datasource.password=
|
||||||
|
|
||||||
|
# Druid 数据源配置,继承spring.datasource.* 配置,相同则覆盖
|
||||||
|
spring.datasource.druid.initial-size=5
|
||||||
|
spring.datasource.druid.max-active=5
|
||||||
|
|
||||||
|
# Druid 数据源 1 配置,继承spring.datasource.druid.* 配置,相同则覆盖
|
||||||
|
spring.datasource.druid.one.max-active=10
|
||||||
|
spring.datasource.druid.one.max-wait=10000
|
||||||
|
|
||||||
|
|
||||||
|
# Druid 数据源 2 配置,继承spring.datasource.druid.* 配置,相同则覆盖
|
||||||
|
|
||||||
|
spring.datasource.druid.two.max-active=20
|
||||||
|
spring.datasource.druid.two.max-wait=20000
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
# Druid 数据源配置,继承spring.datasource.* 配置,相同则覆盖
|
||||||
|
|
||||||
|
```properties
|
||||||
|
spring.datasource.druid.initial-size=5
|
||||||
|
spring.datasource.druid.max-active=5
|
||||||
|
```
|
||||||
|
|
||||||
|
# Druid 数据源 1 配置,继承spring.datasource.druid.* 配置,相同则覆盖
|
||||||
|
```properties
|
||||||
|
|
||||||
|
|
||||||
|
spring.datasource.druid.one.max-active=10
|
||||||
|
spring.datasource.druid.one.max-wait=10000
|
||||||
|
```
|
||||||
|
|
||||||
|
# Druid 数据源 2 配置,继承spring.datasource.druid.* 配置,相同则覆盖
|
||||||
|
```properties
|
||||||
|
spring.datasource.druid.two.max-active=20
|
||||||
|
spring.datasource.druid.two.max-wait=20000
|
||||||
|
```
|
||||||
|
|
||||||
|
强烈注意:**Spring Boot 2.X** 版本不再支持配置继承,多数据源的话每个数据源的所有配置都需要单独配置,否则配置不会生效
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
## MyBatis-Plus配置详解
|
||||||
|
- [官网文档](https://baomidou.com/introduce/)
|
||||||
|
|||||||
30
pom.xml
30
pom.xml
@@ -14,6 +14,8 @@
|
|||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||||
<spring-boot.version>3.5.9</spring-boot.version>
|
<spring-boot.version>3.5.9</spring-boot.version>
|
||||||
|
<druid.version>1.2.27</druid.version>
|
||||||
|
<mybatis-plus.version>3.5.15</mybatis-plus.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
|
|
||||||
@@ -40,7 +42,35 @@
|
|||||||
<artifactId>spring-boot-starter-test</artifactId>
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<!-- MySQL相关Maven依赖 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.mysql</groupId>
|
||||||
|
<artifactId>mysql-connector-j</artifactId>
|
||||||
|
<scope>runtime</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.baomidou</groupId>
|
||||||
|
<artifactId>mybatis-plus-spring-boot3-starter</artifactId>
|
||||||
|
<version>${mybatis-plus.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.baomidou</groupId>
|
||||||
|
<artifactId>mybatis-plus-jsqlparser</artifactId>
|
||||||
|
<version>${mybatis-plus.version}</version>
|
||||||
|
<scope>compile</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba</groupId>
|
||||||
|
<artifactId>druid-spring-boot-3-starter</artifactId>
|
||||||
|
<version>${druid.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<dependencyManagement>
|
<dependencyManagement>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
|
|||||||
@@ -1,9 +1,11 @@
|
|||||||
package com.yongfeng.lg.springboot3jdk17;
|
package com.yongfeng.lg.springboot3jdk17;
|
||||||
|
|
||||||
|
import org.mybatis.spring.annotation.MapperScan;
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
|
@MapperScan("com.yongfeng.lg.springboot3jdk17.mapper") // 必须配置,扫描mapper包
|
||||||
public class Application {
|
public class Application {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|||||||
@@ -0,0 +1,57 @@
|
|||||||
|
package com.yongfeng.lg.springboot3jdk17.controller;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||||
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||||
|
import com.yongfeng.lg.springboot3jdk17.entity.User;
|
||||||
|
import com.yongfeng.lg.springboot3jdk17.service.UserService;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/user")
|
||||||
|
public class UserController {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private UserService userService;
|
||||||
|
|
||||||
|
|
||||||
|
// 1. 新增用户
|
||||||
|
@PostMapping("/save")
|
||||||
|
public boolean save(@RequestBody User user) {
|
||||||
|
return userService.save(user);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. 根据ID查询用户
|
||||||
|
@GetMapping("/{id}")
|
||||||
|
public User getById(@PathVariable Long id) {
|
||||||
|
return userService.getById(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3. 查询所有用户
|
||||||
|
@GetMapping("/list")
|
||||||
|
public List<User> list() {
|
||||||
|
return userService.list();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 4. 分页查询用户 (MP分页插件生效)
|
||||||
|
@GetMapping("/page")
|
||||||
|
public Page<User> page(@RequestParam Integer current, @RequestParam Integer size) {
|
||||||
|
Page<User> page = new Page<>(current, size);
|
||||||
|
return userService.page(page);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 5. 条件查询:根据年龄查询
|
||||||
|
@GetMapping("/listByAge/{age}")
|
||||||
|
public List<User> listByAge(@PathVariable Integer age) {
|
||||||
|
LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
|
||||||
|
wrapper.gt(User::getAge, age); // 年龄大于指定值
|
||||||
|
return userService.list(wrapper);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 6. 删除用户
|
||||||
|
@DeleteMapping("/{id}")
|
||||||
|
public boolean remove(@PathVariable Long id) {
|
||||||
|
return userService.removeById(id);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,57 @@
|
|||||||
|
package com.yongfeng.lg.springboot3jdk17.entity;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.annotation.IdType;
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableField;
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableId;
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableName;
|
||||||
|
import com.yongfeng.lg.springboot3jdk17.enums.SexEnum;
|
||||||
|
import lombok.Data;
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 数据库表对应的实体类 (示例:用户表 user)
|
||||||
|
*/
|
||||||
|
@Data // lombok注解,自动生成get/set/toString/equals等方法
|
||||||
|
@TableName("user") // 对应数据库的表名
|
||||||
|
public class User implements Serializable {
|
||||||
|
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
// 自增主键
|
||||||
|
@TableId(type = IdType.AUTO)
|
||||||
|
private Long id;
|
||||||
|
|
||||||
|
// 用户名 (数据库字段:user_name)
|
||||||
|
@TableField("user_name")
|
||||||
|
private String userName;
|
||||||
|
|
||||||
|
// 密码
|
||||||
|
@TableField("password")
|
||||||
|
private String password;
|
||||||
|
|
||||||
|
// 年龄
|
||||||
|
@TableField("age")
|
||||||
|
private Integer age;
|
||||||
|
|
||||||
|
// 性别 使用枚举值
|
||||||
|
@TableField("sex")
|
||||||
|
private SexEnum sex;
|
||||||
|
|
||||||
|
|
||||||
|
// 邮箱
|
||||||
|
@TableField("email")
|
||||||
|
private String email;
|
||||||
|
|
||||||
|
// 创建时间
|
||||||
|
@TableField("create_time")
|
||||||
|
private LocalDateTime createTime;
|
||||||
|
|
||||||
|
// 更新时间
|
||||||
|
@TableField("update_time")
|
||||||
|
private LocalDateTime updateTime;
|
||||||
|
|
||||||
|
// 逻辑删除字段 (0=未删除,1=已删除)
|
||||||
|
@TableField("deleted")
|
||||||
|
private Integer deleted;
|
||||||
|
}
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
package com.yongfeng.lg.springboot3jdk17.enums;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.annotation.EnumValue;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* MyBatis-Plus使用枚举示例
|
||||||
|
*/
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@AllArgsConstructor
|
||||||
|
public enum SexEnum {
|
||||||
|
PRIMARY(1, "小学"),
|
||||||
|
SECONDARY(2, "中学"),
|
||||||
|
HIGH(3, "高中");
|
||||||
|
|
||||||
|
// 枚举属性使用 @EnumValue 注解,
|
||||||
|
// 指定枚举值在数据库中存储的实际值。
|
||||||
|
// 支持枚举类中的任意字段,如序号或编码。
|
||||||
|
// 标记数据库存的值是code
|
||||||
|
@EnumValue
|
||||||
|
private final int code;
|
||||||
|
|
||||||
|
private final String desc;
|
||||||
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
package com.yongfeng.lg.springboot3jdk17.mapper;
|
||||||
|
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||||
|
import com.yongfeng.lg.springboot3jdk17.entity.User;
|
||||||
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* MP的Mapper接口,继承BaseMapper即可,无需写任何方法
|
||||||
|
* BaseMapper<User> 中的泛型是对应的实体类
|
||||||
|
*/
|
||||||
|
//@Mapper // 可选,因为主启动类加了@MapperScan,二选一即可
|
||||||
|
public interface UserMapper extends BaseMapper<User> {
|
||||||
|
|
||||||
|
// 自定义的Mapper可以在这里面写,对应Mapper XML文件
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
package com.yongfeng.lg.springboot3jdk17.service;
|
||||||
|
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.extension.service.IService;
|
||||||
|
import com.yongfeng.lg.springboot3jdk17.entity.User;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 业务层接口,继承MP的IService
|
||||||
|
*/
|
||||||
|
public interface UserService extends IService<User> {
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
package com.yongfeng.lg.springboot3jdk17.service.impl;
|
||||||
|
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
|
import com.yongfeng.lg.springboot3jdk17.entity.User;
|
||||||
|
import com.yongfeng.lg.springboot3jdk17.mapper.UserMapper;
|
||||||
|
import com.yongfeng.lg.springboot3jdk17.service.UserService;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 业务层实现类,继承ServiceImpl,注入Mapper即可
|
||||||
|
*/
|
||||||
|
@Service
|
||||||
|
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,3 +1,53 @@
|
|||||||
# 端口号
|
# Web服务端口号
|
||||||
server.port=8080
|
server.port=8080
|
||||||
|
# 配置数据源类型为Druid
|
||||||
|
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
|
||||||
|
# druid相关配置文件
|
||||||
|
# MySQL8.x 驱动类,固定值;如果是MySQL5.7,改成 com.mysql.jdbc.Driver
|
||||||
|
spring.datasource.druid.driver-class-name=com.mysql.cj.jdbc.Driver
|
||||||
|
# 数据库连接地址
|
||||||
|
spring.datasource.druid.url=jdbc:mysql://10.232.112.35:3306/springboot3-jdk17?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=Asia/Shanghai
|
||||||
|
# 数据库名
|
||||||
|
spring.datasource.druid.username=root
|
||||||
|
# 数据库密码
|
||||||
|
spring.datasource.druid.password=-w9M627JZeYo5p^3lbH0mqDa
|
||||||
|
# 初始化连接数
|
||||||
|
spring.datasource.druid.initial-size=5
|
||||||
|
# 最小空闲连接数
|
||||||
|
spring.datasource.druid.min-idle=5
|
||||||
|
# 最大活跃连接数
|
||||||
|
spring.datasource.druid.max-active=20
|
||||||
|
# 获取连接的最大等待时间(ms)
|
||||||
|
spring.datasource.druid.max-wait=60000
|
||||||
|
# 检测空闲连接的间隔时间
|
||||||
|
spring.datasource.druid.time-between-eviction-runs-millis=60000
|
||||||
|
# 空闲连接的存活时间
|
||||||
|
spring.datasource.druid.min-evictable-idle-time-millis=300000
|
||||||
|
# 检测连接是否有效
|
||||||
|
spring.datasource.druid.validation-query=SELECT 1 FROM DUAL
|
||||||
|
# 空闲时检测连接有效性
|
||||||
|
spring.datasource.druid.test-while-idle=true
|
||||||
|
# 获取连接时不检测(提升性能)但是可能存在使用了废弃的会话
|
||||||
|
spring.datasource.druid.test-on-borrow=false
|
||||||
|
# 归还连接时不检测(提升性能)
|
||||||
|
spring.datasource.druid.test-on-return=false
|
||||||
|
# 开启PSCache
|
||||||
|
spring.datasource.druid.pool-prepared-statements=true
|
||||||
|
spring.datasource.druid.max-pool-prepared-statement-per-connection-size=20
|
||||||
|
|
||||||
|
# MyBatis-Plus配置文件
|
||||||
|
# 配置Mapper.xml映射文件的路径
|
||||||
|
mybatis-plus.mapper-locations=classpath:mapper/**/*.xml
|
||||||
|
# 配置实体类的包别名,简化Mapper中的实体类引用
|
||||||
|
mybatis-plus.type-aliases-package=com.example.demo.entity
|
||||||
|
# 主键策略:AUTO(自增),也可以用雪花算法 (分布式唯一ID,推荐)
|
||||||
|
mybatis-plus.global-config.db-config.id-type=AUTO
|
||||||
|
# 逻辑删除字段配置(可选)
|
||||||
|
mybatis-plus.global-config.db-config.logic-delete-field=deleted
|
||||||
|
# 配合逻辑删除字段 1代表删除
|
||||||
|
mybatis-plus.global-config.db-config.logic-delete-value=1
|
||||||
|
mybatis-plus.global-config.db-config.logic-not-delete-value=0
|
||||||
|
|
||||||
|
mybatis-plus.configuration.map-underscore-to-camel-case=true
|
||||||
|
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
|
||||||
|
mybatis-plus.configuration.cache-enabled=false
|
||||||
|
|||||||
Reference in New Issue
Block a user