spring boot mybatis js对mysql进行增删改查

spring boot mybatis jsp对mysql进行增删改查

之前百度了很多资料发现有关spring boot结合mybatis结合前端页面对数据库进行增删改查比较少,大部分都是直接给接口,然后postman,所以今天来****发布一下史上最丑的前端页面对数据库进行增删改查

添加相关的jar包,在pom.xml添加:

<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>5.1.21</version>
		</dependency>

增加pom对jsp文件的支持:

<!-- servlet依赖. -->
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
</dependency>

<!-- tomcat的支持.-->
<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
    <scope>provided</scope>
</dependency>

增加pom文件的jdbc、mybatis的支持

<!-- jdbc -->     
  <dependency>            
  <groupId>org.springframework.boot</groupId>          
   <artifactId>spring-boot-starter-jdbc</artifactId>      
  </dependency>      
       <!-- mybatis -->   
            <dependency>         
               <groupId>org.mybatis.spring.boot</groupId>          
                <artifactId>mybatis-spring-boot-starter</artifactId>     
                       <version>1.3.0</version>   
              </dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>jstl</artifactId>
		</dependency>

spring boot mybatis js对mysql进行增删改查
App.java

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<Student> 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<Student> 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

spring boot mybatis js对mysql进行增删改查
spring boot mybatis js对mysql进行增删改查
addstudent.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="refresh" content="5;url=listStudent"> 
<title>Insert title here</title>
</head>
<body>
	添加成功
	</br>
	<hr>
	<h3>五秒后将自动跳转到初始页面</h3>
</body>
</html>

deletestudent.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="refresh" content="5;url=listStudent"> 
<title>Insert title here</title>
</head>
<body>

	删除成功</br>
	<hr>
	<h3>五秒后将自动跳转到初始页面</h3>
</body>
</html>

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"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="refresh" content="5;url=listStudent"> 
<title>Insert title here</title>
</head>
<body>
	
	获取成功:${student.name} ${student.sex}  ${student.age} ${student.sex}
	</br>
	<hr>
	<h3>五秒后将自动跳转到初始页面</h3>

</body>
</html>

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"%>

<table align='center' border='1' cellspacing='0'>
    <tr>
        <td>id</td>
        <td>name</td>
        <td>age</td>
        <td>sex</td>
    </tr>
    <c:forEach items="${students}" var="s" varStatus="st">
        <tr>
            <td>${s.id}</td>
            <td>${s.name}</td>
            <td>${s.age}</td>
            <td>${s.sex}</td>
        </tr>
    </c:forEach>
</table>
<hr>
	添加:
	<form action="addstudent" method="post">
	id: <input type="text"  name="id" ></input></br>
	name:<input type="text" name="name" ></input></br>
	age:<input type="text" name="age" ></input></br>
	sex:<input type="text" name="sex"></input></br>
		<input type="submit"></input>
	</form>
	<hr>
<form action="deletestudent" method="post">
	所要删除的id:<input type="text" name = "id"></input>
				<input type="submit"></input>
	</form>
	<hr>
	<form action="updatastudent" method="post">
	所要更改的id:<input type="text" name = "id"></input></br>
	name:<input type="text" name = "name"></input></br>
	age:<input type="text" name = "age"></input></br>
	sex:<input type="text" name = "sex"></input></br>
				<input type="submit"></input>
	</form>
	<hr>
	查询:
	<form action="findstudentbyid" method="get">
		请输入id:<input type="text" id="id" name="id"></input></br> <input type="submit"></input></br>
	</form>

updatastudent.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="refresh" content="5;url=listStudent"> 
<title>Insert title here</title>
</head>
<body>

	修改成功</br>
	<hr>
	<h3>五秒后将自动跳转到初始页面</h3>
</body>
</html>

接下来让各位看看本人做的史上最丑的前端数据管理页面
spring boot mybatis js对mysql进行增删改查
目前不知道为什么插入第7条数据的时候会显示在第6条上面,请知道的大佬指教,谢谢
spring boot mybatis js对mysql进行增删改查