SpringBoot实现分类搜索(模糊)查询 Pageable分页

效果图如下:

SpringBoot实现分类搜索(模糊)查询 Pageable分页_第1张图片

数据库结构:

Student学生表

 

stu表

sid学生id

sname 学生姓名

sex性别

cid班级id

address地址

sorts 排序

SpringBoot实现分类搜索(模糊)查询 Pageable分页_第2张图片

classes班级表

cid  cname 

  1. 1801
  2. 1802

SpringBoot实现分类搜索(模糊)查询 Pageable分页_第3张图片

经过查找又发现了可以在sql语句里写一个case判断 

解决了sql的外键判断为0不显示数据的尴尬,这样就可以写Integer

CASE WHEN :cc_id=0  THEN 1  ELSE cc_id=:cc_id END

在下面我放的是全部的代码,包含一些增删改查

底下有第二种方式,是之前写的,不是很友好

附上代码

StuDao

package com.msfh.news.dao;

import java.util.List;
import java.util.Map;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;

import com.msfh.news.eneity.Stu;

public interface StuDao extends PagingAndSortingRepository, JpaSpecificationExecutor {
	//分页
	@Query(value = "select * from stu join classes on stu.cc_id = classes.ccid where " +
            " sname like CONCAT('%',:sname,'%')"
            + " and CASE WHEN :cc_id=0  THEN 1  ELSE cc_id=:cc_id END " + 
            " order by stu.sid asc",nativeQuery = true)
    Page> findPage(Pageable pageable,String sname,Integer cc_id);
}

StuService层

package com.msfh.news.service;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.msfh.news.dao.StuDao;
import com.msfh.news.eneity.Stu;

import lombok.val;

@Service
public class StuService {
    @Autowired
    private StuDao stuDao;
    
    
    /**
     * 分页
     * @return
     */
    public Page> findPage(Pageable pageable,String sname,Integer cc_id){
        return stuDao.findPage(pageable,sname,cc_id);
    }
    
    public void save(Stu stu) {
    	stuDao.save(stu);
    }
    
    public void delete(Stu stu) {
    	stuDao.delete(stu);
    }
}

StuController

package com.msfh.news.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.msfh.news.eneity.Banner;
import com.msfh.news.eneity.Stu;
import com.msfh.news.service.BannerService;
import com.msfh.news.service.StuService;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Controller
@RequestMapping("stu")
public class StuController {
    @Autowired
    private StuService stuService;
    
    
    @RequestMapping("stu")
    public String stu() {
    	return "/news/stu";
    }
    
    @RequestMapping("stuAdd")
    public String stuAdd() {
    	return "/news/stuAdd";
    }
    
    /**
     * ajax分页
     * @return
     */
    @RequestMapping("findPage")
    @ResponseBody
    public Page> findPage(HttpServletRequest request){
        String sname = request.getParameter("sname");
        Integer cc_id = Integer.parseInt(request.getParameter("ccid"));
        Integer NowPage = Integer.parseInt(request.getParameter("NowPage"));
        Pageable Page = PageRequest.of(NowPage-1, 3);
        Page> data = stuService.findPage(Page,sname,cc_id);
        return data;
    }
    
    @RequestMapping("save")
    @ResponseBody
    public JSONObject save(HttpServletRequest request) {
    	String stuData = request.getParameter("stuData");
    	JSONObject jsonObject = JSONObject.fromObject(stuData);
    	Stu stu = (Stu) jsonObject.toBean(jsonObject,Stu.class);
    	stuService.save(stu);
    	JSONObject data = new JSONObject();
    	data.put("status", "1");
    	data.put("success","添加成功");
    	return data;
    }
    
    @RequestMapping("delete")
    @ResponseBody
    public String delete(HttpServletRequest request) {
    	Integer sid = Integer.parseInt(request.getParameter("sid"));
    	Stu stu = new Stu();
    	stu.setSid(sid);
    	stuService.delete(stu);
    	return "删除成功";
    }
    
    @RequestMapping("deleteAll")
    //@ResponseBody
    public String deleteAll(HttpServletRequest request) {
    	String sids = request.getParameter("sids");
    	JSONArray jsonArray = JSONArray.fromObject(sids);
    	for (int i = 0; i < jsonArray.size(); i++) {
			Stu stu = new Stu();
			stu.setSid((Integer) jsonArray.get(i));
			stuService.delete(stu);
		}
    	return "news/stu";
    }
}

stu.html




  
  
  
  
  
  
  
  
  
  
  


内容列表 添加字段
ID 学生姓名 性别 班级名称 地址 操作
排序: {{stu.sname}} {{stu.sex}} {{stu.ccname}} {{stu.address}}
全选 删除    

我们这个是vue配合springboot 实现分页效果和分类搜索的操作

这里我们dao层需要重写继承的方法,同时也需要重写sql语句的query需要完全执行sql语句

我们在dao层写的是String cid可是我们的cid是外键应该是Integer的,但是因为我们执行了Integer的条件它就是一个sql语句了

所以我在html给赋的初值为null,然后我再controller接受的为String型

StuDao

package com.msfh.news.dao;

import java.util.List;
import java.util.Map;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;

import com.msfh.news.eneity.Stu;

public interface StuDao extends PagingAndSortingRepository, JpaSpecificationExecutor {
	//分页
	@Query(value = "select * from stu join classes on stu.cc_id = classes.ccid where" +
            " sname like CONCAT('%',:sname,'%')"
            + " and cc_id like CONCAT('%',:cc_id,'%') order by stu.sid asc",nativeQuery = true)
    Page> findPage(Pageable pageable,String sname,String cc_id);
}

 

StuService层

package com.msfh.news.service;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.msfh.news.dao.StuDao;
import com.msfh.news.eneity.Stu;

import lombok.val;

@Service
public class StuService {
    @Autowired
    private StuDao stuDao;
    
    
    /**
     * 分页
     * @return
     */
    public Page> findPage(Pageable pageable,String sname,String cc_id){
        return stuDao.findPage(pageable,sname,cc_id);
    }
    
   
}

StuController

package com.msfh.news.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.msfh.news.eneity.Banner;
import com.msfh.news.eneity.Stu;
import com.msfh.news.service.BannerService;
import com.msfh.news.service.StuService;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Controller
@RequestMapping("stu")
public class StuController {
    @Autowired
    private StuService stuService;
    
    
    @RequestMapping("stu")
    public String stu() {
    	return "/news/stu";
    }
    
    @RequestMapping("stuAdd")
    public String stuAdd() {
    	return "/news/stuAdd";
    }
    
    /**
     * ajax分页
     * @return
     */
    @RequestMapping("findPage")
    @ResponseBody
    public Page> findPage(HttpServletRequest request){
        String sname = request.getParameter("sname");
        String cc_id = request.getParameter("ccid");
        Integer NowPage = Integer.parseInt(request.getParameter("NowPage"));
        Pageable Page = PageRequest.of(NowPage-1, 3);
        Page> data = stuService.findPage(Page,sname,cc_id);
        return data;
    }
    
    @RequestMapping("save")
    @ResponseBody
    public JSONObject save(HttpServletRequest request) {
    	String stuData = request.getParameter("stuData");
    	JSONObject jsonObject = JSONObject.fromObject(stuData);
    	Stu stu = (Stu) jsonObject.toBean(jsonObject,Stu.class);
    	stuService.save(stu);
    	JSONObject data = new JSONObject();
    	data.put("status", "1");
    	data.put("success","添加成功");
    	return data;
    }
}

stu.html

这里我们写的分类查询我们是需要查出外键相匹配的值的表的数据

就如同效果图一样

我们需要通过ajax查出来然后进行一个循环,我再下面的班级查询有写的ajax部分,在HTML上面也有循环的部分,所有我就不附上classes的代码和图部分了




  
  
  
  
  
  
  
  
  
  
  


内容列表 添加字段
ID 学生姓名 性别 班级名称 地址 操作
排序: {{stu.sname}} {{stu.sex}} {{stu.ccname}} {{stu.address}}
全选 删除    

 

你可能感兴趣的:(java,springboot,Vue)