1. 前端页面
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>学生信息管理系统</title>
<style>
body{
font-size: 15px;
}
input[type="text"]{
width: 230px;
height: 30px;
margin: 5px 8px;
}
input[type="submit"]{
background-color: lightblue;
width: 70px;
height: 40px;
color: white;
border: 0px;
font-size: 20px;
}
input[type="button"]{
background-color: lightcoral;
width: 70px;
height: 40px;
color: white;
border: 0px;
font-size: 20px;
}
</style>
</head>
<body>
<h1>学生信息管理系统MIS</h1>
<form method="post" action="http://localhost:8080/stu/add">
<div>
<span>姓名:</span>
<input type="text" placeholder="请输入姓名" name="name"/>
<br />
<span>年龄:</span>
<input type="text" placeholder="请输入年龄" name="age"/>
<div>
<span>性别:</span>
<input type="radio" name="gender" id="" value="1" />男
<input type="radio" name="gender" id="" value="0" />女
</div>
<div>
<span>爱好:</span>
<input type="checkbox" name="hobby" id="" value="tabletennis" />乒乓球
<input type="checkbox" name="hobby" id="" value="climbing" />爬山
<input type="checkbox" name="hobby" id="" value="singing" />唱歌
</div>
<div>
<span>学历:</span>
<select name="edu">
<option value="1">本科</option>
<option value="2">研究生</option>
<option value="3">博士生</option>
</select>
</div>
<div>
<span>入学日期:</span>
<input type="date" name="intime" id="" value="" />
</div>
<div>
<input type="submit" value="保存"/>
<input type="button" value="取消"/>
</div>
</div>
</form>
</body>
</html>
2. 创建实体类Student
package cn.tedu.pojo;
import java.util.Arrays;
import java.util.Date;
public class Student {
private Integer id;
private String name;
private Integer age;
private Integer gender;
private String[] hobby;
private Integer edu;
@DateTimeFormat(pattern="yyyy-MM-dd")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone="GMT+8")
private Date intime;
public Student() {
}
public Student(Integer id, String name, Integer age, Integer gender, String[] hobby, Integer edu, Date intime) {
this.id = id;
this.name = name;
this.age = age;
this.gender = gender;
this.hobby = hobby;
this.edu = edu;
this.intime = intime;
}
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 Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Integer getGender() {
return gender;
}
public void setGender(Integer gender) {
this.gender = gender;
}
public String[] getHobby() {
return hobby;
}
public void setHobby(String[] hobby) {
this.hobby = hobby;
}
public Integer getEdu() {
return edu;
}
public void setEdu(Integer edu) {
this.edu = edu;
}
public Date getIntime() {
return intime;
}
public void setIntime(Date intime) {
this.intime = intime;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", gender=" + gender +
", hobby=" + Arrays.toString(hobby) +
", edu=" + edu +
", intime=" + intime +
'}';
}
}
3. 创建数据库 tb_student
create table tb_student(
id int primary key auto_increment,
name varchar(20),
age int,
gender int,
hobby varchar(40),
edu int,
intime datetime
)
4. 在pom.xml中添加依赖
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.23</version>
</dependency>
5. 控制层 StudentController
package cn.tedu.controller;
import cn.tedu.pojo.Student;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.sql.*;
import java.sql.Date;
import java.util.Arrays;
import java.util.Scanner;
@RestController
@RequestMapping("stu")
public class StudentController {
@RequestMapping("add")
public Student add(Student s) {
Connection c = null;
PreparedStatement ps = null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/cgb2106?characterEncoding=utf8&serverTimezone=Asia/Shanghai";
c = DriverManager.getConnection(url, "root", "123456");
String sql = "insert into tb_student values (null ,?,?,?,?,?,?)";
ps = c.prepareStatement(sql);
ps.setObject(1, s.getName());
ps.setObject(2, s.getAge());
ps.setObject(3, s.getGender());
ps.setObject(4, Arrays.toString(s.getHobby()));
ps.setObject(5, s.getEdu());
ps.setObject(6, s.getIntime());
int i = ps.executeUpdate();
System.out.println("受影响的行数"+i);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
ps.close();
c.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return s;
}
}