一、mybatis-plus
MyBatis-Plus (opens new window)(简称 MP)是一个 MyBatis (opens new window)的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。
二、实战
1、添加mysql依赖
<!--mysql驱动--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.26</version></dependency>
2、yml文件配置
spring:datasource:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/fcy_blogs?useUnicode=true&characterEncoding=UTF-8username: rootpassword: root
3、创建包名
4、mysql数据库创建表sys_user
CREATE TABLE `sys_user` (`user_id` bigint NOT NULL,`user_name` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL COMMENT '用户名',`password` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL COMMENT '密码',`real_name` varchar(15) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL COMMENT '姓名',`sex` char(1) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL COMMENT '性别(1-男 2-女)',`salt` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL COMMENT '随机盐',`phone` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL COMMENT '简介',`avatar` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL COMMENT '头像',`dept_id` bigint DEFAULT NULL COMMENT '部门ID',`lock_flag` char(1) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT '0' COMMENT '0-正常,9-锁定',`del_flag` char(1) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT '0' COMMENT '0-正常,1-删除',`create_time` datetime DEFAULT NULL COMMENT '创建时间',`update_time` datetime DEFAULT NULL COMMENT '修改时间',`create_by` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL COMMENT '创建者',`update_by` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL COMMENT '更新人',PRIMARY KEY (`user_id`) USING BTREE,KEY `user_idx1_username` (`user_name`) USING BTREE,KEY `user_id` (`user_id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin COMMENT='用户表';
5、pom文件引入mybatis-puls依赖
<!--mybatis-plus--><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.5.3.2</version></dependency>
6、在 Spring Boot 启动类中添加 @MapperScan
注解,扫描 Mapper 文件夹:
@SpringBootApplication
@MapperScan("com.example.springboot01.blogs.mapper")
public class Springboot01Application {public static void main(String[] args) {SpringApplication.run(Springboot01Application.class, args);}}
7、创建实体类SysUser
package com.example.springboot01.blogs.entity;import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableLogic;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;/*** <p>* 用户实体类* </p>** @author fcy* @since 2023-11-22*/
@Data
@TableName("sys_user")
public class SysUser extends BaseEntity{/*** 主键ID*/@TableId(value = "user_id", type = IdType.ASSIGN_ID)private Long userId;/*** 用户名*/private String userName;/*** 密码*/private String password;/*** 姓名*/private String realName;/*** 随机盐*/@JsonIgnoreprivate String salt;/*** 锁定标记*/private String lockFlag;/*** 手机号*/private String phone;/*** 头像*/private String avatar;/*** 部门ID*/private Long deptId;/*** 0-正常,1-删除*/@TableLogicprivate String delFlag;
}
package com.example.springboot01.blogs.entity;import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import lombok.Getter;
import lombok.Setter;import java.time.LocalDateTime;/*** <p>* 抽象实体* </p>** @author fcy* @since 2023-11-22*/
@Getter
@Setter
public class BaseEntity {/*** 创建者*/@TableField(fill = FieldFill.INSERT)private String createBy;/*** 创建时间*/@TableField(fill = FieldFill.INSERT)private LocalDateTime createTime;/*** 更新者*/@TableField(fill = FieldFill.INSERT_UPDATE)private String updateBy;/*** 更新时间*/@TableField(fill = FieldFill.INSERT_UPDATE)private LocalDateTime updateTime;
}
8、编写 Mapper 包下的 SysUserMapper接口
package com.example.springboot01.blogs.mapper;import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.springboot01.blogs.entity.SysUser;public interface SysUserMapper extends BaseMapper<SysUser> {
}
9、编写service包下的 SysUserService接口
package com.example.springboot01.blogs.service;import com.baomidou.mybatisplus.extension.service.IService;
import com.example.springboot01.blogs.entity.SysUser;/*** <p>* 实现接口* </p>** @author fcy* @since 2023-11-22*/
public interface SysUserService extends IService<SysUser> {
}
9、编写实现类
package com.example.springboot01.blogs.service.impl;import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.springboot01.blogs.entity.SysUser;
import com.example.springboot01.blogs.mapper.SysUserMapper;
import com.example.springboot01.blogs.service.SysUserService;
import org.springframework.stereotype.Service;/*** <p>* 实现类* </p>** @author fcy* @since 2023-11-22*/
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {}
10、控制类
package com.example.springboot01.blogs.controller;import com.example.springboot01.blogs.entity.SysUser;
import com.example.springboot01.blogs.service.SysUserService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import javax.annotation.Resource;
import java.util.List;/*** <p>* 控制类* </p>** @author fcy* @since 2023-11-22*/
@RestController
@RequestMapping("/user")
public class SysUserController {@Resourceprivate SysUserService sysUserService;@GetMapping("/list")public List<SysUser> getLists() {return sysUserService.list();}}
11、验证http://localhost:8090/user/list