spirngboot+thymeleaf实现增删改查的功能
maven依赖
<properties>
<java.version>1.8</java.version>
<mybaitsplus.version>3.4.3</mybaitsplus.version>
<lombok.version>1.16.18</lombok.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- MyBatis-plus依赖 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>${mybaitsplus.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!-- thymeleaf依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
<version>2.3.12.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
启动类
package com.yunhe;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.yunhe.mapper")
public class ThymeleafApplication {
public static void main(String[] args) {
SpringApplication.run(ThymeleafApplication.class, args);
}
}
StudentMapper
package com.yunhe.mapper;
import com.yunhe.entity.Student;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.springframework.stereotype.Repository;
@Repository
public interface StudentMapper extends BaseMapper<Student> {
}
application.yml(配置文件)
server:
port: 80
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/thymeleaf?useUnicode=true&serverTimezone=Asia/Shanghai
password: root
username: root
实体类
package com.yunhe.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import java.io.Serializable;
@Data
@TableName("student")
public class Student implements Serializable {
private static final long serialVersionUID = 1L;
@TableId(value = "sid",type = IdType.AUTO)
private Integer sid;
private String sname;
private Integer sage;
private String saddress;
private String spwd;
}
service层 接口
package com.yunhe.service;
import com.yunhe.entity.Student;
import com.baomidou.mybatisplus.extension.service.IService;
import java.util.List;
public interface IStudentService extends IService<Student> {
List<Student> queryAll(Student student);
boolean add(Student student);
boolean delete(Integer sid);
Student querySid(Integer sid);
boolean update(Student student);
}
接口的实现
package com.yunhe.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yunhe.entity.Student;
import com.yunhe.mapper.StudentMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunhe.service.IStudentService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.List;
@Service
@Transactional
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements IStudentService {
@Resource
private StudentMapper studentMapper;
@Override
public List<Student> queryAll(Student student) {
QueryWrapper<Student> studentQueryWrapper = new QueryWrapper<>(student);
return studentMapper.selectList(studentQueryWrapper);
}
@Override
public boolean add(Student student) {
int insert = studentMapper.insert(student);
if (insert > 0){
return true;
}else {
return false;
}
}
@Override
public boolean delete(Integer sid) {
int i = studentMapper.deleteById(sid);
if (i > 0){
return true;
}else {
return false;
}
}
@Override
public Student querySid(Integer sid) {
Student student = studentMapper.selectById(sid);
return student;
}
@Override
public boolean update(Student student) {
int i = studentMapper.updateById(student);
if (i > 0){
return true;
}else {
return false;
}
}
}
controller层
package com.yunhe.controller;
import com.yunhe.entity.Student;
import com.yunhe.service.IStudentService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
@Controller
public class StudentController {
@Resource
private IStudentService iStudentService;
@RequestMapping("/query")
public String queryAll(Student student,Model model){
List<Student> students = iStudentService.queryAll(student);
model.addAttribute("students",students);
return "index";
}
@GetMapping("/delete")
public String delete(Integer sid, Model model ){
iStudentService.delete(sid);
return "redirect:/query";
}
@GetMapping("/insert")
public String insert(Student student, Model model){
model.addAttribute("student",student);
return "/add";
}
@PostMapping("/addPage")
public String add(Student student){
iStudentService.add(student);
return "redirect:/query";
}
@GetMapping("/querySid")
public String querySid(Integer sid, Model model){
Student student = iStudentService.querySid(sid);
model.addAttribute("student",student);
return "/edit";
}
@PostMapping("update")
public String update(Student student){
iStudentService.update(student);
return "redirect:/query";
}
}
页面
index(查询页面)
<!DOCTYPE HTML>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>学生信息</title>
</head>
<body>
<table border="1px">
<tr>
<th>学号</th>
<th>姓名</th>
<th>年龄</th>
<th>地址</th>
<th>密码</th>
<th>操作</th>
</tr>
<tr th:each="student:${students}">
<td th:text="${student.getSid()}"></td>
<td th:text="${student.getSname()}"></td>
<td th:text="${student.getSage()}"></td>
<td th:text="${student.getSaddress()}"></td>
<td th:text="${student.getSpwd()}"></td>
<td>
<a th:href="@{querySid(sid=${student.getSid()})}">更新</a>
<a th:href="@{delete(sid=${student.getSid()})}">删除</a>
</td>
</tr>
<a th:href="@{/insert}">新增</a>
</table>
</body>
</html>
add(添加页面)
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>新增</title>
</head>
<body>
<form action="/addPage" method="post" th:object="${student}">
姓名:<input type="text" name="sname" th:value="${student.getSname()}">
年龄:<input type="text" name="sage" th:value="${student.getSage()}">
地址:<input type="text" name="saddress" th:value="${student.getSaddress()}">
密码:<input type="text" name="spwd" th:value="${student.getSpwd()}">
<input type="submit" value="添加">
</form>
</body>
</html>
edit (更新界面)
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>编辑</title>
</head>
<body>
<form action="/update" method="post" th:object="${student}">
学号:<input type="text" name="sid" th:value="${student.getSid()}" readonly>
姓名:<input type="text" name="sname" th:value="${student.getSname()}">
年龄:<input type="text" name="sage" th:value="${student.getSage()}">
地址:<input type="text" name="saddress" th:value="${student.getSaddress()}">
密码:<input type="text" name="spwd" th:value="${student.getSpwd()}">
<input type="submit" value="更新">
</form>
</body>
</html>