java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > MybatisPlus增删改查

MybatisPlus实现简单增删改查功能

作者:''''''''Beyond''''''''

这篇文章主要介绍了MybatisPlus实现简单增删改查的相关资料,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

实现步骤:

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

一、环境搭建

1.创建springboot项目

pom.xml

2.pom.xml : spring web、lombok mysql

<dependencies>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-devtools</artifactId>
   <scope>runtime</scope>
   <optional>true</optional>
  </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>

3.添加mybatisplus依赖

<dependency>
   <groupId>com.baomidou</groupId>
   <artifactId>mybatis-plus-boot-starter</artifactId>
   <version>3.3.1.tmp</version>
  </dependency>

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, 'test1@baomidou.com'),
(2, 'Jack', 20, 'test2@baomidou.com'),
(3, 'Tom', 28, 'test3@baomidou.com'),
(4, 'Sandy', 21, 'test4@baomidou.com'),
(5, 'Billie', 24, 'test5@baomidou.com');

代码详解

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<User> {

}

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<User> getAll(){
  List<User> 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<User> selectUserByIds(){
  List<User> 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实现简单增删改查的文章就介绍到这了,更多相关MybatisPlus增删改查内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文