目录
CREATE TABLE `sys_user` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键',
`create_time` datetime DEFAULT NULL COMMENT '创建时间',
`update_time` datetime DEFAULT NULL COMMENT '修改时间',
`del_flag` tinyint(1) DEFAULT '1' COMMENT '删除状态:1正常/2删除',
`user_id` varchar(255) DEFAULT NULL COMMENT '用户id',
`user_name` varchar(255) DEFAULT NULL COMMENT '用户名称',
PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC COMMENT='用户信息';
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!--它是 Spring Boot 应用程序的父项目,可以继承 Spring Boot 的默认参数、
默认配置和依赖管理,简化了项目的构建和配置工作。-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.5.RELEASE</version>
</parent>
<groupId>org.example</groupId>
<artifactId>mybatisplus1</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!--依赖版本控制-->
<lombok.version>1.18.16</lombok.version>
<mybatis-plus.version>3.4.1</mybatis-plus.version>
<druid.version>1.1.23</druid.version>
</properties>
<dependencies>
<!--spring boot相关依赖-->
<!--这个依赖项是 Spring Boot 的核心依赖,它包含了一系列常用的 Spring Boot 模块和库。
这个依赖项主要用于构建 Spring Boot 应用程序,它封装了许多常用的配置和依赖项,使得开发者
可以更快速地搭建和启动一个基于 Spring Boot 的项目。-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!--是用于编写测试代码的 Spring Boot 依赖项,它包含了一些常用的测试框架和工具。
这个依赖项的 scope 被设为 test,表示这些依赖项仅在测试阶段需要使用,不会被包含到最终的构建结果中。-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--这个依赖项是用于开发基于 Spring Boot 的 Web 应用程序的。它提供了一些基本的依赖项和配置,
以帮助你快速搭建一个 Web 应用程序。-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<scope>compile</scope>
</dependency>
<!--Lombok 是一个开发工具,可以通过注解自动化生成 Java 代码中的一些样板代码,
如 getter、setter、构造函数等,减少了编写冗余代码的工作量。-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</dependency>
<!--连接数据库-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!--Druid是一个开源的数据库连接池和监控框架,可以提供高性能、可扩展和可跟踪的数据库访问功能。
Druid还提供了丰富的监控和统计功能,可以对数据库连接、执行SQL、慢查询等进行监控和统计。-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>${druid.version}</version>
</dependency>
<!--mybatis plus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>${mybatis-plus.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<!-- 指定该Main Class为全局的唯一入口 这里是启动类的地址 -->
<mainClass>com.lpc.Application</mainClass>
<layout>ZIP</layout>
<includeSystemScope>true</includeSystemScope>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal><!--可以把依赖的包都打包到生成的Jar包中-->
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
package com.lpc;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @Author lpc
* @Date 2024 01 11 11 38
**/
@SpringBootApplication
@Slf4j
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
log.info("项目启动成功......");
}
}
server:
port: 8888
spring:
#连接数据库
datasource:
druid:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/mybatisplus_db?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowPublicKeyRetrieval=true
username: root
password: lpc123456
mybatis-plus:
configuration:
#在映射实体或者属性时,将数据库中表名和字段名中的下划线去掉,按照驼峰命名法映射
map-underscore-to-camel-case: true
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
global-config:
db-config:
id-type: ASSIGN_ID
package com.lpc.entity;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.format.annotation.DateTimeFormat;
import java.io.Serializable;
import java.util.Date;
/**
* @Author lpc
* @Date 2024 01 11 14 08
**/
@Data
@EqualsAndHashCode(callSuper = false)
public class SysUser implements Serializable {
/**
* 主键
*/
@TableId(value = "id", type = IdType.AUTO)
private Integer id;
/**
* 创建时间
*/
@JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd HH:mm:ss")
@DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
@TableField(fill = FieldFill.INSERT)
private Date createTime;
/**
* 更新时间
*/
@JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd HH:mm:ss")
@DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
@TableField(fill = FieldFill.INSERT_UPDATE)
private Date updateTime;
/**
* 删除状态默认1正常/2删除
*/
private Integer delFlag;
/**
* 用户id
*/
private String userId;
/**
* 用户名称
*/
private String userName;
}
package com.lpc.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.lpc.entity.SysUser;
import org.apache.ibatis.annotations.Mapper;
/**
* @Author lpc
* @Date 2024 01 11 14 19
**/
@Mapper
public interface SysUserMapper extends BaseMapper<SysUser> {
}
package com.lpc.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.lpc.entity.SysUser;
/**
* @Author lpc
* @Date 2024 01 11 14 20
**/
public interface SysUserService extends IService<SysUser> {
}
package com.lpc.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lpc.entity.SysUser;
import com.lpc.mapper.SysUserMapper;
import com.lpc.service.SysUserService;
import org.springframework.stereotype.Service;
/**
* @Author lpc
* @Date 2024 01 11 14 21
**/
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {
}
package com.lpc.common;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.lpc.enums.CodeEnum;
import lombok.Data;
import java.io.Serializable;
/**
* 接口返回数据格式
* @author scott
*/
@Data
public class Result<T> implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 成功标志
*/
private boolean success = true;
/**
* 返回处理消息
*/
private String message = "操作成功!";
/**
* 返回代码
*/
private Integer code = 0;
/**
* 返回数据对象 data
*/
private T result;
//返回数据
private T data;
/**
* 时间戳
*/
private long timestamp = System.currentTimeMillis();
public Result(Integer code, String message, T result) {
this.code=code;
this.message=message;
this.result=result;
}
public Result() {
}
public Result<T> success(String message) {
this.message = message;
this.code = CodeEnum.SUCCESS.getCode();
this.success = true;
return this;
}
@Deprecated
public static Result<Object> ok() {
Result<Object> r = new Result<Object>();
r.setSuccess(true);
r.setCode(CodeEnum.SUCCESS.getCode());
r.setMessage("成功");
return r;
}
@Deprecated
public static Result<Object> ok(String msg) {
Result<Object> r = new Result<Object>();
r.setSuccess(true);
r.setCode(CodeEnum.SUCCESS.getCode());
r.setMessage(msg);
return r;
}
@Deprecated
public static Result<Object> ok(Object data) {
Result<Object> r = new Result<Object>();
r.setSuccess(true);
r.setCode(CodeEnum.SUCCESS.getCode());
r.setResult(data);
return r;
}
public static<T> Result<T> OK() {
Result<T> r = new Result<T>();
r.setSuccess(true);
r.setCode(CodeEnum.SUCCESS.getCode());
r.setMessage("成功");
return r;
}
public static<T> Result<T> OK(T data) {
Result<T> r = new Result<T>();
r.setSuccess(true);
r.setCode(CodeEnum.SUCCESS.getCode());
r.setResult(data);
return r;
}
public static<T> Result<T> OK(String msg, T data) {
Result<T> r = new Result<T>();
r.setSuccess(true);
r.setCode(CodeEnum.SUCCESS.getCode());
r.setMessage(msg);
r.setResult(data);
return r;
}
public static Result error(String msg) {
return error(CodeEnum.FAILURE.getCode(), msg);
}
public static Result<Object> error(int code, String msg) {
Result<Object> r = new Result<Object>();
r.setCode(code);
r.setMessage(msg);
r.setSuccess(false);
return r;
}
public Result<T> error500(String message) {
this.message = message;
this.code = CodeEnum.FAILURE.getCode();
this.success = false;
return this;
}
@JsonIgnore
private String onlTable;
}
package com.lpc.enums;
/**
* @ClassName CodeEnum
* @Description 状态码
*/
public enum CodeEnum {
/**
* 处理成功
*/
SUCCESS(200, "成功"),
/**
* 处理失败
*/
FAILURE(422, "处理失败"),
/**
* 处理失败
*/
PERMISSION_DENIED(403, "权限不足"),
/**
* 门户二维码轮训请求:未登录成功
*/
QR_LOGIN_NOT_SUCCESS(1000, "未登录成功"),
/**
* 门户二维码轮训请求:二维码已过期
*/
QR_LOGIN_OVERDUE(1001, "二维码已过期,请重新刷新"),
/**
* 全局异常
*/
ERROR_1002(1002, "服务器错误,请联系管理员"),
/**
* 缺少必要参数
*/
ERROR_1004(1004, "缺少必要参数"),
/**
* 参数类型错误
*/
ERROR_1005(1005, "参数类型错误"),
/**
* 日期转换异常
*/
ERROR_1006(1006, "日期格式错误"),
/**
* 没有权限,请联系管理员授权
*/
SC_JEECG_NO_AUTHZ(510, "没有权限,请联系管理员授权"),
/**
* 数据库中已存在该记录
*/
ERROR_1007(1007, "数据库中已存在该记录"),
/**
* 路径不存在,请检查路径是否正确
*/
ERROR_404(404, "路径不存在,请检查路径是否正确"),
/**
* token失效
*/
ERROR_1008(1008, "Token失效,请重新登录!"),
/**
* 用户不存在
*/
ERROR_1009(1009, "用户不存在"),
/**
* 账号锁定
*/
ERROR_1010(1010, "账号锁定"),
/**
* 指纹登录轮训请求,未登录成功
*/
FINGERPRINT_LOGIN_NOT_SUCCESS(1011, "未登录成功");
private final Integer code;
private final String msg;
CodeEnum(Integer code, String msg) {
this.code = code;
this.msg = msg;
}
public Integer getCode() {
return code;
}
public String getMsg() {
return msg;
}
/**
* 根据 value 值反推 GlobalError
*
* @param value
* @return
*/
public static CodeEnum valueOf(int value) {
for (CodeEnum right : CodeEnum.values()) {
if (value == right.code) {
return right;
}
}
return null;
}
}
package com.lpc.config;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import java.util.Date;
/**
* @Author lpc
* @Date 2024 01 11 14 14
*
**/
@Component
@Slf4j
public class MybatisPlusConfig implements MetaObjectHandler {
/**
* 公共字段操作
* 插入的时候进行操作的两个字段createTime,updateTime (自动)
*/
@Override
public void insertFill(MetaObject metaObject) {
this.strictInsertFill(metaObject, "createTime", Date::new, Date.class);
this.strictUpdateFill(metaObject, "updateTime", Date::new, Date.class);
}
/**
*公共字段操作
* 修改更新的时候只操作updateTime这个字段(自动)
*/
@Override
public void updateFill(MetaObject metaObject) {
this.strictUpdateFill(metaObject, "updateTime", Date::new, Date.class);
}
/**
* 新的分页插件,一缓和二缓遵循mybatis的规则,需要设置 MybatisConfiguration#useDeprecatedExecutor = false 避免缓存出现问题(该属性会在旧插件移除后一同移除)
*/
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
}
package com.lpc.controller;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lpc.common.Result;
import com.lpc.entity.SysUser;
import com.lpc.service.SysUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
* @Author lpc
* @Date 2024 01 11 14 23
**/
@RequestMapping("/lpc/user")
@RequiredArgsConstructor
@RestController
public class SysUserController {
private final SysUserService userService;
/**
* 路径:localhost:8888/lpc/user/delete
*/
@PostMapping("/delete")
public Result<SysUser> delete(@RequestBody SysUser sysUser){
userService.update(Wrappers.lambdaUpdate(SysUser.class)
.eq(SysUser::getId,sysUser.getId())
.set(SysUser::getDelFlag,2)
);
return Result.OK();
}
}
package com.lpc.controller;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lpc.common.Result;
import com.lpc.entity.SysUser;
import com.lpc.service.SysUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
* @Author lpc
* @Date 2024 01 11 14 23
**/
@RequestMapping("/lpc/user")
@RequiredArgsConstructor
@RestController
public class SysUserController {
private final SysUserService userService;
/**
* 彻底删除记录
* 路径:localhost:8888/lpc/user/delete2
*/
@PostMapping("/delete2")
public Result<SysUser> delete2(Integer id){
userService.removeById(id);
return Result.OK();
}
}