Springboot实现 表连接,下拉框,模糊查询,分页 功能

两表连接实现curd 下拉框 模糊查询 分页

在这里插入图片描述

** Entrty实体类**

package com.fan.demo.Entrty;
import javax.persistence.*;
@Table
@Entity(name = "student")
public class Student {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int sid;
    private String sname;
    private int age;
    private int sex;
    private int scid;
    @ManyToOne
    @JoinColumn(name = "scid",referencedColumnName = "cid",insertable = false,updatable = false)
                        //学生表的外键                  //班级表的主键
    private Classes classes;
    public Classes getClasses() {
        return classes;
    }
    public void setClasses(Classes classes) {
        this.classes = classes;
    }
    public void setSid(int sid){
        this.sid=sid;
    }
    public int getSid(){
        return this.sid;
    }
    public void setSname(String sname){
        this.sname=sname;
    }
    public String getSname(){
        return this.sname;
    }
    public void setAge(int age){
        this.age=age;
    }
    public int getAge(){
        return this.age;
    }
    public void setSex(int sex){
        this.sex=sex;
    }
    public int getSex(){
        return this.sex;
    }
    public void setScid(int scid){
        this.scid=scid;
    }
    public int getScid(){
        return scid;
    }
}
package com.fan.demo.Entrty;
import javax.persistence.*;
@Table
@Entity(name = "classes")
public class Classes {
    @Id
    @GeneratedValue(strategy= GenerationType.IDENTITY)
    private int cid;
    private String cname;
    public void setCid(int cid){
        this.cid=cid;
    }
    public int getCid(){
        return cid;
    }
    public void setCname(String cname){
        this.cname=cname;
    }
    public String getCname(){
        return cname;
    }

}

Dao层

package com.fan.demo.Dao;

import com.fan.demo.Entrty.Student;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

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

public interface IStudentDao extends JpaRepository <Student,Integer> {
    //表连接
    @Query(value = "select * from student  inner join classes on student.scid=classes.cid", nativeQuery = true)
    List<Student> findAllDate();
    //模糊查询
    @Query(value = "select * from student inner join classes on student.scid=classes.cid where sname like concat('%',:sname,'%') ", nativeQuery = true)
    List<Student> findName(@Param("sname") String sname);
    //分页
    @Query(value = "select * from student inner join classes on student.scid =classes.cid", nativeQuery = true)
    Page<Student> findAllDatePage(Pageable pageable);
}
package com.fan.demo.Dao;

import com.fan.demo.Entrty.Classes;
import org.springframework.data.jpa.repository.JpaRepository;


public interface IClassesDao extends JpaRepository<Classes,Integer> {

}

Serverse

package com.fan.demo.Service;
import com.fan.demo.Dao.IClassesDao;
import com.fan.demo.Dao.IStudentDao;
import com.fan.demo.Entrty.Student;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
public class StudentService {
    @Resource
    IStudentDao iStudent;
    //添加数据
    public void doCreat(Student student1) {
        iStudent.save(student1);
    }
    public void doDelete(int sid){//删除数据
        iStudent.deleteById(sid);
    }
    public Student findId(int sid) {//修改数据
        return iStudent.getOne(sid);
    }
    //模糊查询
    public List<Student> findName(String sname) {
        return iStudent.findName(sname);
    }
    public List<Student> findAll() {//表连接
        return iStudent.findAllDate();
    }
    //分页
    public Page<Student> findAllDatePage(Pageable pageable) {
        return iStudent.findAllDatePage(pageable);
    }
}

package com.fan.demo.Service;

import com.fan.demo.Dao.IClassesDao;
import com.fan.demo.Entrty.Classes;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ClassesService {
    @Resource
    IClassesDao iClassesDao;
    public List<Classes> findAll() {
        return iClassesDao.findAll();
    }
    //下拉框
    public List<Classes> find() {
        return iClassesDao.findAll();
    }

}

Controller控制层

package com.fan.demo.StudentController;
import com.fan.demo.Dao.IStudentDao;
import com.fan.demo.Entrty.Classes;
import com.fan.demo.Entrty.Student;
import com.fan.demo.Service.ClassesService;
import com.fan.demo.Service.StudentService;
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.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/fan")
public class StudentController {
    @Autowired
    StudentService studentService;
    @Autowired
    ClassesService classesService;
    @Autowired
    IStudentDao iStudent;

    @RequestMapping("/qing")//显示页面
    public String toIndex(Model model) {
        List<Student> list = iStudent.findAll();
        model.addAttribute("student", list);
        return "index";
    }
    //添加页面
    @RequestMapping("/doAdd")
    public String doAdd(Model model) {
        //添加的表连接
        List <Student> list = studentService.findAll();
        model.addAttribute("student", list);
        List<Classes> list1 = classesService.findAll();
        model.addAttribute("classes", list1);
        return "add";
    }
    @RequestMapping("/toAdd")
    public String toAdd(Student student1) {
        studentService.doCreat(student1);
        return "redirect:/fan/qing";

    }
    //删除数据
    @RequestMapping("/doDelete")
//    public String doDelete(HttpServletRequest request) {
//        int sid = Integer.parseInt(request.getParameter("sid"));
//        iStudent.deleteById(sid);
//        return "redirect:/fan/qing";
//    }
// 删除第二种方法
    public String doDelete(HttpServletRequest request){
        int sid=Integer.parseInt(request.getParameter("sid"));
        studentService.doDelete(sid);
        return "redirect:/fan/qing";
    }
    //修改数据
    @RequestMapping("/toUpdate")
    public String doUpdate(HttpServletRequest request,Model model) {
        int sid = Integer.parseInt(request.getParameter("sid"));
        Student student=studentService.findId(sid);
        List<Classes> list1 = classesService.findAll();
        model.addAttribute("student",student);
        model.addAttribute("classes", list1);
        return "upload";
    }
    //模糊查询
    @RequestMapping("/findNameLike")
    public String findNameLike(HttpServletRequest request, Model model) {
        String sname = request.getParameter("sname");
        List <Student> list = studentService.findName(sname);
        model.addAttribute("student", list);
        List<Classes> list1 = classesService.findAll();
        model.addAttribute("classes", list1);
        return "index";
    }
    //表连接
    @RequestMapping("/biao")
    public String biao(Model model) {
        List<Student> list = studentService.findAll();
        model.addAttribute("student", list);
        return "index";
    }
    //下拉框
    public String toAdd(Model model) {
        List<Classes> list = classesService.findAll();
        model.addAttribute("classes", list);
        return "add";
    }
    //分页
    @RequestMapping("/PageList")
    public String findDatePage(Model model, Integer pageNum) {
        if (pageNum == null) {
            pageNum = 1;
        }
        Pageable pageable = PageRequest.of(pageNum - 1, 5);
        Page<Student> page = studentService.findAllDatePage(pageable);
        model.addAttribute("Pageinfo", page);
        return "index1";
    }
}
package com.fan.demo.StudentController;
import com.fan.demo.Entrty.Classes;
import com.fan.demo.Service.ClassesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
@Controller
@RequestMapping("/classes")
public class ClassesController {
    @Autowired
    ClassesService classesService;
    //显示页面
    @RequestMapping("/fan")
    @ResponseBody
    public List<Classes> doindex(Model model) {
        return classesService.findAll();
    }
}

前端显示页面

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<center>
<table border="1">
    <tr>
        <th colspan="6">学生信息</th>
    </tr>
    <tr>
        <td colspan="6">
            <form action="/fan/findNameLike">
                <input type="text" name="sname">
                <input type="submit" value="查询">
            </form>
        </td>
    </tr>
    <tr><th colspan="1"><a href="/fan/doAdd">添加</a></th></tr>
    <tr>
        <td>编号</td>
        <td>姓名</td>
        <td>年龄</td>
        <td>性别</td>
        <td>班级</td>
        <td>操作</td>
    </tr>
    <tr th:each="stu:${student}">
        <td th:text="${stu.sid}"></td>
        <td th:text="${stu.sname}"></td>
        <td th:text="${stu.age}"></td>
        <td th:text="${stu.sex}==1?'':''"></td>
        <td th:text="${stu.classes.cname}"></td>
        <td><a th:href="@{/fan/doDelete(sid=${stu.sid})}">删除</a>|
            <a th:href="@{/fan/toUpdate(sid=${stu.sid})}">修改</a>
        </td>
    </tr>
</table>
</center>
</body>
</html>

添加

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <script src="https://cdn.staticfile.org/jquery/2.0.0/jquery.min.js"></script>
    <title>Title</title>
</head>
<body>
<center>
    <form action="/fan/toAdd" method="post">
        <table>
            <th>添加页面</th>
            <tr>
                <td>姓名:<input type="text" name="sname" id="sname"></td>
            </tr>
            <tr>
                <td>性别:
                    <input type="radio" name="sex" value="1"><input type="radio" name="sex" value="2"></td>
            </tr>
            <tr>
                <td>
                    年龄:<input type="text" name="age" id="age">
                </td>
            </tr>
            <tr>
                <td>
                   班级:<select name="scid" id="">
                        <option th:each="classes:${classes}" th:value="${classes.cid}" th:text="${classes.cname}"></option>
                    </select>
                </td>
            </tr>
        </table>
         <input type="submit" value="提交">
    </form>
</center>
</body>
</html>

修改

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<center>
    <form action="/fan/toAdd" th:each="stu:${student}">
        姓名:<input type="text" name="sname" th:value="${stu.sname}"><br>
        年龄:<input type="text" name="age"   th:value="${stu.age}"><br>
        学生性别:<input type="radio" name="sex" value="1"><input type="radio" name="sex" value="2"><br>
        班级:
        <select name="scid" id="">
            <option th:each="classes:${classes}" th:value="${classes.cid}" th:text="${classes.cname}"></option>
        </select>
        <input type="hidden" name="sid"     th:value="${student.sid}"><!--修改的关键所在-->
        <input type="submit" value="修改">
    </form>
</center>
</body>
</html>

分页

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<center>
    <table width="80%" border="1">
        <tr>
            <th colspan="6">学生信息</th>
        </tr>
        <tr>
            <td colspan="7">
                <form action="/fan/findNameLike">
                    <input type="text" name="sname">
                    <input type="submit" value="查询">
                </form>
            </td>
        </tr>
        <tr>
            <th colspan="1"><a href=/fan/doAdd>添加</a></th>
        </tr>
        <form action="/fan/doDelete">
            <tr>
                <td>编号 <input type="submit" value="删除"></td>
                <td>姓名</td>
                <td>成绩</td>
                <td>性别</td>
                <td>班级</td>
                <td>操作</td>
            </tr>
            <tr th:each="stu:${Pageinfo.content}">
                <td><input type="checkbox" name="sid" th:text="${stu.sid}" th:value="${stu.sid}"></td>
                <td th:text="${stu.sname}"></td>
                <td th:text="${stu.age}"></td>
                <td th:text="${stu.sex}==1?'':''"></td>
                <td th:text="${stu.classes.cname}"></td>
<!--                <td><img th:src="@{${student.file}}" height="100" width="100">-->
                <td><a th:href="@{/fan/doDelete(sid=${stu.sid})}">删除</a>|
                    <a th:href="@{/fan/toUpdate(sid=${stu.sid})}">修改</a>
            </tr>
        </form>
    </table>
    <ul>
        当前第 <span th:text="${Pageinfo.number} +1"></span> 页
        总共<span th:text="${Pageinfo.totalPages}"></span> 页
        总共<span th:text="${Pageinfo.totalElements}"></span> 条数据
        <a th:href="@{/fan/PageList(pageNum=1)}">首页</a>
        <a th:href="@{/fan/PageList(pageNum=${Pageinfo.hasPrevious()}?${Pageinfo.getNumber()}:1)}">上一页</a>
        <a th:href="@{/fan/PageList(pageNum=${Pageinfo.hasNext()}?${Pageinfo.getNumber()+2}:${Pageinfo.totalPages})}">下一页</a>
        <a th:href="@{/fan/PageList(pageNum=${Pageinfo.totalPages})}">尾页</a>
    </ul>
</center>
</body>
</html>
©️2020 CSDN 皮肤主题: 1024 设计师:上身试试 返回首页