CRUD
查询,分为三种,分页查询,条件查询,查询单条
增加,修改,删除
Mapper层
package com.haoguanjia.information.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.haoguanjia.information.pojo.Test;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
public interface TestMapper extends BaseMapper<Test> {
// 添加
@Select("INSERT INTO test (id, name, phone, idcard,address,money,create_time,update_time) VALUES " +
"(#{id}, #{name}, #{phone},#{idcard},#{address},#{money},#{createTime},#{updateTime}")
void insert(
@Param("id") Integer id,
@Param("name") Integer name,
@Param("phone") Integer phone,
@Param("idcard") Integer idcard,
@Param("address") Integer address,
@Param("money") Integer money,
@Param("createTime") Integer createTime,
@Param("updateTime") Integer updateTime,
);
Service层(接口)
package com.haoguanjia.information.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.haoguanjia.common.page.PageResult;
import com.haoguanjia.information.dto.TestDTO;
import com.haoguanjia.information.pojo.Test;
public interface TestService extends IService<Test> {
// 按id进行查询
TestDTO selectOne(Integer id);
// 查询全部
PageResult<TestDTO> selectAll(Integer page, Integer rows, String sortBy, Boolean desc);
// 条件搜索
PageResult<TestDTO> search(Integer page, Integer rows, String sortBy, Boolean desc, TestDTO testDTO);
// 添加
Boolean add(TestDTO testDTO);
//删除
Boolean delete(Integer id);
// 修改
Boolean update(TestDTO testDTO);
ServiceImpl层(类)
package com.haoguanjia.information.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.haoguanjia.common.page.PageResult;
import com.haoguanjia.common.utils.BeanHelper;
import com.haoguanjia.information.dto.TestDTO;
import com.haoguanjia.information.mapper.TestMapper;
import com.haoguanjia.information.pojo.Test;
import com.haoguanjia.information.service.TestService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;
@Service
public class TestServiceImpl extends ServiceImpl<TestMapper, Test> implements TestService {
@Autowired
private TestMapper mapper;
/**
* id查询
* @return
*/
@Override
public TestDTO selectOne(Integer id) {
Test test = this.getById(id);//按照id进行查询一条信息
TestDTO result = BeanHelper.copyProperties(test, TestDTO.class);//数据类型转换
return result;
}
/**
* 查询全部
* @param page
* @param rows
* @param sortBy
* @param desc
* @return
*/
@Override
public PageResult<TestDTO> selectAll(Integer page, Integer rows, String sortBy, Boolean desc) {
Page<Test> t = new Page<>(page, rows);//进行分页
if (StringUtils.isNotBlank(sortBy)) {//判定是否有值,要是有值进行排序,无值直接跳过
if (desc) {
t.setDesc(sortBy);
} else {
t.setAsc(sortBy);
}
}
IPage<Test> iPage = this.page(t);//获取当前页数
long total = iPage.getTotal();//获取总数
long totalPage = iPage.getPages();//获取页面
List<Test> records = iPage.getRecords();//获取记录数
List<TestDTO> result = BeanHelper.copyWithCollection(records, TestDTO.class);
return new PageResult<>(total, totalPage, result);
}
/**
* 条件查询
* @param page
* @param rows
* @param sortBy
* @param desc
* @param testDTO
* @return
*/
@Override
public PageResult<TestDTO> search(Integer page, Integer rows, String sortBy, Boolean desc, TestDTO testDTO) {
Page<Test> t = new Page<>(page, rows);
if (StringUtils.isNotBlank(sortBy)) {//判定是否有值,要是有值进行排序,无值直接跳过
if (desc) {
t.setDesc(sortBy);
} else {
t.setAsc(sortBy);
}
}
QueryWrapper<Test> where = new QueryWrapper<>();
if (testDTO.getName() != null) {
where.lambda().like(Test::getName, testDTO.getName());
}
if (testDTO.getPhone() != null) {
where.lambda().like(Test::getPhone, testDTO.getPhone());
}
IPage<Test> iPage = this.page(t, where);//选择分页
long total = iPage.getTotal();//总数
long totalPage = iPage.getPages();//获取页面
List<Test> records = iPage.getRecords();//获取记录
List<TestDTO> result = BeanHelper.copyWithCollection(records, TestDTO.class);
return new PageResult<>(total, totalPage, result);
}
/**
* 添加
* @return
*/
@Override
public Boolean add(TestDTO testDTO) {
Test test =BeanHelper.copyProperties(testDTO,Test.class);
test.setCreateTime(new Date());//后台进行设置取出创建时间
test.setUpdateTime(new Date());//后台进行设置取出设置更新
boolean result = this.save(test);//save内置添加方法
return result;
}
/**
* 删除
* @return
*/
@Override
public Boolean delete(Integer id) {
boolean result = this.removeById(id);//内置删除方法
return result;
}
/**
* 修改
* @param testDTO
* @return
*/
@Override
public Boolean update(TestDTO testDTO) {
/*数据类型转换*/
Test testOne = BeanHelper.copyProperties(testDTO, Test.class);
testOne.setUpdateTime(new Date());//更新时间
boolean result = this.updateById(testOne);//内置修改
return result;
}
}
}
Controller层
package com.haoguanjia.information.controller;
import com.haoguanjia.common.enums.ResultCodeEnum;
import com.haoguanjia.common.page.PageResult;
import com.haoguanjia.common.utils.Result;
import com.haoguanjia.information.dto.TestDTO;
import com.haoguanjia.information.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/test")
public class TestController {
@Autowired
private TestService service;
/**
* 查询一条数据
* @param id
* @return
*/
@GetMapping("/selectOne/{id}")
public ResponseEntity<Result> selectOne(@PathVariable("id") Integer id) {
TestDTO result = service.selectOne(id);//直接调用查询一条的方法
return ResponseEntity.ok(new Result(ResultCodeEnum.OK, result));
}
/**
* 查询全部
* @param page
* @param rows
* @param sortBy
* @param desc
* @return
*/
@GetMapping("/list")
public ResponseEntity<Result> list(
/*设置固定参数,第一页10条数据,sortBy进行排序,desc倒序*/
@RequestParam(name = "page", defaultValue = "1") Integer page,
@RequestParam(name = "rows", defaultValue = "10") Integer rows,
@RequestParam(name = "sortBy", required = false) String sortBy,
@RequestParam(name = "desc", required = false) Boolean desc
) {
PageResult<TestDTO> result = service.selectAll(page, rows, sortBy, desc);
return ResponseEntity.ok(new Result(ResultCodeEnum.OK, result));
}
/**
* 条件查询
* @param page
* @param rows
* @param sortBy
* @param desc
* @param testDTO
* @return
*/
@PutMapping("/search")
public ResponseEntity<Result> search(
/*设置固定参数,第一页10条数据,sortBy进行排序,desc倒序*/
@RequestParam(name = "page", defaultValue = "1") Integer page,
@RequestParam(name = "rows", defaultValue = "10") Integer rows,
@RequestParam(name = "sortBy", required = false) String sortBy,
@RequestParam(name = "desc", required = false) Boolean desc,
TestDTO testDTO
) {
PageResult<TestDTO> result = service.search(page, rows, sortBy, desc, testDTO);//进行条件查询直接调用
return ResponseEntity.ok(new Result(ResultCodeEnum.OK, result));//返回枚举类
}
/**
* 新增
* @param testDTO
* @return
*/
@PostMapping("/add")
public ResponseEntity<Result> add(TestDTO testDTO) {
Boolean result = service.add(testDTO);//直接调用方法进行新增
return ResponseEntity.ok(new Result(ResultCodeEnum.OK, result));
}
/**
* 删除一条数据
* @param id
* @return
*/
@DeleteMapping("/delete/{id}")
public ResponseEntity<Result> delete(@PathVariable("id") Integer id) {
Boolean result = service.delete(id);//直接调用方法进行删除
return ResponseEntity.ok(new Result(ResultCodeEnum.OK, result));
}
/**
* 修改一条数据
* @param testDTO
* @return
*/
@PutMapping("/update")
public ResponseEntity<Result> update(TestDTO testDTO) {
Boolean result = service.update(testDTO);//直接调用方法进行删除
return ResponseEntity.ok(new Result(ResultCodeEnum.OK, result));
}
}