赞
踩
设计一张表 客户表(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
个人习惯先写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); } }
再写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); }
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; } }
由于是mybatis-plus 所以mapper层不用我们写,但是启动类要加上@MapperScan(“com.example.demo.Mapper”)
自己测试,ok增删改查成功,时间也记录成功,感觉还是很快的。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。