springboot實(shí)現(xiàn)學(xué)生管理系統(tǒng)
本文實(shí)例為大家分享了SpringBoot實(shí)現(xiàn)學(xué)生管理系統(tǒng),供大家參考,具體內(nèi)容如下
一、創(chuàng)建springboot項(xiàng)目
點(diǎn)擊下一步
點(diǎn)擊下一步,選擇要添加的依賴
點(diǎn)擊下一步,再點(diǎn)擊完成
修改pom.xml如下
<?xml version="1.0" encoding="UTF-8"?> <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.0</modelVersion> ?? ?<parent> ?? ??? ?<groupId>org.springframework.boot</groupId> ?? ??? ?<artifactId>spring-boot-starter-parent</artifactId> ?? ??? ?<version>2.6.7</version> ?? ??? ?<relativePath/> <!-- lookup parent from repository --> ?? ?</parent> ?? ?<groupId>com.young</groupId> ?? ?<artifactId>springboot04</artifactId> ?? ?<version>0.0.1-SNAPSHOT</version> ?? ?<name>springboot04</name> ?? ?<description>Demo project for Spring Boot</description> ?? ?<properties> ?? ??? ?<java.version>1.8</java.version> ?? ?</properties> ?? ?<dependencies> ?? ??? ?<dependency> ?? ??? ??? ?<groupId>org.springframework.boot</groupId> ?? ??? ??? ?<artifactId>spring-boot-starter-thymeleaf</artifactId> ?? ??? ?</dependency> ?? ??? ?<dependency> ?? ??? ??? ?<groupId>org.springframework.boot</groupId> ?? ??? ??? ?<artifactId>spring-boot-starter-web</artifactId> ?? ??? ?</dependency> ?? ??? ?<dependency> ?? ??? ??? ?<groupId>org.springframework.boot</groupId> ?? ??? ??? ?<artifactId>spring-boot-starter-web-services</artifactId> ?? ??? ?</dependency> ?? ??? ?<dependency> ?? ??? ??? ?<groupId>org.springframework.boot</groupId> ?? ??? ??? ?<artifactId>spring-boot-starter-jdbc</artifactId> ?? ??? ?</dependency> ?? ??? ?<dependency> ?? ??? ??? ?<groupId>org.mybatis.spring.boot</groupId> ?? ??? ??? ?<artifactId>mybatis-spring-boot-starter</artifactId> ?? ??? ??? ?<version>2.1.0</version> ?? ??? ?</dependency> ?? ??? ?<dependency> ?? ??? ??? ?<groupId>mysql</groupId> ?? ??? ??? ?<artifactId>mysql-connector-java</artifactId> ?? ??? ??? ?<scope>runtime</scope> ?? ??? ?</dependency> ?? ??? ?<dependency> ?? ??? ??? ?<groupId>org.springframework.boot</groupId> ?? ??? ??? ?<artifactId>spring-boot-starter-test</artifactId> ?? ??? ??? ?<scope>test</scope> ?? ??? ?</dependency> ?? ??? ?<dependency> ?? ??? ??? ?<groupId>org.webjars</groupId> ?? ??? ??? ?<artifactId>jquery</artifactId> ?? ??? ??? ?<version>3.3.1</version> ?? ??? ?</dependency> ?? ??? ?<dependency> ?? ??? ??? ?<groupId>org.webjars</groupId> ?? ??? ??? ?<artifactId>bootstrap</artifactId> ?? ??? ??? ?<version>4.0.0</version> ?? ??? ?</dependency> ?? ?</dependencies> ?? ?<build> ?? ??? ?<plugins> ?? ??? ??? ?<plugin> ?? ??? ??? ??? ?<groupId>org.springframework.boot</groupId> ?? ??? ??? ??? ?<artifactId>spring-boot-maven-plugin</artifactId> ?? ??? ??? ?</plugin> ?? ??? ?</plugins> ?? ?</build> </project>
修改resources目錄下的application.yml,代碼如下
spring: ? thymeleaf: ? ? prefix: classpath:/templates/ ? ? suffix: .html ? mvc: ? ? view: ? ? ? static-path-pattern: /static/** ? datasource: ? ? url: jdbc:mysql://localhost:3306/springboot?useSSL=false&serverTimezone=UTC ? ? username: root ? ? password: 3fa4d180 ? ? driver-class-name: com.mysql.cj.jdbc.Driver mybatis: ? mapper-locations: classpath:mapper/*.xml
我們在我們創(chuàng)建的目錄下,創(chuàng)建pojo軟件包,在該包下創(chuàng)建Student.java
public class Student { ? ? private Integer id; ? ? private String name; ? ? private String grade; ? ? private String major; ? ? public Integer getId() { ? ? ? ? return id; ? ? } ? ? public void setId(Integer id) { ? ? ? ? this.id = id; ? ? } ? ? public String getName() { ? ? ? ? return name; ? ? } ? ? public void setName(String name) { ? ? ? ? this.name = name; ? ? } ? ? public String getGrade() { ? ? ? ? return grade; ? ? } ? ? public void setGrade(String grade) { ? ? ? ? this.grade = grade; ? ? } ? ? public String getMajor() { ? ? ? ? return major; ? ? } ? ? public void setMajor(String major) { ? ? ? ? this.major = major; ? ? } ? ? @Override ? ? public String toString(){ ? ? ? ? return "Student{id="+id+",name="+name+ ? ? ? ? ? ? ? ? ",grade="+grade+",major="+major+ ? ? ? ? ? ? ? ? "}"; ? ? } }
我們在創(chuàng)建一個(gè)mapper軟件包,創(chuàng)建StudentMapper接口
@Mapper public interface StudentMapper { ? ? List<Student>listStudent(); ? ? List<Student>queryStudentByValue(String value); ? ? int saveStudent(Student student); ? ? int deleteStudent(Integer id); ? ? int updateStudent(Student student); }
創(chuàng)建一個(gè)service軟件包,創(chuàng)建StudentService
@Service public class StudentService { ? ? @Autowired ? ? private StudentMapper studentMapper; ? ? public List<Student>listStudent(){ ? ? ? ? return studentMapper.listStudent(); ? ? } ? ? public List<Student>queryStudentByValue(String value){ ? ? ? ? return studentMapper.queryStudentByValue(value); ? ? } ? ? public int saveStudent(Student student){ ? ? ? ? return studentMapper.saveStudent(student); ? ? } ? ? public int deleteStudent(Integer id){ ? ? ? ? return studentMapper.deleteStudent(id); ? ? } ? ? public int updateStudent(Student student){ ? ? ? ? return studentMapper.updateStudent(student); ? ? } }
創(chuàng)建一個(gè)controller軟件包,創(chuàng)建StudentController
@RestController @RequestMapping("/student") public class StudentController { ? ? @Autowired ? ? private StudentService studentService; ? ? @RequestMapping("/listStudent") ? ? public ModelAndView listStudent(){ ? ? ? ? List<Student>studentList=studentService.listStudent(); ? ? ? ? ModelAndView modelAndView=new ModelAndView(); ? ? ? ? modelAndView.addObject("studentList",studentList); ? ? ? ? modelAndView.setViewName("/listStudent"); ? ? ? ? return modelAndView; ? ? } ? ? @RequestMapping("/queryStudentByValue") ? ? public ModelAndView queryStudentByValue(String value){ ? ? ? ? List<Student>studentList=studentService.queryStudentByValue(value); ? ? ? ? ModelAndView modelAndView=new ModelAndView(); ? ? ? ? modelAndView.addObject("studentList",studentList); ? ? ? ? modelAndView.setViewName("/listStudent"); ? ? ? ? return modelAndView; ? ? } ? ? @RequestMapping("/saveStudent") ? ? public ModelAndView saveStudent(Student student){ ? ? ? ? ModelAndView modelAndView=new ModelAndView(); ? ? ? ? studentService.saveStudent(student); ? ? ? ? modelAndView.setViewName("forward:/student/listStudent"); ? ? ? ? return modelAndView; ? ? } ? ? @RequestMapping("/updateStudent") ? ? public ModelAndView updateStudent(Student student){ ? ? ? ? ModelAndView modelAndView=new ModelAndView(); ? ? ? ? studentService.updateStudent(student); ? ? ? ? modelAndView.setViewName("forward:/student/listStudent"); ? ? ? ? return modelAndView; ? ? } ? ? @RequestMapping("/deleteStudent") ? ? public ModelAndView deleteStudent(Integer id){ ? ? ? ? ModelAndView modelAndView=new ModelAndView(); ? ? ? ? studentService.deleteStudent(id); ? ? ? ? modelAndView.setViewName("forward:/student/listStudent"); ? ? ? ? return modelAndView; ? ? } }
在resources目錄下,創(chuàng)建mapper目錄,在該目錄下,放入我們的StudentMapper.xml配置
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper ? ? ? ? PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" ? ? ? ? "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.young.springboot04.mapper.StudentMapper"> ? ? <select id="listStudent" resultType="com.young.springboot04.pojo.Student"> ? ? ? ? select * from student ? ? </select> ? ? <select id="queryStudentByValue" parameterType="string" resultType="com.young.springboot04.pojo.Student"> ? ? ? ? select * from student ? ? ? ? where id like '%${value}%' or name like '%${value}%' or grade like '%${value}%' ? ? ? ? or major like '%${value}%' ? ? </select> ? ? <insert id="saveStudent" parameterType="com.young.springboot04.pojo.Student"> ? ? ? ? insert into student ? ? ? ? values(#{id},#{name},#{grade},#{major}) ? ? </insert> ? ? <delete id="deleteStudent" parameterType="int"> ? ? ? ? delete from student ? ? ? ? where id=#{id} ? ? </delete> ? ? <update id="updateStudent" parameterType="com.young.springboot04.pojo.Student"> ? ? ? ? update student ? ? ? ? set name=#{name},grade=#{grade},major=#{major} ? ? ? ? where id=#{id} ? ? </update> </mapper>
在我們的static目錄下, 放入我們需要的css目錄,我這里用的是pio框架的css目錄
接著我們在templates目錄下創(chuàng)建listStudent.html,用于顯示內(nèi)容
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> ? ? <meta charset="UTF-8"> ? ? <title>學(xué)生列表</title> ? ? <meta name="viewport" content="width=device-width,initial-scale=1"> ? <link rel="stylesheet" href="../static/css/pico.min.css" th:href="@{/css/pico.min.css}"> ? <style> ? ? #bodyLeft{ ? ? ? position:relative; ? ? ? width:66%; ? ? ? float:left; ? ? } ? ? #bodyRight{ ? ? ? position:relative; ? ? ? width:29%; ? ? ? float:right; ? ? } ? ? #editForm{ ? ? ? position:absolute; ? ? ? margin-top:5%; ? ? ? margin-left:35%; ? ? ? width:30%; ? ? ? background-color:white; ? ? ? z-index:1; ? ? ? border:1px solid black; ? ? ? modal:true; ? ? ? border-radius:5%; ? ? } ? </style> ? <script type="text/javascript" th:inlne="javascript"> ? ? function editStudent(student){ ? ? ? var id=student.id; ? ? ? var name=student.name ? ? ? var grade=student.grade ? ? ? var major=student.major ? ? ? var editForm=document.getElementById("editForm"); ? ? ? editForm.style.display="block"; ? ? ? editForm.innerHTML= ? ? ? "<form action='http://localhost:8080/student/updateStudent' method='post' style='width:80%;margin-left:10%;margin-top:10%;'>"+ ? ? ? "<label for='name'>姓名<input type='text' id='name' value="+name+" name='name'></label>"+ ? ? ? "<label for='grade'>年級<input type='text' id='grade' value="+grade+" name='grade'></label>"+ ? ? ? "<label for='major'>專業(yè)<input type='text' id='major' value="+major+" name='major'></label>"+ ? ? ? "<label for='id' style='display:none;'>學(xué)號<input type='text' id='id' value="+id+" name='id' hidden></label>"+ ? ? ? "<input type='submit' value='修改'>"+ ? ? ? "<button style='outline'><a th:href='@{/student/listStudent}' style='color:white;'>取消</a></button>"+ ? ? ? "</form>"; ? ? } ? </script> </head> <body> ? <!--隱藏的表單彈窗--> <div id="editForm" style="display:none"></div> <div class="container" style="margin-top:5%;"> ? <div id="bodyLeft"> ? ? <table role="grid"> ? ? ? <tr> ? ? ? ? <th scope="col">學(xué)號</th> ? ? ? ? <th scope="col">姓名</th> ? ? ? ? <th scope="col">年級</th> ? ? ? ? <th scope="col">專業(yè)</th> ? ? ? ? <th scope="col">編輯</th> ? ? ? ? <th scope="col">刪除</th> ? ? ? </tr> ? ? ? <div th:each="student:${studentList}"> ? ? ? ? <tr> ? ? ? ? ? <td scope="row" th:text="${student.id}">學(xué)號</td> ? ? ? ? ? <td th:text="${student.name}">姓名</td> ? ? ? ? ? <td th:text="${student.grade}">年級</td> ? ? ? ? ? <td th:text="${student.major}">專業(yè)</td> ? ? ? ? ? <td><a th:onclick="editStudent([[${student}]])" href="#">編輯</a></td> ? ? ? ? ? <td><a th:href="@{/student/deleteStudent(id=${student.id})}">刪除</a></td> ? ? ? ? </tr> ? ? ? </div> ? ? </table> ? </div> ? <div id="bodyRight"> ? ? <form th:action="@{/student/saveStudent}" method="post"> ? ? ? <label for="name"> ? ? ? ? 姓名<input type="text" id="name" name="name" placeholder="請輸入姓名"> ? ? ? </label> ? ? ? <label for="grade"> ? ? ? ? 年級<input type="text" id="grade" name="grade" placeholder="請輸入年級"> ? ? ? </label> ? ? ? <label for="major"> ? ? ? ? 專業(yè)<input type="text" id="major" name="major" placeholder="請輸入專業(yè)"> ? ? ? </label> ? ? ? <input type="submit" value="修改"> ? ? </form> ? </div> </div> </body> </html>
運(yùn)行效果如下:
點(diǎn)擊編輯
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
mybatis.type-aliases-package之巨坑的解決
這篇文章主要介紹了mybatis.type-aliases-package之巨坑的解決,具有很好的參考價(jià)值,希望對大家有所幫助。2021-09-09實(shí)例詳解Spring Boot實(shí)戰(zhàn)之Redis緩存登錄驗(yàn)證碼
本章簡單介紹redis的配置及使用方法,本文示例代碼在前面代碼的基礎(chǔ)上進(jìn)行修改添加,實(shí)現(xiàn)了使用redis進(jìn)行緩存驗(yàn)證碼,以及校驗(yàn)驗(yàn)證碼的過程。感興趣的的朋友一起看看吧2017-08-08springcloud 中 zuul 修改請求參數(shù)信息的方法
這篇文章主要介紹了springcloud 中 zuul 修改請求參數(shù)信息的方法,需要的朋友可以參考下2018-02-02Spring boot如何基于攔截器實(shí)現(xiàn)訪問權(quán)限限制
這篇文章主要介紹了Spring boot如何基于攔截器實(shí)現(xiàn)訪問權(quán)限限制,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10Jackson中json格式的字符串與對象的互相轉(zhuǎn)換方式
這篇文章主要介紹了Jackson中json格式的字符串與對象的互相轉(zhuǎn)換方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07基于LinkedHashMap實(shí)現(xiàn)LRU緩存
LinkedHashMap是Java集合中一個(gè)常用的容器,它繼承了HashMap, 是一個(gè)有序的Hash表。那么該如何基于LinkedHashMap實(shí)現(xiàn)一個(gè)LRU緩存呢?本文將介紹LinkedHashMap的實(shí)現(xiàn)原理,感興趣的同學(xué)可以參考一下2023-05-05為何修改equals方法時(shí)還要重寫hashcode方法的原因分析
這篇文章主要介紹了為何修改equals方法時(shí)還要重寫hashcode方法的原因分析,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06