当前位置:   article > 正文

增删改查(springboot+mybatis-plus)_springbootmybatisplus增删改查

springbootmybatisplus增删改查

设计一张表 客户表(customer)
1、字段包括(流水号、客户名称、客户性别、客户电话、客户年龄、客户地址);
2、实现新增、修改、删除、查询 功能;
3、删除需要支持 单条、批量;
4、新增功能需要支持 单条、批量导入;(记录导入1000条时间)

注意:
1、以上功能通过springboot实现,
2、只需要实现后端服务 ,不需要前端功能
3、工程中只保留示例项目代码

create table customer
(
id int not null
primary key,
name varchar(50) null,
sex char null,
tel varchar(11) null,
age int null,
address varchar(255) null
);
在这里插入图片描述
创建一个springboot项目,实现增删改查
(由于是单表,所以使用mybatis-plus
在这里插入图片描述

application.properties:

server.port=8021
#datasource
spring.datasource.url=jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=UTF-8&serverTimeZone=UTC
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl



  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

个人习惯先写controller层

package com.example.demo.Controller;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.example.demo.Pojo.Customer;
import com.example.demo.Service.CustomerService;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@RestController
@RequestMapping("api")
public class CustomerController {

    @Autowired
    private CustomerService customerService;

    //增加
    @PostMapping("add")
    public String AddCustomer(@RequestBody Customer customer){
        return customerService.AddCustomer(customer);
    }

    //批量增加
    @PostMapping("addDanList")
    public String AddList(@RequestBody List<Customer> customers){
        return customerService.AddList(customers);
    }

    //增加1000条数据
    @PostMapping("addList")
    public String AddList(@RequestParam MultipartFile file) throws IOException {
        InputStream in = file.getInputStream();
        ExcelReader excelReader= ExcelUtil.getReader(in);
        List<Customer> list = excelReader.readAll(Customer.class);
        return customerService.AddList(list);
    }

    //批量删除
    @DeleteMapping("deleteList")
    public String DeleteList(@RequestBody List<Integer> list){
        return customerService.DeleteList(list);
    }

    //修改
    @PutMapping("update")
    public String UpdateCustomer(@RequestBody Customer customer){
        return customerService.UpdateCustomer(customer);
    }

    //根据id删除
    @DeleteMapping("delete/{id}")
    public String DeleteCustomer(@PathVariable("id") int id){
        return customerService.DeleteById(id);
    }

    //根据id搜索
    @GetMapping("select/{id}")
    public Object SelectCustomer(@PathVariable("id") int id){
        return customerService.SelectById(id);
    }

    //根据id搜索
    @GetMapping("selectAll")
    public Object SelectCustomer(@RequestBody Customer customer){
        return customerService.SelectAll(customer);
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71

再写service 和 serviceImpl

package com.example.demo.Service;

import com.baomidou.mybatisplus.extension.service.IService;
import com.example.demo.Pojo.Customer;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: tsl
 * @Date: 2022/12/06/13:25
 * @Description:
 */

public interface CustomerService extends IService<Customer>{
    //添加
    String AddCustomer(Customer customer);

    //修改
    String UpdateCustomer(Customer customer);

    //根据id删除
    String DeleteById(int id);

    //根据id查询
    Object SelectById(int id);

    //批量添加
    String AddList(List<Customer> customers);

    //批量删除
    String DeleteList(List<Integer> list);

    //根据条件查询
    Object SelectAll(Customer customer);

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
package com.example.demo.Service.ServiceImpl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.Mapper.CustomerMapper;
import com.example.demo.Pojo.Customer;
import com.example.demo.Service.CustomerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;


@Service
@Slf4j
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper,Customer> implements CustomerService {

    @Resource
    private CustomerMapper customerMapper;

    //添加
    @Override
    public String AddCustomer(Customer customer) {
        if (this.save(customer)) {
            return "添加成功!";
        } else {
            return "添加失败!";
        }
    }

    //修改
    @Override
    public String UpdateCustomer(Customer customer) {
        LambdaQueryWrapper<Customer> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Customer::getId, customer.getId());
        if (this.update(customer, queryWrapper)) {
            return "修改成功!";
        } else {
            return "修改失败!";
        }
    }

    //根据id删除
    @Override
    public String DeleteById(int id) {
        if (this.removeById(id)) {
            return "删除成功!";
        } else {
            return "删除失败!";
        }
    }
    
    //根据id查询
    @Override
    public Object SelectById(int id) {
        Customer customer = customerMapper.selectById(id);
        Object json = JSON.toJSON(customer);
        if (json == null) {
            return "用户不存在!";
        } else {
            return json;
        }
    }

    //批量增加
    @Override
    public String AddList(List<Customer> customers) {
        log.info(customers.toString());
        long startTime = System.currentTimeMillis();
        boolean b = this.saveBatch(customers);
        long endTime = System.currentTimeMillis();
        if (b) {
            System.out.println("所需时间:" + (endTime - startTime));
            return "添加成功!";
        } else {
            return "添加失败!";
        }
    }

    //批量删除
    @Override
    public String DeleteList(List<Integer> list) {
        if (this.removeByIds(list)) {
            return "删除成功!";
        } else {
            return "删除失败!";
        }
    }

    //根据条件查询
    @Override
    public Object SelectAll(Customer customer) {
        LambdaQueryWrapper<Customer> queryWrapper = new LambdaQueryWrapper<>();
        if(customer.getId() != 0){
            queryWrapper.eq(Customer::getId,customer.getId());
        }
        if(StringUtils.isNotBlank(customer.getAddress())){
            queryWrapper.eq(Customer::getAddress,customer.getAddress());
        }
        if(StringUtils.isNotBlank(customer.getName())){
            queryWrapper.like(Customer::getName,customer.getName());
        }
        if(StringUtils.isNotBlank(customer.getSex())){
            queryWrapper.eq(Customer::getSex,customer.getSex());
        }
        if(StringUtils.isNotBlank(customer.getTel())){
            queryWrapper.eq(Customer::getTel,customer.getTel());
        }
        if(customer.getAge() != 0){
            queryWrapper.eq(Customer::getAge,customer.getAge());
        }

        List<Customer> customers = customerMapper.selectList(queryWrapper);
        if (customers.size() == 0){
            return "查询用户不存在!";
        }
        Object json = JSON.toJSON(customers);
        return json;
    }

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124

由于是mybatis-plus 所以mapper层不用我们写,但是启动类要加上@MapperScan(“com.example.demo.Mapper”)

自己测试,ok增删改查成功,时间也记录成功,感觉还是很快的。
在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/598323
推荐阅读
相关标签
  

闽ICP备14008679号