本次代码结构都是通过 mybatis plus 代码生成器直接生成
因为 mybatis plus 在 service 层和在 mapper 层中封装的方法有部分差异,所以这里特别分开进行罗列
一、mybatis plus 在 service 中封装的常用的方法
package com.wxw.springboot_mybatisplus.sys.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 com.wxw.springboot_mybatisplus.sys.entity.SysUser;
import com.wxw.springboot_mybatisplus.sys.service.SysUserService;
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.HashMap;
import java.util.List;
import java.util.Map;
/**
* <p>
* 用户表 前端控制器
* </p>
*
* @author WXW
* @since 2020-10-23
*/
@RestController
@RequestMapping("/sys/sys-user")
public class SysUserController {
@Autowired
SysUserService sysUserService;
//分页查询
public void page() {
IPage<SysUser> page = sysUserService.page(
new Page<>(0, 5),
new QueryWrapper<SysUser>().lambda()// lambda 表达式格式的条件构造器
.eq(SysUser::getUserName,"admin")//查询名字等于 admin
.ne(SysUser::getUserName,"admin")//查询名字不等于 admin
);
page.getRecords();//对象集合
page.getTotal();//总条数
page.getCurrent();//当前页
page.getPages();//总页数
page.getSize();//每页条数
}
//list 查询
public void list() {
sysUserService.list(); //查询所有
sysUserService.list(new QueryWrapper<SysUser>().lambda() //查询list 带条件的
//这些条件都可以加判断,比如接收前端传入参数,可能为空,可能不为空
// .eq(判断条件,对象属性,参数)
.eq(1 == 1,SysUser::getUserName,"admin")//查询名字等于 admin
.ne(1 == 1,SysUser::getUserName,"admin")//查询名字不等于 admin
);
//根据 map 查询
Map<String,Object> map = new HashMap<>();
map.put("user_name","admin");
sysUserService.listByMap(map);
}
//插入
public void insert(SysUser sysUser) {
List<SysUser> list = new ArrayList<>();
sysUserService.save(sysUser);//插入单个对象
sysUserService.saveBatch(list);//批量插入
sysUserService.saveOrUpdate(sysUser);//插入或者修改单个对象
sysUserService.saveOrUpdateBatch(list);//批量插入或者修改单个对象
}
//修改
public void update(SysUser sysUser) {
List<SysUser> list = new ArrayList<>();
//根据条件修改
sysUserService.update(new QueryWrapper<SysUser>().lambda()
.eq(SysUser::getUserName,"admin")
);
sysUserService.updateById(sysUser);//根据 id 修改
}
//修改
public void delete(SysUser sysUser) {
//根据条件删除
sysUserService.remove(new QueryWrapper<SysUser>().lambda()
.eq(SysUser::getUserName,"admin")
);
sysUserService.removeById(sysUser);//根据 id 删除
List<Integer> list = new ArrayList<>();
list.add(1);
list.add(2);
sysUserService.removeByIds(list);//根据多个 id 删除
//根据map 条件 删除
Map<String,Object> map = new HashMap<>();
map.put("user_name","admin");
sysUserService.removeByMap(map);
}
//获取单个对象
public void getOne() {
//根据条件获取单个对象
sysUserService.getOne(new QueryWrapper<SysUser>().lambda()
.eq(SysUser::getUserName,"admin"));
}
//根据id 查询
public void selectById(SysUser sysUser) {
//根据条件获取单个对象
sysUserService.getById(sysUser.getId());
sysUserService.getById(sysUser);
}
//count
public void count(SysUser sysUser) {
//获取count
sysUserService.count();
//根据条件获取 count
sysUserService.count(new QueryWrapper<SysUser>().lambda()
.eq(SysUser::getUserName,"admin"));
}
}
二、mybatis plus 在 mapper 中封装的常用方法
package com.wxw.springboot_mybatisplus.sys.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.wxw.springboot_mybatisplus.sys.entity.SysUser;
import com.wxw.springboot_mybatisplus.sys.mapper.SysUserMapper;
import com.wxw.springboot_mybatisplus.sys.service.SysUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* <p>
* 用户表 服务实现类
* </p>
*
* @author WXW
* @since 2020-10-23
*/
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {
//分页查询
public void page() {
IPage<SysUser> page = baseMapper.selectPage(
new Page<>(0, 5),
new QueryWrapper<SysUser>().lambda()// lambda 表达式格式的条件构造器
.eq(SysUser::getUserName,"admin")//查询名字等于 admin
.ne(SysUser::getUserName,"admin")//查询名字不等于 admin
);
page.getRecords();//对象集合
page.getTotal();//总条数
page.getCurrent();//当前页
page.getPages();//总页数
page.getSize();//每页条数
}
//list 查询
public List<SysUser> list() {
baseMapper.selectList(new QueryWrapper<SysUser>()); //查询所有
//最大值查询(有时候我们需要自定义一些sql ,就可以使用这种写法)
QueryWrapper<SysUser> userQueryWrapper = new QueryWrapper<>();
userQueryWrapper.select("max(age) as total");
List<Map<String, Object>> list = baseMapper.selectMaps(userQueryWrapper);
list.get(0).get("total");
baseMapper.selectList(new QueryWrapper<SysUser>().lambda() //查询list 带条件的
//这些条件都可以加判断,比如接收前端传入参数,可能为空,可能不为空
// .eq(判断条件,对象属性,参数)
.eq(1 == 1,SysUser::getUserName,"admin")//查询名字等于 admin
.ne(1 == 1,SysUser::getUserName,"admin")//查询名字不等于 admin
);
//根据 map 查询
Map<String,Object> map = new HashMap<>();
map.put("user_name","admin");
baseMapper.selectByMap(map);
return null;
}
//插入
public void insert(SysUser sysUser) {
baseMapper.insert(sysUser);//插入单个对象
}
//修改
public void update(SysUser sysUser) {
//根据条件修改
baseMapper.update(sysUser,new QueryWrapper<SysUser>().lambda()
.eq(SysUser::getUserName,"admin")
);
baseMapper.updateById(sysUser);//根据 id 修改
}
//修改
public void delete(SysUser sysUser) {
//根据条件删除
baseMapper.delete(new QueryWrapper<SysUser>().lambda()
.eq(SysUser::getUserName,"admin")
);
baseMapper.deleteById(sysUser);//根据 id 删除
List<Integer> list = new ArrayList<>();
list.add(1);
list.add(2);
baseMapper.deleteBatchIds(list);//根据多个 id 删除
//根据map 条件 删除
Map<String,Object> map = new HashMap<>();
map.put("user_name","admin");
baseMapper.deleteByMap(map);
}
//获取单个对象
public void getOne() {
//根据条件获取单个对象
baseMapper.selectOne(new QueryWrapper<SysUser>().lambda()
.eq(SysUser::getUserName,"admin"));
}
//根据id 查询
public void selectById(SysUser sysUser) {
//根据条件获取单个对象
baseMapper.selectById(sysUser.getId());
baseMapper.selectById(sysUser);
}
//count
public void count(SysUser sysUser) {
//获取count
baseMapper.selectCount(new QueryWrapper<>());
//根据条件获取 count
baseMapper.selectCount(new QueryWrapper<SysUser>().lambda()
.eq(SysUser::getUserName,"admin"));
}
}
三、关于 mybatis plus 查询条件构造器
类似于上文用在查询语句中的 .eq 的方法
最后贴上 mybatis plus 官方地址
关注公众号查看更多资源