springboot中使用Mybatisplus+Vue+elementUI实现分页

前端发送的数据

let params = {
        params: {
          currentPage: this.currentPage, //当前页
          pageSize: this.pageSize, //每页显示条数
          sort: this.sort, //排序字段信息
          priceGte: this.min, //价格区间
          priceLte: this.max,
        }
      }

后端

用于接收的实体类

@Data
@ToString
public class SelectPage {
    private Integer currentPage;
    private Integer pageSize;
    private String sort;
    private String priceGte; //大于等于
    private String priceLte; //小于等于
}

controller层

    /**
     * 分页按条件查询
     * @param selectPage 分页查询对象
     * @return Page对象
     */
    @GetMapping("goods/allGoods")
    public Page selectAll(SelectPage selectPage) {
        Page<TbGoodsAll> goodsAllPage = tbGoodsAllService.selectPage(selectPage);
        return goodsAllPage;
    }

service层

    @Autowired
    TbGoodsAllDao tbGoodsAllDao;

    @Override
    public Page<TbGoodsAll> selectPage(SelectPage selectPage) {
        //参数一:当前页
        //参数二:页面显示条数
        Page<TbGoodsAll> page = new Page<>(selectPage.getCurrentPage(),selectPage.getPageSize()); //从map中取出分页数据
        QueryWrapper<TbGoodsAll> queryWrapper = new QueryWrapper<>();
        QueryWrapper<TbGoodsAll> price;
        //利用条件构造器,进行排序查询
        if ("1".equals(selectPage.getSort())) {
            price = queryWrapper.orderByAsc("price"); //从小到大
        }else if ("-1".equals(selectPage.getSort())){
            price = queryWrapper.orderByDesc("price"); //从大到小
        } else {
            price = null;
        }
        //按区间查
        if (!("".equals(selectPage.getPriceGte()) && "".equals(selectPage.getPriceLte()))){
            price = queryWrapper.between("price", selectPage.getPriceGte(),selectPage.getPriceLte());
        }
        Page<TbGoodsAll> tbGoodsAllPage = tbGoodsAllDao.selectPage(page, price);
        return tbGoodsAllPage;
    }

vue

        <div class="pagination">
          <el-pagination
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="currentPage"
              :page-sizes="[8, 20, 40, 80]"
              :page-size="pageSize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="total">
          </el-pagination>
        </div>

_getAllGoods()将新数据向后端发起数据请求

    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageSize = val
      this._getAllGoods()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.currentPage = val
      this._getAllGoods()
    },
首先,在前端页面上添加一个输入框用于输入搜索关键字,并且在点击搜索按钮时将关键字传递到后端。 然后,在后端的控制器,接收前端传递的关键字参数,并且调用MyBatisPlus提供的模糊查询API进行查询。 具体实现步骤如下: 1.前端页面实现 在前端页面上添加一个输入框和搜索按钮,当用户在输入框输入了关键字并点击搜索按钮时,将关键字传递到后端。 ```vue <template> <div> <input v-model="keyword" /> <button @click="search">搜索</button> </div> </template> <script> export default { data() { return { keyword: '', }; }, methods: { search() { this.$axios.get('/api/search', { params: { keyword: this.keyword } }).then((res) => { console.log(res.data); }); }, }, }; </script> ``` 2.后端控制器实现 在后端控制器,接收前端传递的关键字参数,并且调用MyBatisPlus提供的模糊查询API进行查询。这里我们使用MyBatisPlus提供的QueryWrapper对象来构建查询条件,并且使用Lambda表达式来简化代码。 ```java @RestController @RequestMapping("/api") public class SearchController { @Autowired private UserService userService; @GetMapping("/search") public List<User> search(String keyword) { QueryWrapper<User> wrapper = new QueryWrapper<>(); wrapper.like("name", keyword).or().like("email", keyword); return userService.list(wrapper); } } ``` 在上面的代码,我们构建了一个QueryWrapper对象,使用like方法来查询name或者email字段包含关键字的数据。最终调用userService的list方法来执行查询操作。 需要注意的是,在这里我们假设UserService是一个MyBatisPlus提供的服务类,并且已经配置好了MapperScannerConfigurer来扫描Mapper接口。如果还没有配置,可以参考MyBatisPlus的文档进行配置。 3.配置MyBatisPlus 在使用MyBatisPlus的模糊查询功能前,需要先配置MyBatisPlus。在SpringBoot,只需要在配置文件添加如下配置即可: ```yaml mybatis-plus: mapper-locations: classpath:/mapper/**/*.xml global-config: db-config: logic-delete-value: 1 logic-not-delete-value: 0 configuration: map-underscore-to-camel-case: true ``` 在上面的配置,我们指定了Mapper接口XML文件的位置,配置了逻辑删除的值和驼峰命名规则等。 4.编写Mapper接口和XML文件 最后,我们需要编写Mapper接口和XML文件来实现具体的查询操作。这里我们以User表为例,编写如下代码: ```java public interface UserMapper extends BaseMapper<User> { } ``` ```xml <mapper namespace="com.example.mapper.UserMapper"> <resultMap id="BaseResultMap" type="com.example.entity.User"> <id column="id" property="id" /> <result column="name" property="name" /> <result column="email" property="email" /> </resultMap> <select id="selectByKeyword" resultMap="BaseResultMap"> select * from user where name like concat('%', #{keyword}, '%') or email like concat('%', #{keyword}, '%') </select> </mapper> ``` 在上面的代码,我们使用MyBatisPlus提供的BaseMapper接口,并且编写了一个selectByKeyword方法来执行模糊查询操作。需要注意的是,我们使用了concat函数来将%和关键字拼接起来,以实现模糊查询的效果。 至此,我们已经完成了springboot+mybatisplus+vue实现模糊查询的全部操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值