这个最主要的是构造方法的使用和EntityUtils工具类的使用
/**
* 实现部门Do转部门Vo
* @param dept
*/
public Deptvo(Dept dept) {
super(dept);
}
一对一查询
单条记录
场景一:单条记录查询
一对一查询最典型的应用场景是将
id
替换成name
,比如将userId
替换成userName
。
@Override
public UserVo getOneUser(Integer userId) {
LambdaQueryWrapper<User> userWrapper = Wrappers.lambdaQuery(User.class).eq(User::getUserId, userId);
User user = getOne(userWrapper);
UserVo userVo = EntityUtils.toObj(user, UserVo::new);
Optional.ofNullable(userVo).ifPresent(this::addDeptNameInfo);
return userVo;
}
/**
* 补充部门名称信息
*/
private void addDeptNameInfo(UserVo userVo) {
LambdaQueryWrapper<Dept> deptWrapper = Wrappers.lambdaQuery(Dept.class).eq(Dept::getDeptId, userVo.getDeptId());
Dept dept = deptService.getOne(deptWrapper);
Optional.ofNullable(dept).ifPresent(d -> userVo.setDeptName(d.getDeptName()));
}
列表查询
场景一:无条件数据查询
/**
* 无条件数据查询
*
* @return
*/
@Override
public List<UserVo> selectUserList() {
//1.获取用户信息
List<User> userList = this.list();
//2. 将其存至将要返回的Vo中
List<UserVo> userVos = EntityUtils.toList(userList, UserVo::new);
//3. 将其vo中的部门Id查出至set集合中
Set<Long> deptIds = EntityUtils.toSet(userVos, User::getDeptId);
//4. 构造根据id查询出所属部门的plus 的wrapper
//此处in内参数理解为where后的deptId in '1,2...'
// 此处wrapper后可链式调用select方法,确定放回的字段
//LambdaQueryWrapper<Dept> deptWrapper =
// Wrappers.lambdaQuery(Dept.class).in(Dept::getDeptId, deptIds).select(Dept::getDeptId,
// Dept::getDeptName, Dept::getStaff, Dept::getTel);
LambdaQueryWrapper<Dept> deptWrapper = Wrappers.lambdaQuery(Dept.class).in(Dept::getDeptId, deptIds);
// 5. 调用plus查询获取jion表list
List<Dept> depts = deptMapper.selectList(deptWrapper);
//6. 将depts转成map形式为后续for循环做准备
Map<Long, Dept> deptMap = EntityUtils.toMap(depts, Dept::getDeptId, d -> d);
for (UserVo userVo : userVos) {
Dept dept = deptMap.get(userVo.getDeptId());
userVo.setDeptName(dept.getDeptName());
userVo.setStaff(dept.getStaff());
userVo.setTel(dept.getTel());
}
return userVos;
}
场景二:有条件的数据查询
/**
* 选择用户用于
* 有条件数据查询,条件在用户表里面
* 年龄大于1岁的用户
*
* @return {@link List}<{@link UserVo}>
*/
@Override
public List<UserVo> selectUserList2() {
//0. 构造条件
LambdaQueryWrapper<User> gt = Wrappers.lambdaQuery(User.class).gt(User::getAge, 1);
//1.获取用户信息
List<User> userList = this.list(gt);
//2. 将其存至将要返回的Vo中
List<UserVo> userVos = EntityUtils.toList(userList, UserVo::new);
//3. 将其vo中的部门Id查出至set集合中
Set<Long> deptIds = EntityUtils.toSet(userVos, User::getDeptId);
//4. 构造根据id查询出所属部门的plus 的wrapper
//此处in内参数理解为where后的deptId in '1,2...'
// 此处wrapper后可链式调用select方法,确定放回的字段
//LambdaQueryWrapper<Dept> deptWrapper =
// Wrappers.lambdaQuery(Dept.class).in(Dept::getDeptId, deptIds).select(Dept::getDeptId,
// Dept::getDeptName, Dept::getStaff, Dept::getTel);
LambdaQueryWrapper<Dept> deptWrapper = Wrappers.lambdaQuery(Dept.class).in(Dept::getDeptId, deptIds);
// 5. 调用plus查询获取jion表list
List<Dept> depts = deptMapper.selectList(deptWrapper);
//6. 将depts转成map形式为后续for循环做准备
Map<Long, Dept> deptMap = EntityUtils.toMap(depts, Dept::getDeptId, d -> d);
for (UserVo userVo : userVos) {
Dept dept = deptMap.get(userVo.getDeptId());
userVo.setDeptName(dept.getDeptName());
userVo.setStaff(dept.getStaff());
userVo.setTel(dept.getTel());
}
return userVos;
}
场景三:有条件数据查询,条件在用户表里面
/**
* 有条件数据查询,条件在用户表里面
* 部门名称是tomcat的用户
*
* @return
*/
@Override
public List<UserVo> selectUserList3() {
//0. 构造条件 构造根据id查询出所属部门的plus 的wrapper
LambdaQueryWrapper<Dept> deptWrapper = Wrappers.lambdaQuery(Dept.class).eq(Dept::getDeptName, "tomcat");
//1.获取筛选后部门信息
List<Dept> deptsList = deptMapper.selectList(deptWrapper);
//2. 将depts转成map形式为后续for循环做准备
Map<Long, Dept> deeptMap = EntityUtils.toMap(deptsList, Dept::getDeptId, d -> d);
//3. 将其depts中的部门Id查出至set集合中
Set<Long> deptIds = EntityUtils.toSet(deptsList, Dept::getDeptId);
LambdaQueryWrapper<User> userWrapper = Wrappers.lambdaQuery(User.class).in(User::getDeptId, deptIds);
List<User> userList = this.list(userWrapper);
List<UserVo> userVos = EntityUtils.toList(userList, UserVo::new);
for (UserVo userVo : userVos) {
Dept dept = deeptMap.get(userVo.getDeptId());
userVo.setStaff(dept.getStaff());
userVo.setDeptName(dept.getDeptName());
userVo.setTel(dept.getTel());
}
return userVos;
}
分页查询
场景一: 无任何筛选条件分页查询
- 用户表关联部门表
/**
* 分页
* 场景一: 无任何筛选条件分页查询用户表关联部门表
*/
@Override
public IPage<UserVo> selectUserPage1() {
Page<User> page = new Page<>(1, 2);
Page<User> userPage = this.page(page);
IPage<UserVo> userVoIPage = EntityUtils.toPage(userPage, UserVo::new);
Set<Long> deptIds = EntityUtils.toSet(userVoIPage.getRecords(), UserVo::getDeptId);
if (deptIds.size() > 0) {
LambdaQueryWrapper<Dept> deptWrapper = Wrappers.lambdaQuery(Dept.class).in(Dept::getDeptId, deptIds);
List<Dept> deptList = deptMapper.selectList(deptWrapper);
Map<Long, Dept> longDeptMap = EntityUtils.toMap(deptList, Dept::getDeptId, d -> d);
for (UserVo userVo : userVoIPage.getRecords()) {
Dept dept = longDeptMap.get(userVo.getDeptId());
userVo.setDeptName(dept.getDeptName());
userVo.setStaff(dept.getStaff());
userVo.setTel(dept.getTel());
}
}
return userVoIPage;
}
场景二: 分页增加查询条件
/**
* 场景二: 增加查询条件
* 主表为查询条件
* 分页
*
* @return
*/
@Override
public IPage<UserVo> selectUserPage2() {
Page<User> page = new Page<>(1, 2);
//gt 大于等于
LambdaQueryWrapper<User> userGt = Wrappers.lambdaQuery(User.class).gt(User::getAge, 2);
Page<User> userPage = this.page(page, userGt);
IPage<UserVo> userVoIPage = EntityUtils.toPage(userPage, UserVo::new);
Set<Long> deptIds = EntityUtils.toSet(userVoIPage.getRecords(), UserVo::getDeptId);
if (deptIds.size() > 0) {
LambdaQueryWrapper<Dept> deptWrapper = Wrappers.lambdaQuery(Dept.class).in(Dept::getDeptId, deptIds);
List<Dept> deptList = deptMapper.selectList(deptWrapper);
Map<Long, Dept> longDeptMap = EntityUtils.toMap(deptList, Dept::getDeptId, d -> d);
for (UserVo userVo : userVoIPage.getRecords()) {
Dept dept = longDeptMap.get(userVo.getDeptId());
userVo.setDeptName(dept.getDeptName());
userVo.setStaff(dept.getStaff());
userVo.setTel(dept.getTel());
}
}
return userVoIPage;
}
场景三: 增加查询条件
/**
* 场景三: 增加查询条件
* 子表为查询条件
* 分页
*
* @return
*/
@Override
public IPage<UserVo> selectUserPage3() {
Page<User> page = new Page<>(1, 2);
LambdaQueryWrapper<Dept> deptQuery = Wrappers.lambdaQuery(Dept.class).eq(Dept::getDeptName, "Tomcat");
List<Dept> deptList = deptMapper.selectList(deptQuery);
Map<Long, Dept> longDeptMap = EntityUtils.toMap(deptList, Dept::getDeptId, e -> e);
Set<Long> deptIds = EntityUtils.toSet(deptList, Dept::getDeptId);
LambdaQueryWrapper<User> userQuery = Wrappers.lambdaQuery(User.class).in(User::getDeptId, deptIds);
Page<User> userPage = this.page(page, userQuery);
IPage<UserVo> userVoIPage = EntityUtils.toPage(userPage, UserVo::new);
//优化1
//for (UserVo userVo : userVoIPage.getRecords()) {
// Dept dept = longDeptMap.get(userVo.getDeptId());
// userVo.addDeptInfo(dept);
//}
//优化2
//for (UserVo userVo : userVoIPage.getRecords()) {
// userVo.addDeptInfo(longDeptMap.get(userVo.getDeptId()));
//}
//优化3最终
userVoIPage.getRecords().forEach(userVo -> userVo.addDeptInfo(longDeptMap.get(userVo.getDeptId())));
return userVoIPage;
}
一对多
单条记录查询
/**
* 一对多查询,单条记录,userList属性为null
*
* @param deptId
* @return
*/
public Deptvo selectDeptList(Long deptId) {
Dept dept = this.getById(deptId);
Deptvo deptvo = EntityUtils.toObj(dept, Deptvo::new);
return deptvo;
}
/**
* 一对多查询,单条记录,userList属性完成赋值
*
* @param deptId
* @return
*/
public Deptvo selectDeptList1(Long deptId) {
Dept dept = this.getById(deptId);
Deptvo deptvo = EntityUtils.toObj(dept, Deptvo::new);
LambdaQueryWrapper<User> param = Wrappers.lambdaQuery(User.class).eq(User::getDeptId, deptvo.getDeptId());
List<User> users = userMapper.selectList(param);
deptvo.setUserList(users);
return deptvo;
}
列表查询
一对多查询, 多条记录查询
/**
* 一对多查询, 多条记录查询
*
* @return
*/
public List<Deptvo> selectDeptList2() {
LambdaQueryWrapper<Dept> deptLambdaQueryWrapper = Wrappers.lambdaQuery(Dept.class);
List<Dept> deptList = this.list(deptLambdaQueryWrapper);
List<Deptvo> deptvoList = EntityUtils.toList(deptList, Deptvo::new);
return deptvoList;
}
一对多查询, 多条记录查询,对list空属性进行赋值
*
* 一对多查询, 多条记录查询,对list空属性进行赋值
*
* @return
*/
public List<Deptvo> selectDeptList3() {
LambdaQueryWrapper<Dept> deptLambdaQueryWrapper = Wrappers.lambdaQuery(Dept.class);
List<Dept> deptList = this.list(deptLambdaQueryWrapper);
List<Deptvo> deptvoList = EntityUtils.toList(deptList, Deptvo::new);
//找出部门ID
Set<Long> deptIds = EntityUtils.toSet(deptvoList, Deptvo::getDeptId);
if (deptIds.size() > 0) {
LambdaQueryWrapper<User> inParam = Wrappers.lambdaQuery(User.class).in(User::getDeptId, deptIds);
List<User> userList = userMapper.selectList(inParam);
//以部门为单位对用户进行分组
Map<Long, List<User>> userMap = userList.stream().collect(Collectors.groupingBy(User::getDeptId));
for (Deptvo deptvo : deptvoList) {
deptvo.setUserList(userMap.get(deptvo.getDeptId()));
}
}
return deptvoList;
}
一对多查询, 查询出部门里面年龄最大的用户
/**
* 一对多查询, 查询出部门里面年龄最大的用户
*
* @return
*/
public List<Deptvo> selectDeptList4() {
LambdaQueryWrapper<Dept> deptLambdaQueryWrapper = Wrappers.lambdaQuery(Dept.class);
List<Dept> deptList = this.list(deptLambdaQueryWrapper);
List<Deptvo> deptvoList = EntityUtils.toList(deptList, Deptvo::new);
//找出部门ID
Set<Long> deptIds = EntityUtils.toSet(deptvoList, Deptvo::getDeptId);
if (deptIds.size() > 0) {
LambdaQueryWrapper<User> inParam = Wrappers.lambdaQuery(User.class).in(User::getDeptId, deptIds);
List<User> userList = userMapper.selectList(inParam);
//以部门为单位对用户进行分组
Map<Long, List<User>> usersMap = userList.stream().collect(Collectors.groupingBy(User::getDeptId));
Map<Long, User> userMap = MapUtils.transMap(usersMap,
e -> e.stream().max(Comparator.comparingInt(User::getAge)).orElse(null));
for (Deptvo deptvo : deptvoList) {
deptvo.setUserList(ColUtils.toCol(userMap.get(deptvo.getDeptId())));
}
}
return deptvoList;
}
分页查询
分页详解
/**
* 取出部门Do
*
* @return
*/
public Page<Dept> selectDeptPage1(){
LambdaQueryWrapper<Dept> deptLambdaQueryWrapper = Wrappers.lambdaQuery(Dept.class);
Page<Dept> deptPage = this.page(new Page<>(1, 3), deptLambdaQueryWrapper);
return deptPage;
}
/**
* 部门Do转vo
*
* @return
*/
public IPage<Deptvo> selectDeptPage2(){
LambdaQueryWrapper<Dept> deptLambdaQueryWrapper = Wrappers.lambdaQuery(Dept.class);
Page<Dept> deptPage = this.page(new Page<>(1, 3), deptLambdaQueryWrapper);
IPage<Deptvo> deptvoIPage = EntityUtils.toPage(deptPage, Deptvo::new);
return deptvoIPage;
}
/**
* 部门Do转vo ,并对userList赋值
*
* @return
*/
public IPage<Deptvo> selectDeptPage3(){
LambdaQueryWrapper<Dept> deptLambdaQueryWrapper = Wrappers.lambdaQuery(Dept.class);
Page<Dept> deptPage = this.page(new Page<>(1, 3), deptLambdaQueryWrapper);
IPage<Deptvo> deptvoIPage = EntityUtils.toPage(deptPage, Deptvo::new);
Set<Long> deptIds = EntityUtils.toSet(deptvoIPage.getRecords(), Deptvo::getDeptId);
LambdaQueryWrapper<User> userLambdaQueryWrapper = Wrappers.lambdaQuery(User.class).in(User::getDeptId, deptIds);
List<User> userList = userMapper.selectList(userLambdaQueryWrapper);
if (userList.size()>0) {
//不适用工具类
//Map<Long, List<User>> userMap = userList.stream().collect(Collectors.groupingBy(User::getDeptId));
Map<Long, List<User>> usersMap = EntityUtils.groupBy(userList, User::getDeptId);
for (Deptvo deptvo : deptvoIPage.getRecords()) {
deptvo.setUserList(usersMap.get(deptvo.getDeptId()));
}
}
return deptvoIPage;
}
使用FieldInjectUtils工具类优化代码
工具类FieldInjectUtils 方法:injectListField
/**
* 优化代码,一行代码完成userList赋值
* @return
*/
public IPage<Deptvo> selectDeptPage4(){
LambdaQueryWrapper<Dept> deptLambdaQueryWrapper = Wrappers.lambdaQuery(Dept.class);
Page<Dept> deptPage = this.page(new Page<>(1, 3), deptLambdaQueryWrapper);
IPage<Deptvo> deptvoIPage = EntityUtils.toPage(deptPage, Deptvo::new);
//一行代码完成userList属性注入
FieldInjectUtils.injectListField(deptvoIPage,Deptvo::getDeptId,UserServiceImpl.class,User::getDeptId,Deptvo::getUserList);
return deptvoIPage;
}
- 参考文档
评论区