ssm实现搜索功能

本文介绍了一种使用Spring Boot的Food应用,通过FoodMapper的SQL查询,结合二级缓存技术,实现在前端分页浏览食品数据的功能。FoodService接口处理了分页参数,并利用PageInfo进行动态分页。前端使用Vue框架,通过axios与后端交互并显示搜索结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.数据库持久层(mapper层)

FoodMapper

List<Food>  findFood(@Param("foodName") String foodName, @Param("foodExplain") String foodExplain, @Param("start") int start, @Param("end") int end);

在这里插入图片描述

FoodMapper.xml

<!--搜索食品功能-->
<!--将查询的结果放在二级缓存中-->
<select id="findFood" useCache="true" resultType="com.neusoft.pojo.Food">
    select * from food
    <where>
        <if test="foodName!=null and foodName!=''">
            and foodName like concat('%',#{foodName},'%')
        </if>
        <if test="foodExplain!=null and foodExplain!=''">
            and foodExplain like concat('%',#{foodExplain},'%')
        </if>
        <if test="start>=0 and end>start">
            and foodPrice between #{start} and #{end}
        </if>

    </where>
</select>

在这里插入图片描述

业务逻辑层(service层)

FoodService接口(因为查询到的结果为分页显示,因此要加上分页需要的参数)

/*获取Food数据,处理分页*/
    public PageInfo<Food> findListBiz(int currPage,int pageSize,String foodName,String foodExplain,int start,int end);

在这里插入图片描述

FoodService实现类

  @Override
    public PageInfo<Food> findListBiz(int currPage, int pageSize, String foodName, String foodExplain, int start, int end) {

//        搜索的所有结果进行调用
        List<Food> findlist = foodMapper.findFood(foodName,foodExplain,start,end);
//        2.创建分页对象
        PageInfo<Food> pageInfo = new PageInfo<Food>( findlist.size(),pageSize);
//        3.开始分页
        pageInfo.doPaging(currPage);//计算每页开始的数据位置

//        4.计算每页的结束位置
        if (currPage==pageInfo.getTotalPages())
            pageInfo.setEnd(findlist.size());
        else
            pageInfo.setEnd(pageInfo.getStart()+pageSize);

        List<Food> foods = findlist.subList(pageInfo.getStart(),pageInfo.getEnd());

        //5.将分页结果存到pageInfo中的list
        pageInfo.setList(foods);

        return pageInfo;
    }

在这里插入图片描述

控制层(Controller层)

FoodController

    @ResponseBody
    @RequestMapping("/searchfun")
//    public PageInfo<Food> searchFoods(@RequestParam("foodName") String foodName,
//                                      @RequestParam("foodExplain") String foodExplain,
//                                      @RequestParam("start") int start,
//                                      @RequestParam("end") int end){

    public PageInfo<Food> searchFoods( int currPage, int pageSize,String foodName,String foodExplain,int start,int end){

        PageInfo<Food> pageInfo = foodService.findListBiz(currPage,pageSize,foodName,foodExplain,start,end);
        return  pageInfo;
    }

在这里插入图片描述

分页工具类

pageInfo

	package com.neusoft.util;
	
	
	/*
	* 分页信息类
	* @Author
	* @Date 2021-8-16
	* */
	
	import com.neusoft.pojo.Food;
	
	import java.util.List;
	
	public class PageInfo<T> {
	
	    private int totalRows;//声明数据库中一共有多少条记录
	
	    private int totalPages;//一共能分多少页
	
	    private int pageSize;//每页显示多少条记录
	
	    private int currentPage = 1;//当前页,前端是第几页,后端就是第几页。需要接收前端数据
	
	    private int start;//每页开始的位置
	
	    private int end;//每页结束的位置
	
	    private List<T> list; //每页展示所需展示的数据
	
	    //构造方法
	    public PageInfo(int totalRows,int pageSize){
	        this.totalRows=totalRows;
	        this.pageSize=pageSize;
	
	        //一共能分出多少页
	        if (this.totalRows%this.pageSize==0){
	            this.totalPages=this.totalRows/this.pageSize;
	        }else {
	            this.totalPages=this.totalRows/this.pageSize+1;
	        }
	    }
	
	    //分页逻辑
	    public void doPaging(int reqPage){
	        this.currentPage=reqPage;
	        //计算开始和结束的位置
	        this.start= (this.currentPage-1)*this.pageSize;
	    }
	
	
	    public int getTotalRows() {
	        return totalRows;
	    }
	
	    public void setTotalRows(int totalRows) {
	        this.totalRows = totalRows;
	    }
	
	    public int getTotalPages() {
	        return totalPages;
	    }
	
	    public void setTotalPages(int totalPages) {
	        this.totalPages = totalPages;
	    }
	
	    public int getPageSize() {
	        return pageSize;
	    }
	
	    public void setPageSize(int pageSize) {
	        this.pageSize = pageSize;
	    }
	
	    public int getCurrentPage() {
	        return currentPage;
	    }
	
	    public void setCurrentPage(int currentPage) {
	        this.currentPage = currentPage;
	    }
	
	    public int getStart() {
	        return start;
	    }
	
	    public void setStart(int start) {
	        this.start = start;
	    }
	
	    public int getEnd() {
	        return end;
	    }
	
	    public void setEnd(int end) {
	        this.end = end;
	    }
	
	    public List<T> getList() {
	        return list;
	    }
	
	    public void setList(List<T> list) {
	        this.list = list;
	    }
	}

前端显示(vue代码)

方法:

searchFun(){
				let that = this
				this.$axios({
					methods:'post',
					url:'http://localhost:8888/proSSM/food/searchfun?currPage='+that.currentPage+'&pageSize='+that.pageSize+'&'+that.$qs.stringify(that.searchFood)
				}).then(function(response){
					//一共分了多少页
					that.totalPages=response.data.totalPages
					that.currentPage=response.data.currentPage
					that.food=response.data.list
				})
			},

显示表单

		<el-form label-width="80px" :model="searchFood">
		<el-row>
			<el-col :span="5"><el-form-item label="食物名称">
				<el-input v-model="searchFood.foodName"></el-input>
			</el-form-item></el-col>
			<el-col :span="5"><el-form-item label="食物简介">
				<el-input v-model="searchFood.foodExplain"></el-input>
			</el-form-item></el-col>
			<el-col :span="5"><el-form-item label="价格区间">
				<el-input type="number" v-model="searchFood.start"></el-input>
			<!-- -
				<el-input v-model="searchFood.end"></el-input> -->
			</el-form-item></el-col>
			<el-col :span="0.5" style="margin: 25px;">-</el-col>
			<el-col :span="5"><el-form-item >
			<!-- 	<el-input v-model="searchFood.start"></el-input>
			- -->
				<el-input type="number" v-model="searchFood.end"></el-input>
			</el-form-item></el-col>
			<el-col :span="3.5"><el-button @click="searchFun()">搜索</el-button></el-col>
		</el-row>

用到的数据

		searchFood:{
							foodName:'',
							foodExplain:'',
							start:0,
							end:0
						},
			pageSize:3,
			currentPage:1,
			food:[]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值