mybatis-plus Mapper CRUD 接口和Service CRUD 接口使用(二)

参考文档:https://www.bookstack.cn/read/mybatis-plus-3.x/quickstart.md 

如果使用mybatis-plus Mapper CRUD 接口,则需要在dao层加上BaseMapper

public interface GoodDao extends BaseMapper {
}

 如果使用Service CRUD 接口,则需要在service层加上IService

public interface IGoodService extends IService {
}

Service实现类中展示的为mybatis-plus Mapper CRUD 的用法

package mptest.mybatistest.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 mptest.mybatistest.mapper.GoodDao;
import mptest.mybatistest.entity.Good;
import mptest.mybatistest.service.IGoodService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class GoodServiceImpl extends ServiceImpl implements IGoodService {
    @Autowired
    private GoodDao goodDao;
    //insert into tb (name) values('nameValue');
    public int insert(String name){
        Good po=new Good();
        po.setName(name);
        return  goodDao.insert(po);
    }
    //delete from tb where id=idValue;
    public int deleteByID(int id){
      return goodDao.deleteById(id);
    }
    //delete from tb where sex=1 and name='李四';
    public int deleteByMap(){
        Map columnMap=new HashMap<>();
        columnMap.put("sex",1);
        columnMap.put("name","李四");
        return goodDao.deleteByMap(columnMap);
    }
    //delete from tb where name="李六";
    public int delete(){
        return goodDao.delete(new QueryWrapper().eq("name","李六"));
    }
    //delete from tb where id in(1,3);
    public int deleteBatchIds(){
        List list = new ArrayList();
        list.add(1);
        list.add(3);
        return  goodDao.deleteBatchIds(list);
    }
    //update tb set sex=0 where id=6;
    public int updateById(){
        Good good=new Good();
        good.setId(6);
        good.setSex(0);
        return  goodDao.updateById(good);
    }
    //update tb set sex=1 where name='七七';
    public int update1(){
        Good good=new Good();
        good.setSex(1);
        return goodDao.update(good,new QueryWrapper().eq("name","七七"));
    }
    //同delete
    public Good selectById(){
       return goodDao.selectById(6);
    }
    //同delete
    public List selectBatchIds(){
        List list = new ArrayList();
        list.add(1);
        list.add(3);
        return goodDao.selectBatchIds(list);
    }
    //同delete
    public List selectByMap(){
        Map columnMap=new HashMap<>();
        columnMap.put("sex",1);
        columnMap.put("name","李四");
        return goodDao.selectByMap(columnMap);
    }
    //如果逻辑非唯一该方法不会自动替您 limit 1 你需要 wrapper.last("limit 1") 设置唯一性。
    public Good selectOne(){
        return goodDao.selectOne(new QueryWrapper().eq("name","七七").last("limit 1"));
    }
    //记录数
    public int selectCount(){
        return goodDao.selectCount(new QueryWrapper().eq("sex","1"));
    }
    //查全部
    public List selectList(){
        return goodDao.selectList(new QueryWrapper());
    }
    //同上但可自定义增加参数
    public List selectMaps(){
        List> list=goodDao.selectMaps(new QueryWrapper());
        for(Map m:list){
       m.put("zidingyi","aaa");
        }
        return list;
    }
    //只返回第一个字段的值
    public List selectObjs(){
        return goodDao.selectObjs(new QueryWrapper());
    }
    //分页 select * from tb limit 0,2
    public void selectPage(){
        Page page = new Page<>(1,2);
       IPage iPage= goodDao.selectPage(page,new QueryWrapper());
    }
    public void selectMapsPage(){
        Page page = new Page<>(1,2);
        IPage iPage= goodDao.selectMapsPage(page,new QueryWrapper());
    }
}

controller层中展示的是Service CRUD 的用法

​
package mptest.mybatistest.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import mptest.mybatistest.entity.Good;
import mptest.mybatistest.service.IGoodService;
import net.sf.json.JSON;
import net.sf.json.JSONObject;
import net.sf.json.JSONString;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/work")
public class GoodsController {

    @Autowired
    private IGoodService goodService;
    @RequestMapping("/save")
    //update 根据 updateWrapper 条件,更新记录
    //updateBatchById 根据ID 批量更新
    //insert into tb (name,sex) values('李玉',1);
    public boolean save(){
        Good good=new Good();
        good.setSex(1);
        good.setName("李玉");
        return goodService.save(good);
    }
    @RequestMapping("/saveBatch")
    public boolean saveBatch(){
        Good good1=new Good();
        good1.setName("李玉1");
        good1.setSex(1);
        Good good2=new Good();
        good2.setName("李玉2");
        good2.setSex(1);
        List list =new ArrayList();
        list.add(good1);
        list.add(good2);
        //insert into tb (name,sex) values('李玉',1); 执行多条
        goodService.saveBatch(list);
        //insert into tb (name,sex) values('李玉',1); 执行多条,每次执行5个
        goodService.saveBatch(list,5);
        return true;
    }
    @RequestMapping("/saveOrUpdateBatch")
    //saveOrUpdate 则修改,无则新增 一条
    //saveOrUpdateBatch有则修改,无则新增,多条 根据主键ID判断
    public boolean saveOrUpdateBatch(){
        Good good1=new Good();
        good1.setName("李玉3");
        good1.setSex(0);
        good1.setId(5);
        Good good2=new Good();
        good2.setName("李玉2");
        good2.setSex(1);
        List list =new ArrayList();
        list.add(good1);
        list.add(good2);
        return goodService.saveOrUpdateBatch(list);
    }
    //removeById 根据主键删
    //removeByMap 根据表字段map对象删
    //removeByIds 根据主键批量删除
    //remove 根据QueryWrapper删除
    @RequestMapping("/remove")
    public boolean  remove(){
      return goodService.remove(new QueryWrapper().eq("name","李玉2"));
    }
    //getById 根据 ID 查询
    //listByIds 根据ID 批量查询
    //listByMap 根据表字段map对象
    //getOne 根据 Wrapper,查询一条记录
    //getMap 根据 Wrapper,查询一条记录
    //getObj 根据 Wrapper,查询一条记录(有问题?)
    //count 根据 Wrapper 条件,查询总记录数
    //list 查询列表
    //listMaps 查询列表 List>
    //listObjs 根据 Wrapper 条件,查询全部记录
    @RequestMapping("/getObj")
    public Good getOne(){
        return goodService.getOne(new QueryWrapper().eq("name","李玉2").last("limit 1,1"));
    }
    //page 翻页查询
    //pageMaps 翻页查询
    @RequestMapping("/page")
    public String page(){
        Page page = new Page<>(1,3);
        IPage iPage= goodService.page(page,new QueryWrapper());
        return JSONObject.fromObject(iPage).toString();
    }
}

​

 

你可能感兴趣的:(mybatis-plus,mybatis-plus)