[Separation of front-end and back-end] Back-end multi-table joint query

Effect: query the user table through the information in the input box, design the joint query of the department and the user table

insert image description here

Business logic

According to the information entered in several different input boxes on the page, the corresponding data is found in the data table, similar to querying by conditions

project code

1 Project original code

@GetMapping("/findDoctorList")
    public Result findDoctorList(@RequestParam(required = true,defaultValue = "1")Integer current,
                                 @RequestParam(required = true,defaultValue = "6")Integer size){
    
    
        //对 用户进行分页,泛型注入的为用户类
        Page<Doctor>page=new Page<>(current,size);
        //此种查询不限查询条件,即全部查询
        Page<Doctor> doctorPage = doctorService.page(page);
        long total = doctorPage.getTotal();
        List<Doctor> records = doctorPage.getRecords();
        return Result.ok().data("total",total).data("records",records);
    }

Test effect (find out all data)

insert image description here

2 Use the conditional query method that comes with Mybatisplus to query, here is an example of a certain attribute

@GetMapping("/findDList")
    public Result findDList(@RequestParam(required = true,defaultValue = "1")Integer current,
                                 @RequestParam(required = true,defaultValue = "6")Integer size){
    
    
        //对 用户进行分页,泛型注入的为用户类
        Page<D>page=new Page<>(current,size);
        LambdaQueryWrapper<D> queryWrapper=new LambdaQueryWrapper<>();
        // 只适用于单表查询 此举含义是将D中的dmentID属性赋值为1,即仅查询dmentID=1的对象
        //但此方法多表联查不好查,只能在单表内进行查询
        queryWrapper.eq(D::getDmentId,1);
        //限制性别属性为1
        queryWrapper.eq(D::getSex,1);
        // 如果以上两条同时限定,则在需要限制同时查出 dment=1和sex=1的用户
        Page<D> dPage = dService.page(page,queryWrapper);
        long total = dPage.getTotal();
        List<D> records = dPage.getRecords();
        return Result.ok().data("total",total).data("records",records);
    }

Test results (limit dment=1 and sex=1 at the same time)

insert image description here
3 Override the QueryWrapper method

It can be seen from the above process that when querying by conditions, the method of LambdaQueryWrapper is used. In fact, it is also possible to query directly by means of QueryWrapper , but QueryWrapper uses the Entity object to encapsulate the operation class, while LambdaQueryWrapper uses the Lambda expressions, because the attribute fields in multiple tables are required when performing a multi-table joint query, the method of rewriting QueryWrapper is mainly introduced here .

  1. To query the SQL statement, in the xml file corresponding to the entity class, write the following statement
<!--findDPage与mapper.java种对应的方法一样-->
  <select id="findDPage" resultType="D">
        select u.`id`, `username`, `nickname`, `email`, `avatar`, `phone_number`, `status`,
               u.`create_time`, u.`modified_time`, `sex`, `salt`, `type`, `password`, `birth`,
               `dment_id`, `deleted`,d.name as name
        from d u
        inner join dment d
        on u.dment_id = d.id
        ${ew.customSqlSegment}<!-- 为了配合QueryWrapper中的条件 -->
    </select>

2 Mapper.java methods

public interface DMapper extends BaseMapper<D> {
    
    
    IPage<D> findDPage(Page<D>page,@Param(Constants.WRAPPER) QueryWrapper<D> wrapper);
}

3 Service methods

public interface DService extends IService<D> {
    
    
    IPage<D> findDPage(Page<D> page, @Param(Constants.WRAPPER) QueryWrapper<D> wrapper);
}

4 ServiceImp method

@Service
public class DServiceImpl extends ServiceImpl<DMapper, D> implements DService {
    
    
    @Override
    public IPage<D> findDPage(Page<D> page, QueryWrapper<D> wrapper) {
    
    
        return this.baseMapper.findDPage(page,wrapper);
    }
}

5 Define the ViewObject class corresponding to the View layer. The properties in this category correspond to the query conditions required by the display page

@Data
public class DVO {
    
    
    private String username;
    private String nickname;
    private String email;
    private Integer sex;
    private Long departmentId;
}

insert image description here
5 Controller call

Since the front-end page needs to query a lot of data, encapsulate it in the json file of DVO to pass the value from the front-end to the back-end, pay attention to using the Post method to request,

 @PostMapping("/findDPage")
    public Result findDPage(@RequestParam(required = true,defaultValue = "1")Integer current,
                                 @RequestParam(required = true,defaultValue = "6")Integer size,
                                 @RequestBody DVO dVO){
    
    
        //对 用户进行分页,泛型注入的为用户类
        Page<D>page=new Page<>(current,size);
        //LambdaQueryWrapper<D> queryWrapper=new LambdaQueryWrapper<>();
        // 只适用于单表查询 多表联查不好查
        //queryWrapper.eq(D::getDepartmentId,1);
        QueryWrapper<Doctor>queryWrapper=getWapper(dVO);
        IPage<D> dPage = dService.findDPage(page,queryWrapper);
        long total = dPage.getTotal();
        List<D> records = dPage.getRecords();
        return Result.ok().data("total",total).data("records",records);
    }
    private QueryWrapper<D>getWapper(DVO dVO){
    
    
        QueryWrapper<D>queryWrapper=new QueryWrapper<>();
        if(dVO!=null){
    
    
            if(!StringUtils.isEmpty(dVO.getDepartmentId())){
    
    
                queryWrapper.eq("department_id",dVO.getDepartmentId());
            }
            if(!StringUtils.isEmpty(dVO.getUsername())){
    
    
                queryWrapper.eq("username",dVO.getUsername());
            }
            if(!StringUtils.isEmpty(dVO.getEmail())){
    
    
                queryWrapper.eq("email",dVO.getEmail());
            }
            if(!StringUtils.isEmpty(dVO.getSex())){
    
    
                queryWrapper.eq("sex",dVO.getSex());
            }
            if(!StringUtils.isEmpty(dVO.getNickname())){
    
    
                queryWrapper.eq("nickname",dVO.getNickname());
            }
        }
        return queryWrapper;
    }

Test (query success)

insert image description here

Guess you like

Origin blog.csdn.net/qq_29750461/article/details/122606486