之前百度了很多资料发现有关spring boot结合mybatis结合前端页面对数据库进行增删改查比较少,大部分都是直接给接口,然后postman,所以今天来csdn发布一下史上最丑的前端页面对数据库进行增删改查
数据库名和表名都是student,里面有四个字段,id,name,age,sex,都是vchar类型的
添加相关的jar包,在pom.xml添加:
mysql
mysql-connector-java
5.1.21
增加pom对jsp文件的支持:
javax.servlet
javax.servlet-api
provided
javax.servlet
jstl
org.apache.tomcat.embed
tomcat-embed-jasper
provided
增加pom文件的jdbc、mybatis的支持
org.springframework.boot
spring-boot-starter-jdbc
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.3.0
javax.servlet
jstl
最终的pom.xml文件是这样的:
4.0.0
com.example
demo-2
0.0.1-SNAPSHOT
jar
demo-3
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
2.0.5.RELEASE
UTF-8
UTF-8
1.8
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-devtools
true
javax.servlet
javax.servlet-api
provided
javax.servlet
jstl
org.apache.tomcat.embed
tomcat-embed-jasper
provided
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.1.1
mysql
mysql-connector-java
5.1.21
org.springframework.boot
spring-boot-maven-plugin
package cn.suliaosxd;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("cn.suliaosxd.springboot.mapper")
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
StudentController.java
package cn.suliaosxd.springboot.controller;
import java.util.List;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
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.RequestMethod;
import cn.suliaosxd.springboot.mapper.StudentMapper;
import cn.suliaosxd.springboot.pojo.Student;
@Controller
public class StudentController {
@Autowired
StudentMapper studentMapper;
@RequestMapping("/listStudent")
public String listStudent(Model model) {
List students = studentMapper.findAll();
model.addAttribute("students", students);
return "listStudent";
}
@RequestMapping( value = "/findstudentbyid", method = RequestMethod.GET)
public String findStudent(HttpServletRequest request) {
Student student = studentMapper.findStudentByid(request.getParameter("id"));
request.setAttribute("student", student);
return "findstudentbyid";
}
@RequestMapping(value = "/addstudent",method = RequestMethod.POST)
public void addstudent(Student student)
{
studentMapper.addstudent(student);
}
@RequestMapping(value = "/deletestudent")
public void deletestudent(String id) {
studentMapper.deleteStudent(id);
}
@RequestMapping(value = "/updatastudent",method = RequestMethod.POST)
public void updatestudent(String id,String name,String age,String sex) {
Student student = new Student();
student.setId(id);
student.setName(name);
student.setAge(age);
student.setSex(sex);
studentMapper.update(student);
}
}
StudentMapper.java
package cn.suliaosxd.springboot.mapper;
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.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;
import cn.suliaosxd.springboot.pojo.Student;
@Repository
public interface StudentMapper {
@Select("SELECT * FROM student")
public List findAll();
@Select("SELECT * FROM student WHERE ID = #{id}")
public Student findStudentByid(@Param("id") String id);
@Insert("INSERT INTO student(id,name,age,sex) values(#{id},#{name},#{age},#{sex})")
public void addstudent(Student student);
@Delete("delete from student where id=#{id}")
public int deleteStudent(@Param("id")String id);
@Update("update student set name=#{name},age=#{age},sex=#{sex} where id=#{id}")
public int update(Student studet);
}
Student.java
package cn.suliaosxd.springboot.pojo;
import java.util.Date;
public class Student {
private String id;
private Integer student_id;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public Integer getStudent_id() {
return student_id;
}
public void setStudent_id(Integer student_id) {
this.student_id = student_id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
private String name;
private String age;
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
private String sex;
private Date birthday;
}
application.yml
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
Insert title here
添加成功
五秒后将自动跳转到初始页面
deletestudent.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
Insert title here
删除成功
五秒后将自动跳转到初始页面
findstudentbyid.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
Insert title here
获取成功:${student.name} ${student.sex} ${student.age} ${student.sex}
五秒后将自动跳转到初始页面
listStudent.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
id
name
age
sex
${s.id}
${s.name}
${s.age}
${s.sex}
添加:
查询:
updatastudent.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
Insert title here
修改成功
五秒后将自动跳转到初始页面
接下来让各位看看本人做的史上最丑的前端数据管理页面,因为我用的是谷歌浏览器,所有按钮那里有写着“提交”两字。
目前不知道为什么插入第7条数据的时候会显示在第6条上面,请知道的大佬指教,谢谢