MybatisPlus实现简单增删改查功能

实现步骤:

工具:IDEA
数据库版本:mysql5.7

一、环境搭建

1.创建springboot项目

pom.xml

2.pom.xml : spring web、lombok mysql


  
   org.springframework.boot
   spring-boot-starter-web
  
  
   org.springframework.boot
   spring-boot-devtools
   runtime
   true
  
  
   mysql
   mysql-connector-java
   runtime
  
  
   org.projectlombok
   lombok
   true
  
  
   org.springframework.boot
   spring-boot-starter-test
   test
  
 

3.添加mybatisplus依赖


   com.baomidou
   mybatis-plus-boot-starter
   3.3.1.tmp
  

application.properties

4.application.properties配置文件:用户名、密码、连接驱动、配置日志

注意:mysql8 需要增加时区的配置 serverTimezone=GMT%2B8 东八区

#mysql5 驱动不同
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.url=jdbc:mysql://localhost:3306/mybatisplus?useSSL=false&useUnicode=true&characterEncoding=utf-8
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

#配置日志
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

数据库数据

#建表
DROP TABLE IF EXISTS user;

CREATE TABLE user
(
	id BIGINT(20) NOT NULL COMMENT '主键ID',
	name VARCHAR(30) NULL DEFAULT NULL COMMENT '姓名',
	age INT(11) NULL DEFAULT NULL COMMENT '年龄',
	email VARCHAR(50) NULL DEFAULT NULL COMMENT '邮箱',
	PRIMARY KEY (id)
);

#插入数据
DELETE FROM user;

INSERT INTO user (id, name, age, email) VALUES
(1, 'Jone', 18, '[email protected]'),
(2, 'Jack', 20, '[email protected]'),
(3, 'Tom', 28, '[email protected]'),
(4, 'Sandy', 21, '[email protected]'),
(5, 'Billie', 24, '[email protected]');

代码详解

1.新建实体类User
lombok注解详解:
@Data:自动生成get\set方法
@AllArgsConstructor :有参方法
@NoArgsConstructor:无参方法
@TableId(value = “id”,type = IdType.AUTO) :给实体设置自增

package com.sh.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.context.annotation.Primary;

import java.util.Date;

@Data
@AllArgsConstructor
@NoArgsConstructor

public class User {
 @TableId(value = "id",type = IdType.AUTO)
 private Integer id;
 private String name;
 private Integer age;
 private String email;
 private Date createTime;
 private Date updateTime;
}

2. 新建mapper包
在mapper包里建UserMappe接口r 继承BaseMapper<实体类>

package com.sh.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.sh.entity.User;
import org.springframework.stereotype.Repository;

@Repository
public interface UserMapper extends BaseMapper {

}

3.创建控制层controller:
新建UserController类

package com.sh.controller;

import com.sh.entity.User;
import com.sh.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RestController
public class UserController {
 @Autowired
 private UserMapper userMapper;
 //查询 查询所以 null 就是没有条件
 @GetMapping("/selectUser")
 public List getAll(){
  List user = userMapper.selectList(null);
  return user;
 }
 //根据id查询
 @GetMapping("/selectById")
 public User selectUserById(){
  User user = userMapper.selectById(1);
  return user;
 }
 //根据多个id查询 Arrays.asList集合
 @GetMapping("/selectByIds")
 public List selectUserByIds(){
  List user = userMapper.selectBatchIds(Arrays.asList(1,2,3));
  return user;
 }
 //添加
 @PostMapping("/insertUser")
 public Integer insertUsers(User user){
  Integer result = userMapper.insert(user);
  return result;
 }
 //修改 根据id
 @PutMapping("/updateUser")
 public Integer updateUsers(User user){
  Integer result = userMapper.updateById(user);
  return result;
 }
 //删除 根据id
 @DeleteMapping("/deleteUser")
 public Integer deleteUsers(Integer id){
  Integer result = userMapper.deleteById(id);
  return result;
 }
}
项目结构

MybatisPlus实现简单增删改查功能_第1张图片

注意:添加,修改操作需要数据库和实体设置自增

到此这篇关于MybatisPlus实现简单增删改查的文章就介绍到这了,更多相关MybatisPlus增删改查内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

你可能感兴趣的:(MybatisPlus实现简单增删改查功能)