plus的pom依赖:替代原mybatis

<!--mybatis plus-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus</artifactId>
            <version>2.1.9</version>
        </dependency>
 
        <!-- mybatis plus start -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatisplus-spring-boot-starter</artifactId>
            <version>1.0.5</version>
            <exclusions>
                <exclusion>
                    <groupId>org.apache.tomcat</groupId>
                    <artifactId>tomcat-jdbc</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

Controller层:

package com.cn.controller;

import com.cn.utils.BaU;
import com.cn.utils.Q;
import com.cn.utils.Rat;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Created by Administrator on 2019/11/10 0010.
 */
@Slf4j
@RestController
@RequestMapping("/development")
public class ExampleController {

}

对象层

package com.cn.model.obj;

import com.baomidou.mybatisplus.activerecord.Model;
import com.baomidou.mybatisplus.annotations.TableField;
import com.baomidou.mybatisplus.annotations.TableId;
import com.baomidou.mybatisplus.annotations.TableName;
import com.baomidou.mybatisplus.enums.IdType;
import lombok.Data;

import java.io.Serializable;

/**
 * Created by Administrator on 2019/11/10 0010.
 */
@Data
@TableName("gs_develop")
public class Example extends Model<Example> {

    /**
     * 主键ID
     */
    @TableId(type = IdType.AUTO)
    private Integer id;

    /**
     * 1、有效    -1、无效
     */
    private Integer status;

    @TableField("create_id")
    private Integer createId;
    @TableField("create_time")
    private String createTime;
    @TableField("modify_id")
    private Integer modifyId;
    @TableField("modify_time")
    private String modifyTime;

    //审核 1=通过  2=驳回  3=待审核
    @TableField(exist = false)
    private Integer checks;

    //    驳回理由
    @TableField(exist = false)
    private String reject;

    private static final long serialVersionUID = 1L;

    @Override
    protected Serializable pkVal() {
        return this.id;
    }


}

 

Mapper层:

package com.cn.mapper.fmapper;

import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.baomidou.mybatisplus.plugins.Page;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.util.List;

/**
 * Created by Administrator on 2019/11/10 0010.
 */
@Mapper
public interface ExampleMapper extends BaseMapper<Example> {

    List<ExamplePo> getListPage(Page page, @Param("s") ExamplePo examplePo);
}

xml:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.cn.mapper.ExampleMapper">

    <select id="getListPage" parameterType="com.cn.model.pojo.ExamplePo"
            resultType="com.cn.model.pojo.ExamplePo">
        SELECT
        s.id AS bid,
        s.*
        FROM gs_develop s
        WHERE 1=1
        AND s.`status`=1
        GROUP BY s.id
    </select>
 
</mapper>

 

Service层:

package com.cn.fservice;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.IService;

/**
 * Created by Administrator on 2019/11/10 0010.
 */
public interface ExampleService extends IService<Example> {

    Page getListPage(Page query, ExamplePo examplePo);
}

IMpl:

package com.cn.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by Administrator on 2019/11/10 0010.
 */
@Service
public class ExampleServiceImpl extends ServiceImpl<ExampleMapper, Example> implements ExampleService {

    @Autowired
    private ExampleMapper exampleMapper;

    @Override
    public Page getListPage(Page query, ExamplePo examplePo) {


        return query.setRecords(exampleMapper.getListPage(query, examplePo));
    }

}

 

操作方法:

/**
     * 分页
     *
     * @param develop
     * @return
     */
    @DeleteMapping
    public Q page(@RequestBody ExamplePo develop, Q q) {
        if (BaU.full(develop) && BaU.full(develop.getQ())) {

            return q.r(exampleService.getListPage(BaU.qs(develop.getQ()), develop));

        }

        return q.f();
    }

    /**
     * 增加
     *
     * @param develop
     * @return
     */
    @PostMapping
    public Q post(@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);

            return q.r();
        }
        return q.f();
    }

    /**
     * 修改
     *
     * @param develop
     * @return
     */
    @PutMapping
    public Q put(@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);

            return q.r();
        }
        return q.f();
    }


    /**
     * 获取一个
     *
     * @param id ID
     * @return success/fail
     */
    @GetMapping("/{id}")
    public Q get(@PathVariable Integer id) {
        if (BaU.full(id)) {

            return Q.rq(exampleService.selectById(id));
        }
        return Q.fq();
    }

    /**
     * 删除一个
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Q deleteNoticeById(@PathVariable Integer id) {
        if (BaU.full(id)) {
            Example develop = new Example();
            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);

            return Q.rq();
        }
        return Q.fq();
    }


    /**
     * 审核
     *
     * @param notice
     * @return
     */
    @PutMapping("/Q")
    public Q checkNoticePage(@RequestBody ExamplePo notice, Q q) {
        if (notice != null && notice.getId() != null && notice.getChecks() != null) {
            Example develop = new Example();
            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);

            return q.r();
        }
        return q.f();
    }

 

批量新增方法:

/**
     * 新增 批量
     *
     * @param objList
     * @return
     */
    @PostMapping
    public R post(@RequestBody List<Obj> objList) {
        if (BaU.full(objList)) {
            Integer sta = Rat.NORMAL_STATUS;
            Integer staff = Rat.getStaff;

            for (Obj obj: objList) {


                if (obj!= null) {

                    obj.setStatus(sta);
                    obj.setCreateId(staff);
                    obj.setCreateTime(BaU.formatTime());

// 加入时间戳
                    obj.setModifyTime(BaU.currentRan());
                   

                }
            }
            if (BaU.full(objList)) {
                specialtyService.insertBatch(objList);
            }
            return new R(true, Rat.Result_ok);
        }
        return new R(false, Rat.Params_err + "[ ]");
    }

批量修改:

/**
     * 批量 修改
     *
     * @param objList
     * @return
     */
    @PutMapping
    public Q put(@RequestBody List<Obj> objList) {
        if (BaU.full(objList)) {
            Integer sta = Rat.NORMAL_STATUS;
            Integer staff = Rat.getStaff;

            for (Obj obj: objList) {


                if (obj!= null) {

                    obj.setStatus(sta);
                    obj.setModifyId(staff);
                    obj.setModifyTime(BaU.formatTime());

                    // 加入时间戳
                    obj.setModifyTime(BaU.currentRan());


                }
            }
            if (BaU.full(objList)) {
                specialtyService.updateBatchById(objList);
            }
            return Q.rq();
        }
        return Q.fq();
    }

批量删除:

/**
     * 批量 删除
     *
     * @param objList
     * @return
     */
    @DeleteMapping
    public Q put(@RequestBody List<Integer> objList) {
        if (BaU.full(objList)) {
            Integer sta = Rat.NORMAL_STATUS;

             
            if (BaU.full(objList)) {
                specialtyService.deleteBatchIds(objList);
            }
            return Q.rq();
        }
        return Q.fq();
    }

批量查询:

/**
     * 批量 查询
     *
     * @param objList
     * @return
     */
    @PutMapping("/page")
    public Q put(@RequestBody Page objList) {
        if (BaU.full(objList)) {
            Integer sta = Rat.NORMAL_STATUS;


            if (BaU.full(objList)) {
              return   specialtyService.selectBatchIds(objList.getRecords());
//                specialtyService.selectPage(objList);
            }
            return Q.rq();
        }
        return Q.fq();
    }

 

复杂查询:

/**
     * 查询
     *
     * @param obj
     * @return
     */
    @GetMapping("/byId")
    public Q put(@RequestParam Page obj) {
        if (BaU.full(obj)) {
            Integer sta = Rat.NORMAL_STATUS;


            if (BaU.full(obj)) {
                
                //查询出某个对象 
                Specialty so = new Specialty();
                so.setId(Rat.int_first);
                Specialty sos= specialtyService.selectOne(new EntityWrapper<>(so));
                
// 查询出 统计出符合某条件的对象数量
                Specialty sc = new Specialty();
                sc.setCreateId(Rat.sh_bh_id);
                Integer scs= specialtyService.selectCount(new EntityWrapper<>(sc));
                
// 查询群体list 
                Specialty s = new Specialty();
                s.setStatus(obj.getCurrent());
                s.setCreateTime(sos.getModifyTime());
                EntityWrapper<Specialty> ss = new EntityWrapper<>(s);
                ss.in("id", obj.getRecords());
                return Q.rq(specialtyService.selectList(ss).get(scs));
            }
            return Q.rq();
        }
        return Q.fq();
    }

 mybatis plus 兼容mybatis ,可以写sql,可以调用内置方法,自动生成sql

内置增删改查接口都有基本注释,看了就知道如何使用。