Using springboot to write a student management system

Time:2021-4-8
requirement:

Student information is composed of ID, name, gender and admission time, which can add, delete, modify and query student information.

preparation in advance
  • Create a new spring starter project
  • Add the following dependencies

    • MySQL Driver
    • JDBC API
    • MyBatis Framework
    • Srping Web
    • thymeleaf
The application configuration file is as follows:
#server
server.port=80

#close banner
spring.main.banner-mode=off
spring.datasource.url=jdbc:mysql:///student?serverTimezone=GMT%2B8&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=root

#spring mybatis
mybatis.mapper-locations=classpath:/mapper/*/*.xml

#spring log
logging.level.com.cy=debug

#spring thymeleaf
spring.thymeleaf.cache=false
spring.thymeleaf.prefix=classpath:/templates/pages/
spring.thymeleaf.suffix=.html
The project contents are as follows:

Using springboot to write a student management system

The encapsulated POJO object is Stu.java
package com.cy.pj.stu.pojo;

import java.util.Date;

public class Stu {
    private Long id;
    private String name;
    private String gender;
    private Date admissionTime;
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getGender() {
        return gender;
    }
    public void setGender(String gender) {
        this.gender = gender;
    }
    public Date getAdmissionTime() {
        return admissionTime;
    }
    public void setAdmissionTime(Date admissionTime) {
        this.admissionTime = admissionTime;
    }
    @Override
    public String toString() {
        return "Stu [id=" + id + ", name=" + name + ", gender=" + gender + ", admissionTime=" + admissionTime + "]";
    }
    

}
Dao layer interface code is
package com.cy.pj.stu.dao;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.cy.pj.stu.pojo.Stu;
@Mapper
public interface StuDao {
    //Query information
    @Select("select*from stu")
    List<Stu>findStu();
    
    //Delete record
    @Delete("delete from stu where id=#{id}")
    void deleteById(Integer id);
    
    //Add information
    @Insert("insert into stu(name,gender,admissionTime) value(#{name},#{gender},now())")
    void insertStu(Stu stu);
    
    //Modify information
    @Select("select*from stu where id=#{id}")
    Stu findById(Integer id);
    @Update("update stu set name=#{name},gender=#{gender} where id=#{id}")
    void updateById(Stu stu);

}
The service layer interface code is
package com.cy.pj.stu.service;

import java.util.List;

import com.cy.pj.stu.pojo.Stu;

public interface StuService {
    List<Stu>findStu();
    void deleteById(Integer id);
    void insertStu(Stu stu);
    Stu findById(Integer id);
    void updateById(Stu stu);
    

}
The service layer interface implementation class code is
package com.cy.pj.stu.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.cy.pj.stu.dao.StuDao;
import com.cy.pj.stu.pojo.Stu;
import com.cy.pj.stu.service.StuService;
@Service
public class StuServiceImpl implements StuService {    
    @Autowired
    private StuDao stuDao;
    @Override
    public List<Stu> findStu() {
        return stuDao.findStu();
    }
    @Override
    public void deleteById(Integer id) {
        stuDao.deleteById(id);
    }
    @Override
    public void insertStu(Stu stu) {
        stuDao.insertStu(stu);
    }
    @Override
    public Stu findById(Integer id) {
        return stuDao.findById(id);
    }
    @Override
    public void updateById(Stu stu) {
        stuDao.updateById(stu);
    }
}
The stucontroller class code of controller layer is
package com.cy.pj.stu.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.cy.pj.stu.pojo.Stu;
import com.cy.pj.stu.service.StuService;

@Controller
@RequestMapping("/stu/")
public class StuController {
    
    @Autowired
    private StuService stuService;
    
    @RequestMapping("doFindStu")
    public String doFindStu(Model model) {
        List<Stu> list = stuService.findStu();
        model.addAttribute("stu", list);
        return "stu";
    }
    
    @RequestMapping("doDeleteById/{id}")
    public String doDeleteById(@PathVariable Integer id) {
        stuService.deleteById(id);
        return "redirect:/stu/doFindStu";
    }
    
    @RequestMapping("doinsert")
    public String doinsert() {
        return "stu_add";
    }
    @RequestMapping("doInsert")
    public String doInsert(Stu stu) {
        stuService.insertStu(stu);
        return "redirect:/stu/doFindStu";
    }
    
    @RequestMapping("doupdateById/{id}")
    public String doupdateById(@PathVariable Integer id,Model model) {
        Stu stu = stuService.findById(id);
        model.addAttribute("stu", stu);
        return "stu_mod";
    }
    @RequestMapping("doUpdateById")
    public String doUpdateById(Stu stu) {
        stuService.updateById(stu);
        return "redirect:/stu/doFindStu";
    }
    
}
The rendering of the front main page is as follows

Using springboot to write a student management system

The implementation code is
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Student Information Management System</title>
</head>
<body>
    <h1 align="center">Student Information Management System</h1>
    <h3 >
    <table align="center">
      <thead>
        <tr>
            <th >id</th>
            <th >name</th>
            <th >gender</th>
            <th >admissionTime</th>
            <th colspan="2">operation</th>
        </tr>
    </thead>
    <tbody>
        <tr th:each="s:${stu}">
            <td th:text="${s.id}">1</td>
            <td th:text="${s.name}">2</td>
            <td th:text="${s.gender}">3</td>
            <td th:text="${#dates.format(s.admissionTime, 'yyyy.MM.dd')}">4</td>
            <td>
                <a th:href="@{/stu/doDeleteById/{id}(id=${s.id})}">delete</a>
                </td>
            <td>
                <a th:href="@{/stu/doupdateById/{id}(id=${s.id})}">update</a>
            </td>
        </tr>
    </tbody>
  </table>
  </h3>
  <h2 align="center">
    <a th:href="@{/stu/doinsert}">Add information</a>
  </h2>
</body>
</html>
The new information jump page is as follows

Using springboot to write a student management system

The implementation code is
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Add Student Information</title>
</head>
<body>
    <h1 align="center">Add Student Information</h1>
    <form th:action="@{/stu/doInsert}" method="post">
        <table align="center">
            <tr>
                <th >name</th>
                <th >gender</th>
            </tr>
            <tr>
                <td><input type="text" name="name"></td>
                <td><input type="text" name="gender"></td>
                <td><input type="submit" value="submit"></td>
            </tr>
        </table>
    </form>
    
</body>
</html>
Change the jump page to

Using springboot to write a student management system

The implementation code is
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Modify Student Information</title>
</head>
<body>
    <h1 align="center">Modify Student Information</h1>
    <form th:action="@{/stu/doUpdateById}" method="post">
    <input type="hidden" name="id" th:value="${stu.id}">
        <table align="center">
            <tr>
                <th >name</th>
                <th >gender</th>
            </tr>
            <tr>
                <td><input type="text" name="name" th:value="${stu.name}"></td>
                <td><input type="text" name="gender" th:value="${stu.gender}"></td>
                <td><input type="submit" value="submit"></td>
            </tr>
        </table>
    </form>
</body>
</html>

Recommended Today

Third party calls wechat payment interface

Step one: preparation 1. Wechat payment interface can only be called if the developer qualification has been authenticated on wechat open platform, so the first thing is to authenticate. It’s very simple, but wechat will charge 300 yuan for audit 2. Set payment directory Login wechat payment merchant platform( pay.weixin.qq . com) — > Product […]