数据库
使用的是MySQL数据库
数据库的写的字段有些多所以有些代码比较繁杂
目录结构
application.yml
1901java为数据库
spring:
thymeleaf:
cache: false
datasource:
username: root
password: root
# 数据库地址和 编码 数据加密 设置时区
url: jdbc:mysql://localhost:3306/1901java?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=GMT%2B8
#serverTimezone=UTC
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
server:
port: 8080
servlet:
context-path:
Entity–》Student
package com.yyy.demo.Entity;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import java.util.Date;
@Table
@Entity(name = "student")
public class Student {
@Id
private int sid;
private String sname;
private int sex;
private String scard;
private String stelephone;
private Date sbirthday;
private int cid;
//此处省略set,get方法
Dao–》StudentDao
继承的JpaRepository 接口自带
(使用了里面的findAll和save方法)
(修改的方法其实也可以直接调用save添加方法)
package com.yyy.demo.Dao;
import com.yyy.demo.Entity.Student;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Component;
import javax.transaction.Transactional;
import java.util.Date;
import java.util.Map;
@Component
public interface StudentDao extends JpaRepository<Student,Integer> {
//删除
@Transactional
@Modifying
@Query(name="deleteBySid",value="delete from student where sid= :sid",nativeQuery=true)
void deleteBySid(@Param("sid") Integer sid);
//按照id查询
@Query(name="findBySid",value="select * from student where sid = :sid",nativeQuery=true)
Map findBySid(@Param("sid") Integer sid);
//修改
@Transactional
@Modifying
@Query(name="update",
value="update student set sname=?1 ,sex=?2,scard=?3,stelephone=?4,sbirthday=?5,cid=?6 where sid=?7",
nativeQuery=true)
void update(String name, int sex, String scard, String stelephone, Date sbirthday,int cid,int sid);
}
Service–》StudentService
package com.yyy.demo.Service;
import com.yyy.demo.Dao.StudentDao;
import com.yyy.demo.Entity.Student;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;
@Service
public class StudentService {
@Resource
StudentDao studentDao;
//查询全部
public List<Student> findAll(){
return studentDao.findAll();
}
//添加
public Student addAll(Student student){
return studentDao.save(student);
}
//删除
public void deleteBySid(int sid){
studentDao.deleteBySid(sid);
}
//根据sid查询
public Map findBySid(int sid){
return studentDao.findBySid(sid);
}
//修改
public void update(String name, int sex, String scard, String stelephone, Date sbirthday, int cid, int sid){
studentDao.update(name,sex,scard,stelephone,sbirthday,cid,sid);
}
}
Controller–》StudentController
package com.yyy.demo.Controller;
import com.yyy.demo.Entity.Student;
import com.yyy.demo.Service.StudentService;
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 javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;
@Controller
@RequestMapping("/toStudent")
public class StudentController {
@Autowired
StudentService studentService;
//查询
@RequestMapping("/findAll")
public String findAll(Model model){
List<Student> list = studentService.findAll();
model.addAttribute("student",list);
return "Student";
}
//跳转添加页面
@RequestMapping("/toadd")
public String toadd(){
return "add";
}
//获取添加页面内容,带哦用添加方法,添加
@RequestMapping("/addAll")
public String addAll(HttpServletRequest request) throws ParseException {
int sid = Integer.parseInt(request.getParameter("sid"));
String sname = request.getParameter("sname");
int sex = Integer.parseInt(request.getParameter("sex"));
String scard = request.getParameter("scard");
String stelephone = request.getParameter("stelephone");
java.util.Date date = new SimpleDateFormat("yyyy-MM-dd").parse(request.getParameter("sbirthday"));
int cid = Integer.parseInt(request.getParameter("cid"));
Student student = new Student();
student.setSid(sid);
student.setSname(sname);
student.setSex(sex);
student.setScard(scard);
student.setStelephone(stelephone);
student.setSbirthday(date);
student.setCid(cid);
studentService.addAll(student);
return "redirect:/toStudent/findAll";
}
//获取页面的sid,调用删除方法
@RequestMapping("/deleteBySid")
public String deleteBySid(HttpServletRequest request){
int sid = Integer.parseInt(request.getParameter("sid"));
studentService.deleteBySid(sid);
return "redirect:/toStudent/findAll";
}
//跳转到修改页面
@RequestMapping("/toupload")
public String toupload(HttpServletRequest request,Model model){
int sid = Integer.parseInt(request.getParameter("sid"));
Map map = studentService.findBySid(sid);
model.addAttribute("student",map);
return "upload";
}
//修改--可以不写,用添加方法代替
@RequestMapping("/doupdate")
public String doupdate(HttpServletRequest request) throws ParseException {
int sid = Integer.parseInt(request.getParameter("sid"));
String sname = request.getParameter("sname");
int sex = Integer.parseInt(request.getParameter("sex"));
String scard = request.getParameter("scard");
String stelephone = request.getParameter("stelephone");
java.util.Date date = new SimpleDateFormat("yyyy-MM-dd").parse(request.getParameter("sbirthday"));
int cid = Integer.parseInt(request.getParameter("cid"));
Student student = new Student();
student.setSid(sid);
student.setSname(sname);
student.setSex(sex);
student.setScard(scard);
student.setStelephone(stelephone);
student.setSbirthday(date);
student.setCid(cid);
studentService.update(sname,sex,scard,stelephone,date,cid,sid);
return "redirect:/toStudent/findAll";
}
}
HTML页面
Student.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 border="1" width="80%">
<tr>
<th colspan="9"><h1>学生信息展示</h1></th>
</tr>
<tr>
<th colspan="9"><a href="/toStudent/toadd">添加</a></th>
</tr>
<tr>
<td>编号</td>
<td>姓名</td>
<td>性别</td>
<td>身份证</td>
<td>手机号</td>
<td>生日</td>
<td>班级</td>
<td>操作</td>
</tr>
<tr th:each = "student:${student}">
<td th:text="${student.sid}"></td>
<td th:text="${student.sname}"></td>
<td th:text="${student.sex}"></td>
<td th:text="${student.scard}"></td>
<td th:text="${student.stelephone}"></td>
<td th:text="${student.sbirthday}"></td>
<td th:text="${student.cid}"></td>
<td>
<a th:href="@{/toStudent/deleteBySid(sid=${student.sid})}">删除</a>
<a th:href="@{/toStudent/toupload(sid=${student.sid})}">修改</a>
</td>
</tr>
</table>
</center>
</body>
</html>
add.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>
<h1>添加学生信息</h1>
<table border="1">
<form action="/toStudent/addAll" method="post">
<tr>
<td>编号:</td>
<td><input type="text" name="sid"/></td>
</tr>
<tr>
<td>姓名:</td>
<td><input type="text" name="sname"/></td>
</tr>
<tr>
<td>性别:</td>
<td><input type="text" name="sex"/></td>
</tr>
<tr>
<td>身份证:</td>
<td><input type="text" name="scard"/></td>
</tr>
<tr>
<td>手机号:</td>
<td><input type="text" name="stelephone"/></td>
</tr>
<tr>
<td>生日:</td>
<td><input type="date" name="sbirthday"/></td>
</tr>
<tr>
<td>cid:</td>
<td><input type="text" name="cid"></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="添加">
<input type="reset" value="取消">
</td>
</tr>
</form>
</table>
</center>
</body>
</html>
upload.html
调用根据sid查询的方法,获取原先的值
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<center>
<h1>修改学生信息</h1>
<table border="1">
<form action="/toStudent/doupdate" method="post">
<tr>
<td>编号:</td>
<td><input type="text" th:value="${student.sid}" readonly name="sid" /><font color="red">慢慢改</font></td>
</tr>
<tr>
<td>姓名:</td>
<td><input type="text" th:value="${student.sname}" name="sname"/></td>
</tr>
<tr>
<td>性别:</td>
<td><input type="text" th:value="${student.sex}" name="sex"/></td>
</tr>
<tr>
<td>身份证:</td>
<td><input type="text" th:value="${student.scard}" name="scard"/></td>
</tr>
<tr>
<td>手机号:</td>
<td><input type="text" th:value="${student.stelephone}" name="stelephone"/></td>
</tr>
<tr>
<td>生日:</td>
<td><input type="date" th:value="${student.sbirthday}" name="sbirthday"/></td>
</tr>
<tr>
<td>cid:</td>
<td><input type="text" th:value="${student.cid}" name="cid"></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="修改">
<input type="reset" value="取消">
</td>
</tr>
</form>
</table>
</center>
</body>
</html>
世界上什么都有,就是没有如果。