程序员社区

spring-boot搭建学生管理系统附总结

Hello,欢迎来到程序员社区。 今天聊一聊 spring-boot搭建学生管理系统附总结,希望对大家有所帮助。

Java面试手册PDF下载:点击下载最全Java面试手册

                
                

先放效果图
spring-boot搭建学生管理系统附总结插图

  1. 目录结构:spring-boot搭建学生管理系统附总结插图1
  2. pom.xml

project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    modelVersion>4.0.0modelVersion>
    parent>
        groupId>org.springframework.bootgroupId>
        artifactId>spring-boot-starter-parentartifactId>
        version>2.4.2version>
        relativePath/> 
    parent>
    groupId>com.huazeqigroupId>
    artifactId>studentdemoartifactId>
    version>0.0.1-SNAPSHOTversion>
    name>studentdemoname>
    description>Demo project for Spring Bootdescription>
    properties>
        java.version>1.8java.version>
    properties>
    dependencies>
        dependency>
            groupId>org.springframework.bootgroupId>
            artifactId>spring-boot-starter-data-jpaartifactId>
        dependency>
        dependency>
            groupId>org.springframework.bootgroupId>
            artifactId>spring-boot-starter-webartifactId>
        dependency>

        dependency>
            groupId>org.springframework.bootgroupId>
            artifactId>spring-boot-devtoolsartifactId>
            scope>runtimescope>
            optional>trueoptional>
        dependency>
        dependency>
            groupId>mysqlgroupId>
            artifactId>mysql-connector-javaartifactId>
            scope>runtimescope>
        dependency>
        dependency>
            groupId>org.springframework.bootgroupId>
            artifactId>spring-boot-starter-testartifactId>
            scope>testscope>
        dependency>

        
        dependency>
            groupId>com.alibabagroupId>
            artifactId>druidartifactId>
            version>1.1.10version>
        dependency>

        dependency>
            groupId>org.springframework.bootgroupId>
            artifactId>spring-boot-starter-thymeleafartifactId>
        dependency>

    dependencies>

    build>
        plugins>
            plugin>
                groupId>org.springframework.bootgroupId>
                artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
    build>

project>

  1. application.properties
#数据源配置
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/stu?useSSL=false&useUnicode=true&characterEncoding=utf-8&autoReconnect=true&serverTimezone=Asia/Shanghai
#3306后面,3306/数据库名,不是表名
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.initialSize=20
spring.datasource.minIdle=50
spring.datasource.maxActive=500

#上下文配置
server.port=8888
serv编程电子书汇总er.servlet.context-path=/stu

#配置jpa
#帮我们自动生成表结构
spring.jpa.properties.hibernate.hbm2ddl.auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.show-sql= true
spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true
spring.jpa.open-in-view=false

  1. Student
package com.huazeqi.studentdemo.entity;

import javax.persistence.*;
/**
 * @author:huazeqi
 * @Date:2021/2/14 12:18
 * @Description:学生类
 **/
@Entity
@Table(name = "student")
public class Student {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    private String name;
    private String age;
    private String sex;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    @Override
    public String toString() {
        return "StudJava面试手册ent{" +
                "id=" + id +
                ", name='" + name + ''' +
                ", age='" + age + ''' +
                ", sex='" + sex + ''' +
                '}';
    }
}

  1. StudentController
package com.huazeqi.studentdemo.controller;

import com.huazeqi.studentdemo.entity.Student;
import com.huazeqi.studentdemo.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/s")
public class StudentController {

    @Autowired
    private StudentService studentService;

    /**
     * 添加学生
     * @param student  参数
     * @return
     */
    @PostMapping("/add")
    public Student save(Student student){
        return studentService.save(student);
    }

    /**
     * 更新学生信息
     * @param student 参数
     * @return
     */
    @PostMapping("/update")
    public  Student update(Student student){
        return studentService.update(student);
    }

    /**
     * 删除学生信息
     * @param id
     */
    @GetMapping("/del/{id}")
    public  String  delete(@PathVariable int id){
        studentService.delete(id);
        return "delete yes";
    }

    /**
     * 通过名字查询学生信息
     * @param name
     * @return
     */
    @GetMapping("/findByName/{name}")
    public ListStudent> findStuByName(@PathVariable  String name){
        return studentService.findStuByName(name);
    }

    /**
     * 分页查询所有学生信息
     * @param
     * @return
     */
    @GetMapping("/query")
    public PageStudent> findByPage(Integer page , HttpServletResponse response){

        response.setHeader("Access-Control-Allow-Origin","*");//异步请求响应头

        if(page==null || page0){
            page = 0;
        }else{
            page -= 1;
        }

        return studentService.findAll(page,5);
    }

    @GetMapping("/findById")
    public Student findStuById(int id){
        return studentService.findStuById(id);
    }

    @RequestMapping("/Hi")
    public ModelAndView sayHello() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("hello");
        modelAndView.addObject("key", 12345);
        //System.out.println("test");
        return modelAndView;
    }

    @GetMapping("/inde编程电子书汇总x")
    public String index(HashMap map){
        map.put("hello","springboot页面");
        //return  "index";
        return "redirect:hello.html";
    }
}

  1. StudentService
package com.huazeqi.studentdemo.service;

import com.huazeqi.studentdemo.entity.Student;
import org.springframework.data.domain.Page;

import java.util.List;

public interface StudentService {

    Student save(Student student);//添加
    Student update(Student student);//更新
    void delete(Integer id);//删除
    Student findStuById(Integer id);//查询by id
    ListStudent> findStuByName(String name);//查询by name

    /**
     *分页查询所有页
     * @param page 当前页,
     * @param pageSize  每页几条数据
     * @return
     */
    PageStudent> findAll(int page, int pageSize);
}

  1. StudentServiceImpl
package com.huazeqi.studentdemo.service;

import com.huazeqi.studentdemo.dao.StudentDao;
import com.huazeqi.studentdemo.entity.Student;
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.Service;

import java.util.List;

@Service
public class StudentServiceImpl implements StudentService{

    @Autowired
    private StudentDao studentDao;

    @Override
    public Student save(Student student) {
        return studentDao.save(student);
    }

    @Override
    public Student update(Student student) {
        return studentDao.save(student);
    }

    @Override
    public void delete(Integer id) {
        studentDao.deleteById(id);
    }

    @Override
    public Student findStuById(Integer id) {
        return studentDao编程电子书汇总.findStudentById(id);
    }

    @Override
    public ListStudent> findStuByName(String name) {
        return studentDao.findStuByName(name);
    }

    /**
     *
     * @param page 当前页,
     * @param pageSize  每页几条数据
     * @return
     */
    @Override
    public PageStudent> findAll(int page,int pageSize) {
        Pageable pageble= PageRequest.of(page,pageSize);
        return studentDao.findAll(pageble);
    }
}

  1. StudentDao
package com.huazeqi.studentdemo.dao;

import com.huazeqi.studentdemo.entity.Student;
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;

public interface StudentDao extends JpaRepositoryStudent, Integer> {

    /**
     * @Description:通过id查询学生
     * @Author:huazeqi
     * @Date:2021/2/14 12:42
     */
    Student findStudentById(Integer id);
    /**
     * @Description:通过name查询学生
     * @Author:huazeqi
     * @Date:2021/2/14 12:43
     */
    @Query(name = "findStuByName",nativeQuery = true,value =
            "select * from student where name=:name")
    ListStudent> findStuByName(@Param("name") String name);
    /**
     * @Description:通过id删除学生
     * @Author:huazeqi
     * @Date:2021/2/14 12:49
     */
    void deleteById(Integer id);
}

  1. jquery.js随便找一个jquery就可以了
  2. index.html

html>
head>
    meta charset="utf-8">
    title>title>
    script src="js/jquery.js" type="text/javascript" charset="utf-8">script>

    script type="text/javascript">

        var page = 1;
        var tp = 0;

        $(function(){
            showData();
        });

        function upPage(){
            if(page>1){
                page--;
                showData();
            }

        }

        function downPage(){
            if(pagetp){
                page++;
                showData();
            }

        }

        function showData(){
            $.ajax({
                url:'http://localhost:8888/stu/s/query?page='+page,
                success:function(result){

                    var rel = result.content;
                    tp = result.totalPages;

                    var htmlStr = ""+"";for(var i=0;irel.length;i++){
                        var stu = rel[i];
                        htmlStr += ""+"";}""

                    htmlStr +="
ID 姓名 年龄 性别 操作
"+stu.id+" "+stu.name+" "+stu.age+" "+stu.sex+" 修改删除
"
; $("#show").html(htmlStr); } }); }
script> head> body> h1 style="text-align: center;">学生管理系统h1> hr> a href="add.html">添加学生a> div id="show"> div> a href="javascript:upPage();">上一页a>a href="javascript:downPage();">下一页a> a href="query.html">查询a> body> html>
  1. add.html

html>
head>
    meta charset="utf-8">
    title>Addtitle>
head>
body>
h1>添加学生h1>
hr>
form action="http://localhost:8Java面试手册888/stu/s/add" method="post">
    p>
        姓名:input type="text" name="name" />
    p>
    p>
        年龄:input type="text" name="age" />
    p>
    p>
        性别:
        input type="radio" name="sex" value=""  checked />input type="radio" name="sex" value="" />p>
    p>
        input type="submit" value="保存" />
    p>
form>
body>
html>
  1. update.html

html>
head>
    meta charset="utf-8">
    title>Updatetitle>
head>
body>
h1 style="text-align: center;">修改学生信息h1>

form action="http://localhost:8888/stu/s/update" method="post">
    p>
        id:input type="text" name="id">
    p>
    p>
        姓名:input type="text" name="name" />
    p>
    p>
        年龄:input type="text" name="age" />
    p>
    p>
        性别:
        input type="radio" name="sex" value=""  checked />input type="radio" name="sex" value="" />p>
    p>
        input  type="submit" value="保存"  />
    p>
form>
body>
html>
  1. query.html

html>
head>
    meta charset="utf-8">
    title>title>
head>
body>
h1 style="text-align: center;">查询学生信息h1>

    form action="http://localhost:8888/stu/s/findById" method="get">
        h1>通过Id查询h1>
        p>
            id:input type="text" name="id">
        p>

        p>
            input  type="submit" value="查询"  />
        p>
    form>
body>
html>

一些小结:

  1. spring Data JPA(实际上是Hibernate的封装):JPA的作用就是通过对象操作数据库的,不用编写sql语句。
    JPA
    Spring JPA查询,JPA 根据方法名字查询详细介绍
  2. devtools,springboot的另一大优点,可以实现热部署
    spring-boot搭建学生管理系统附总结插图2
    配置为true是开启热部署,false是关闭热部署。
    SpringBoot 在idea中实现热部署
  3. thymeleaf,模板,动态页面放置在这里,静态页面放置在static目录下
    spring-boot搭建学生管理系统附总结插图3
  4. yml的配置mysql已经从spring.datasource.driverClassName=com.mysql.jdbc.Driver调整为spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
  5. @PathVariable注解,可以用来映射URL中的占位符到目标方法的参数中
    spring-boot搭建学生管理系统附总结插图4
  6. 分页是spring-data-common包下的,是继承了JpaRepository类实现的spring-boot搭建学生管理系统附总结插图5
  7. 可以点击这里直接进入页展示页面spring-boot搭建学生管理系统附总结插图6

> 时间不一定能证明很多东西,但是一定能看透很多东西。坚信自己的选择,不动摇,使劲跑,明天会更好。

赞(0) 打赏
未经允许不得转载:IDEA激活码 » spring-boot搭建学生管理系统附总结

一个分享Java & Python知识的社区