Vue-SpringBoot分页查询以及条件查询解决方案

前端写好页数和每页显示多少数据,如果搜索条件不为空的话,加上搜索条件。

initEvaluations(){
	let url = '/evaluation/?page=' + this.page + '&size=' + this.size;
	if (this.employeeName) {
    	url += '&employeeName=' + this.employeeName;
    }
    this.getRequest(url).then(resp=>{
    	if(resp){
        	this.evaluations = resp.data;
            this.total = resp.total;
        }
    });
}

Controller层接收请求,调用Service处理。

@RestController
@RequestMapping("/evaluation")
public class EvaluationController {
@Autowired
    EvaluationService evaluationService;
    @GetMapping("/")
    public RespPageBean getEvaluationByPage(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer size, Evaluation evaluation, Date[] beginDateScope) {
        return evaluationService.getAllEvaluation(page,size,evaluation,beginDateScope);
    }
}

Service层把页数和每页条数进行处理,变成从第几条开始,需要几条数据。一般除了数据本身外,还需要数据总数,方便展示。

@Service
public class EvaluationService {
    @Autowired
    EvaluationMapper evaluationMapper;

    public RespPageBean getAllEvaluation(Integer page, Integer size, Evaluation evaluation, Date[] dateScope){
        if (page != null && size != null) {
            page = (page - 1) * size;
        }
        List<Evaluation> data = evaluationMapper.selectAllEvaluationByPage(page,size,evaluation,dateScope);
        Long total = evaluationMapper.count(evaluation,dateScope);
        RespPageBean bean = new RespPageBean();
        bean.setData(data);
        bean.setTotal(total);
        return bean;
    }
}    

Mapper接口声明方法。

public interface EvaluationMapper {
    // 查全部  搜索其实也在里面
    List<Evaluation> selectAllEvaluationByPage( Integer page,  Integer size,  Evaluation evaluation,  Date[] dateScope);

    //查某个条件下,数据总量
    Long count(Evaluation evaluation,  Date[] dateScope);
}

xml文件实现接口,其实就是创建了一个代理类,来实现Mapper接口。实现的方式是用SQL写的。
这里的page其实已经不是页数的意思了,为了代码可读性可以不用这么做。
用<if test标签来实现条件查询。

	<resultMap id="EvaluationWithName" type="org.javaboy.vhr.model.Evaluation">
            <id column="id" property="id" jdbcType="INTEGER"/>
            <result column="evaluation" property="evaluation" jdbcType="LONGVARCHAR"/>
            <result column="evaluationTime" property="evaluationTime" jdbcType="TIMESTAMP"/>
            <result column="employeeName" property="employeeName" jdbcType="VARCHAR"/>
            <result column="hrName" property="hrName" jdbcType="VARCHAR"/>
    </resultMap>
    <select id="selectAllEvaluationByPage" resultMap="EvaluationWithName">
        select evaluation.id,evaluation.evaluation,evaluation.evaluationTime,employee.`name` 'employeeName',hr.`name` 'hrName',hr.`id` 'hrId'
        from evaluation,employee,hr
        where evaluation.employeeId = employee.id
        and evaluation.hrId = hr.id
        <if test="evaluation.employeeName !=null and evaluation.employeeName!=''">
            and employee.`name` like concat('%',#{evaluation.employeeName},'%')
        </if>
        ORDER BY evaluationTime desc
        <if test="page !=null and size!=null">
            limit #{page},#{size}
        </if>
    </select>

	<select id="count" resultType="java.lang.Long">
        select count(1)
        from evaluation,employee,hr
        where evaluation.employeeId = employee.id
        and evaluation.hrId = hr.id
        <if test="evaluation.employeeName !=null and evaluation.employeeName!=''">
            and employee.`name` like concat('%',#{evaluation.employeeName},'%')
        </if>
    </select>
  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
vue-springboot是一种结合了Vue.js和Spring Boot的开发框架。它可以用于构建前后端分离的应用程序。Vue.js是一个用于构建用户界面的JavaScript框架,而Spring Boot是一个用于快速构建Java应用程序的框架。通过结合这两个框架,可以实现前端页面和后端逻辑的分离开发,并且能够通过API进行数据交互。 实现vue-springboot的开发过程可以分为以下几个步骤: . 创建后端Spring Boot项目:使用IDE(如IntelliJ IDEA)创建一个新的Spring Boot项目并配置好相关依赖。 2. 创建前端Vue项目:同样地,在IDE中创建一个新的Vue项目。可以使用Vue CLI进行项目的初始化和配置。 3. Spring Boot集成MyBatis:在后端项目中集成MyBatis作为持久框架,用于与数据库进行交互。可以使用相关的依赖和配置来实现集成。 4. 前后端联调:在进行开发时,需要确保前后端项目能够正常通信。可以通过设置不同的端口号来避免端口冲突,并通过API进行数据的传递和交互。 通过以上步骤,可以实现一个基于vue-springboot的应用程序,其中前端使用Vue.js构建用户界面,后端使用Spring Boot处理业务逻辑,并通过API进行数据交互。这种框架可以提高开发效率和代码的可维护性,同时也能够满足前后端分离开发的需求。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [vue-springboot登录](https://download.csdn.net/download/qq_39999139/12007670)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [搭建SpringBoot+Vue 项目 完整流程](https://blog.csdn.net/pary__for/article/details/125928389)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [vueSpringboot的整合](https://blog.csdn.net/m0_67401660/article/details/125345747)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值