springboot中,使用mybatisPlus实现分页查询,条件查询

建一个实体类

package com.zg.mymes.entities;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.format.annotation.DateTimeFormat;

import java.util.Date;

/**
 * @Auther: Zg
 * @Date: 2023/4/2 - 04 - 02 - 21:51
 * @Description: com.zg.mymes.entities
 * @version: 1.0
 */
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Slf4j
@TableName("the_recipe")
public class TheRecipe {
    @TableId(type = IdType.AUTO)
    private Integer id;
    //private String oderName;
    private String userName; //命令人
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone="GMT+8")
    private Date myStartDate; //命令开始时间,Date格式
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone="GMT+8")
    private Date startDate; //命令开始时间,DateTime格式

    private String aorB;  //A还是B
    private String roNum; //当前产品号
    private String proName; //产品名
    private String openPos; //工装打开位置
    private String closePos; //工装关闭位置
    private String enterPos; //进入焊房位置
    private String exitPos; //退出焊房位置
    private String trVec; //行走滑台速度
    private String fkVec; //工装滑台速度

    private Integer deleted;
    private String remark;
}

建立一个封装的VO,接收前端返回的数据。

package com.zg.mymes.vo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;


@Data
@AllArgsConstructor
@NoArgsConstructor
public class Page implements Serializable {
    private Integer page;
    private Integer limit;

    public Long getStart() {
        return Long.valueOf((page - 1) * limit);
    } //page from,第page页从第(page - 1) * limit 条数据开始
}


在这里插入代码片
package com.zg.mymes.vo;

import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;

import java.util.Date;

/**
 * @Auther: Zg
 * @Date: 2023/4/2 - 04 - 02 - 22:11
 * @Description: com.zg.mymes.vo
 * @version: 1.0
 */
@Data
public class TheRecipeQuery extends Page{
    private String userName;
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date startDate;
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date endDate;
}

因为前端需要复合条件的数据和数据数量,java不能返回2个以上的参数,所以封装一个返回值类

package com.zg.mymes.helper;

import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

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

/**
 * @Auther: Zg
 * @Date: 2023/4/25 - 04 - 25 - 22:29
 * @Description: com.zg.mymes.helper
 * @version: 1.0
 */
@Builder
@NoArgsConstructor
@Slf4j
@Data
public class MyQueryPage<T> {
    private List<T> myList = new ArrayList<>();
    private Long myCount;
    //private R query;

    public MyQueryPage(List<T> myList, Long myCount){
        this.myList = myList;
        this.myCount = myCount;
    }
//    public void myRet(List<T> myList, Long myCount){
//        this.myList = myList;
//        this.myCount = myCount;
//    }
}

mybatisPlus的mapper,Service和Impl

package com.zg.mymes.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zg.mymes.entities.TheRecipe;
import com.zg.mymes.entities.WorkLog;
import com.zg.mymes.vo.TheRecipeQuery;
import com.zg.mymes.vo.WorkLogQuery;
import org.apache.ibatis.annotations.Param;

import java.util.List;

/**
 * @Auther: Zg
 * @Date: 2023/4/2 - 04 - 02 - 22:11
 * @Description: com.zg.mymes.mapper
 * @version: 1.0
 */
public interface TheRecipeMapper extends BaseMapper<TheRecipe> {
    //XML
    List<TheRecipe> getTheRecipeList(TheRecipeQuery param);
    //XML
    Long countTheRecipeList(TheRecipeQuery param);
    //不加xml报错
    void deleteTheRecipeByIds(@Param(value="ids") String ids);
}

package com.zg.mymes.servicePlus;

import com.baomidou.mybatisplus.extension.service.IService;
import com.zg.mymes.entities.TheRecipe;
import com.zg.mymes.helper.MyQueryPage;
import com.zg.mymes.vo.TheRecipeQuery;

/**
 * @Auther: Zg
 * @Date: 2023/4/26 - 04 - 26 - 0:12
 * @Description: com.zg.mymes.servicePlus
 * @version: 1.0
 */
public interface ITheRecipeService extends IService<TheRecipe> {
    MyQueryPage getPageList(TheRecipeQuery param);
}

package com.zg.mymes.servicePlus.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zg.mymes.entities.TheRecipe;
import com.zg.mymes.helper.MyQueryPage;
import com.zg.mymes.mapper.TheRecipeMapper;
import com.zg.mymes.servicePlus.ITheRecipeService;
import com.zg.mymes.vo.TheRecipeQuery;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @Auther: Zg
 * @Date: 2023/4/26 - 04 - 26 - 0:13
 * @Description: com.zg.mymes.servicePlus.impl
 * @version: 1.0
 */
@Service
public class TheRecipeServiceImplPlus extends ServiceImpl<TheRecipeMapper, TheRecipe> implements ITheRecipeService {
    @Override
    public MyQueryPage getPageList(TheRecipeQuery param) {

        LambdaQueryWrapper<TheRecipe> wrapper = new LambdaQueryWrapper<>();
        Boolean myCondition = param.getStartDate() !=null;
        wrapper.eq(StringUtils.hasLength(param.getUserName()), TheRecipe::getUserName,param.getUserName())
                .between(myCondition,
                        TheRecipe::getMyStartDate,
                        param.getStartDate(),
                        param.getEndDate());

        Page<TheRecipe> page = new Page<>(param.getPage(),param.getLimit());

        this.page(page,wrapper);
        Long count = page.getTotal();
        List<TheRecipe> list = page.getRecords();
        MyQueryPage<TheRecipe> myQueryPage = new MyQueryPage<>(list,count);
        
        //this.baseMapper 调用源生mapper技能
        return myQueryPage;
    }
}

Controller(查询对应 /list)

package com.zg.mymes.controller.factoryTec;

import com.zg.mymes.entities.TheRecipe;
import com.zg.mymes.helper.MyQueryPage;
import com.zg.mymes.servicePlus.ITheRecipeService;
import com.zg.mymes.vo.Result;
import com.zg.mymes.vo.TheRecipeQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Auther: Zg
 * @Date: 2023/4/2 - 04 - 02 - 22:04
 * @Description: com.zg.mymes.controller.factoryTec
 * @version: 1.0
 */
@Controller
@RequestMapping("/theRecipe")
public class TheRecipeController {
    @Autowired
    private ITheRecipeService service;

    @GetMapping("")

    public String toTheRecipeListUI(){
        return "therecipe/theRecipeList";
    }

    @GetMapping("/list")
    @ResponseBody

    public Result<Object> getTheRecipeList(TheRecipeQuery param) {

        MyQueryPage pageList = service.getPageList(param);

        return Result.success(pageList.getMyList(),pageList.getMyCount());
    }

    @PostMapping("/add")
    @ResponseBody

    public Result<Object> addTheRecipe(TheRecipe theRecipe) {
        Date date = new Date();//获得当前时间
        Timestamp myDate = new Timestamp(date.getTime());//将时间转换成Timestamp类型,这样便可以存入到Mysql数据库中

        theRecipe.setStartDate(myDate);
        theRecipe.setMyStartDate(myDate);
        service.save(theRecipe);
        return Result.success("新增配方成功");
    }


    @GetMapping("/add/ui")

    public String toAddUI() {
//        List<Dept> deptList = deviceManageService.getAllDept();
//        model.addAttribute("deptList",deptList);
        return "therecipe/theRecipeAdd";
    }

    @DeleteMapping("/{ids}")
    @ResponseBody
    public Result<Object> deleteTheRecipeByIds(@PathVariable("ids") String ids){

        //System.out.println(ids);
        //service.deleteTheRecipeByIds(ids);
        String[] arr = ids.split(",");
        List<String> collect = Arrays.stream(arr).collect(Collectors.toList());
        service.removeBatchByIds(collect);
        return Result.success("删除配方成功");
    }

    @GetMapping("/{id}")
    public String getTheRecipeById(@PathVariable("id") Integer id, Model model)
    {
        //TheRecipe theRecipe = service.findById(id);
        TheRecipe theRecipe = service.getById(id);
        model.addAttribute("theRecipe",theRecipe);

        return "therecipe/theRecipeEdit";
    }

    @PutMapping("")
    @ResponseBody
    public Result<Object> updateTheRecipe(TheRecipe theRecipe){

        service.updateById(theRecipe);
        return  Result.success("修改日志成功");
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

潘诺西亚的火山

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值