springboot+mybatis实现查询(四)

一,获取单个

代码依次为controller->service->serviceImpl->mapper->xml

@GetMapping("/{id}")
    public Result getDept(@PathVariable Integer id){
    log.info("根据id查询部门{}",id);
    List depList = deptService.getDept(id);
    return  Result.success(depList);
}
Emp getById(Integer id);
   @Override
     public List getDept(Integer id) {
        return deptMapper.getDept(id);
    }
@Select("select * from dept where id = #{id}")
    List getDept(Integer id)

二,查询所有

代码依次为controller->service->serviceImpl->mapper->xml

    @Autowired
    private DeptService deptService;
    /**
     * 获取部门列表
     */
    @GetMapping
    public Result list(){
        log.info("查询所有部门");
        List depList =  deptService.list();
        return Result.success(depList);
    };
    //查询全部部门数据
    List list();
    //获取列表
    @Override
    public List list() {
        return deptMapper.list();
    }
    @Select("select  * from  dept order by update_time DESC ")
    List list();

三,条件&&分页查询

代码依次为controller->service->serviceImpl->mapper->xml

1.引入pageHelper依赖

     
        
            com.github.pagehelper
            pagehelper-spring-boot-starter
            1.4.2
        

2.封装page实体类

@Data
@NoArgsConstructor
@AllArgsConstructor
public class PageBean {
    private Long total;
    private List rows;//数据列表
}

3.编码

如果不是条件查询后面几个字段可以不加

    @GetMapping("/emps")
    public Result page(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize,
                       String name, Short gender,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin, @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {
        log.info("分页查询{},{}", page, pageSize);
        PageBean pageBean = empService.page(page, pageSize,  name,  gender,  begin,  end);
        return Result.success(pageBean);
    }
  PageBean page(Integer page, Integer pageSize, String name, Short gender, LocalDate begin,  LocalDate end);
    public PageBean page(Integer page, Integer pageSize, String name, Short gender, LocalDate begin, LocalDate end) {
        //1. 设置分页参数
        PageHelper.startPage(page, pageSize);

        //2. 执行查询
        List empList = empMapper.list(name, gender, begin, end);
        Page p = (Page) empList;

        //3. 封装PageBean对象
        PageBean pageBean = new PageBean(p.getTotal(), p.getResult());
        return pageBean;
    }
    public List list(String name, Short gender,LocalDate begin, LocalDate end);

你可能感兴趣的:(mybatis,spring,boot,java)