plus的pom依赖:替代原mybatis
com.baomidoumybatis-plus2.1.9com.baomidoumybatisplus-spring-boot-starter1.0.5org.apache.tomcattomcat-jdbc
Controller层:
packagecom.cn.controller;importcom.cn.utils.BaU;importcom.cn.utils.Q;importcom.cn.utils.Rat;importlombok.extern.slf4j.Slf4j;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.web.bind.annotation.*;/**
* Created by Administrator on 2019/11/10 0010.
*/@Slf4j@RestController@RequestMapping("/development")publicclassExampleController{}
对象层
packagecom.cn.model.obj;importcom.baomidou.mybatisplus.activerecord.Model;importcom.baomidou.mybatisplus.annotations.TableField;importcom.baomidou.mybatisplus.annotations.TableId;importcom.baomidou.mybatisplus.annotations.TableName;importcom.baomidou.mybatisplus.enums.IdType;importlombok.Data;importjava.io.Serializable;/**
* Created by Administrator on 2019/11/10 0010.
*/@Data@TableName("gs_develop")publicclassExampleextendsModel{/**
* 主键ID
*/@TableId(type = IdType.AUTO)privateInteger id;/**
* 1、有效 -1、无效
*/privateInteger status;@TableField("create_id")privateInteger createId;@TableField("create_time")privateString createTime;@TableField("modify_id")privateInteger modifyId;@TableField("modify_time")privateString modifyTime;//审核 1=通过 2=驳回 3=待审核@TableField(exist =false)privateInteger checks;// 驳回理由@TableField(exist =false)privateString reject;privatestaticfinallongserialVersionUID =1L;@OverrideprotectedSerializablepkVal(){returnthis.id; }}
Mapper层:
packagecom.cn.mapper.fmapper;importcom.baomidou.mybatisplus.mapper.BaseMapper;importcom.baomidou.mybatisplus.plugins.Page;importorg.apache.ibatis.annotations.Mapper;importorg.apache.ibatis.annotations.Param;importjava.util.List;/**
* Created by Administrator on 2019/11/10 0010.
*/@MapperpublicinterfaceExampleMapperextendsBaseMapper{ListgetListPage(Page page, @Param("s")ExamplePo examplePo);}
xml:
SELECT s.id AS bid, s.* FROM gs_develop s WHERE 1=1 AND s.`status`=1 GROUP BY s.id
Service层:
packagecom.cn.fservice;importcom.baomidou.mybatisplus.plugins.Page;importcom.baomidou.mybatisplus.service.IService;/**
* Created by Administrator on 2019/11/10 0010.
*/publicinterfaceExampleServiceextendsIService{PagegetListPage(Page query, ExamplePo examplePo);}
IMpl:
packagecom.cn.service.impl;importcom.baomidou.mybatisplus.plugins.Page;importcom.baomidou.mybatisplus.service.impl.ServiceImpl;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Service;/**
* Created by Administrator on 2019/11/10 0010.
*/@ServicepublicclassExampleServiceImplextendsServiceImplimplementsExampleService{@AutowiredprivateExampleMapper exampleMapper;@OverridepublicPagegetListPage(Page query, ExamplePo examplePo){returnquery.setRecords(exampleMapper.getListPage(query, examplePo)); }}
操作方法:
/** * 分页 * *@paramdevelop *@return*/@DeleteMappingpublicQpage(@RequestBody ExamplePo develop, Q q){if(BaU.full(develop) && BaU.full(develop.getQ())) {returnq.r(exampleService.getListPage(BaU.qs(develop.getQ()), develop)); }returnq.f(); }/** * 增加 * *@paramdevelop *@return*/@PostMappingpublicQpost(@RequestBody ExamplePo develop, Q q){if(BaU.full(develop)) { develop.setStatus(Rat.NORMAL_STATUS); develop.setCreateId(infController.getStaff()); develop.setCreateTime(BaU.formatTime()); develop.setChecks(Rat.sh_z_id); exampleService.insert(develop);returnq.r(); }returnq.f(); }/** * 修改 * *@paramdevelop *@return*/@PutMappingpublicQput(@RequestBody ExamplePo develop, Q q){if(BaU.full(develop)) { develop.setStatus(Rat.NORMAL_STATUS); develop.setModifyTime(BaU.formatTime()); develop.setModifyId(infController.getStaff()); develop.setChecks(Rat.sh_z_id); exampleService.updateById(develop);returnq.r(); }returnq.f(); }/** * 获取一个 * *@paramid ID *@returnsuccess/fail */@GetMapping("/{id}")publicQget(@PathVariable Integer id){if(BaU.full(id)) {returnQ.rq(exampleService.selectById(id)); }returnQ.fq(); }/** * 删除一个 * *@paramid *@return*/@DeleteMapping("/{id}")publicQdeleteNoticeById(@PathVariable Integer id){if(BaU.full(id)) { Example develop =newExample(); develop.setId(id); develop.setStatus(Rat.DEL_STATUS); develop.setModifyId(infController.getStaff()); develop.setModifyTime(BaU.formatTime()); develop.setChecks(Rat.sh_bh_id); exampleService.updateById(develop);returnQ.rq(); }returnQ.fq(); }/** * 审核 * *@paramnotice *@return*/@PutMapping("/Q")publicQcheckNoticePage(@RequestBody ExamplePo notice, Q q){if(notice !=null&& notice.getId() !=null&& notice.getChecks() !=null) { Example develop =newExample(); develop.setId(notice.getId());if(Rat.sh_tg_id.equals(notice.getChecks())) {//通过develop.setChecks(Rat.sh_tg_id); }else{//驳回develop.setChecks(Rat.sh_bh_id); } develop.setStatus(Rat.NORMAL_STATUS); develop.setModifyId(infController.getStaff()); develop.setModifyTime(BaU.formatTime()); exampleService.updateById(develop);returnq.r(); }returnq.f(); }
批量新增方法:
/** * 新增 批量 * *@paramobjList *@return*/@PostMappingpublicRpost(@RequestBody List
批量修改:
/** * 批量 修改 * *@paramobjList *@return*/@PutMappingpublicQput(@RequestBody List
批量删除:
/** * 批量 删除 * *@paramobjList *@return*/@DeleteMappingpublicQput(@RequestBody List
批量查询:
/** * 批量 查询 * *@paramobjList *@return*/@PutMapping("/page")publicQput(@RequestBody Page objList){if(BaU.full(objList)) { Integer sta = Rat.NORMAL_STATUS;if(BaU.full(objList)) {returnspecialtyService.selectBatchIds(objList.getRecords());// specialtyService.selectPage(objList);}returnQ.rq(); }returnQ.fq(); }
复杂查询:
/** * 查询 * *@paramobj *@return*/@GetMapping("/byId")publicQput(@RequestParam Page obj){if(BaU.full(obj)) { Integer sta = Rat.NORMAL_STATUS;if(BaU.full(obj)) {//查询出某个对象 Specialty so =newSpecialty(); so.setId(Rat.int_first); Specialty sos= specialtyService.selectOne(newEntityWrapper<>(so));// 查询出 统计出符合某条件的对象数量Specialty sc =newSpecialty(); sc.setCreateId(Rat.sh_bh_id); Integer scs= specialtyService.selectCount(newEntityWrapper<>(sc));// 查询群体list Specialty s =newSpecialty(); s.setStatus(obj.getCurrent()); s.setCreateTime(sos.getModifyTime()); EntityWrapper ss =newEntityWrapper<>(s); ss.in("id", obj.getRecords());returnQ.rq(specialtyService.selectList(ss).get(scs)); }returnQ.rq(); }returnQ.fq(); }
mybatis plus 兼容mybatis ,可以写sql,可以调用内置方法,自动生成sql
内置增删改查接口都有基本注释,看了就知道如何使用。