SpringBoot集成Thymeleaf模板引擎实现数据的增删改查
本篇是在上⼀篇“SpringBoot快速集成MyBatis+MySQL”的基础上,视图层通过集成Thymeleaf,数据持久层通过集成MyBatis从⽽完成数据的增删改查。
Thymeleaf是什么
简单说, Thymeleaf 是⼀个跟 Velocity、FreeMarker 类似的模板引擎,它可以完全替代 JSP 。
它的优点:
1. 开箱即⽤,它提供标准和spring标准两种⽅⾔,可以直接套⽤模板实现JSTL、 OGNL表达式效果,避免每天套模板、改jstl、改标签
的困扰。同时开发⼈员也可以扩展和创建⾃定义的⽅⾔;
2. Thymeleaf 提供spring标准⽅⾔和⼀个与 SpringMVC 完美集成的可选模块,可以快速的实现表单绑定、属性编辑器、国际化等功
能。
使⽤之前需在l中先引⼊依赖
 <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
还需要在springboot项⽬的application.properties做如下配置
#Thymeleaf配置
spring.thymeleaf.cache=false
ding=utf-8
de=HTML5
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
以下就是我的项⽬⽬录:
接下来就是我做的简单的增删改查:
//封装的头部header.html
<!DOCTYPE html>
<html xmlns:th=""
xmlns:layout="/thymeleaf/layout"> <head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div data-th-fragment="header">
<a href="/users">⾸页</a>
</div>
</body>
</html>
//⾸页index.html
<!DOCTYPE html>
<html xmlns:th=""
xmlns:layout="/thymeleaf/layout"> <head>
<meta charset="utf-8">
<title th:text="${userModel.title}"></title>
</head>
<body>
<div th:replace="~{fragments/header :: header}">...</div>
<div th:text="${userModel.title}"></div>
<div>
<a href="/users/form">添加⽤户</a>
</div>
<table border="1">
<thead>
<tr>
<td>ID</td>
<td>名字</td>
<td>电话</td>
<td>性别</td>
</tr>
</thead>
<tbody>
<tr th:if="${userModel.userList.size()} eq 0">
<td colspan="3">没有⽤户信息!</td>
</tr>
<tr th:each="user : ${userModel.userList}">
<td th:text="${user.userId}"></td>
<td th:text="${user.userName}"></td>
thyme<td th:text="${user.phone}"></td>
<td th:text="${user.sex}"></td>
</tr>
</tbody>
</table>
</body>
</html>
//添加⽤户add.html
<!DOCTYPE html>
<html xmlns:th=""
xmlns:layout="/thymeleaf/layout">
<head>
<meta charset="UTF-8">
<title th:text="${userModel.title}"></title>
</head>
<body>
<div th:replace="~{fragments/header :: header}">...</div>
<div th:text="${userModel.title}"></div>
<form action="/users/add" method="post" th:object="${userModel.user}"> <input type="hidden" name="userId" th:value="*{userId}">
名字:
<input type="text" name="userName" th:value="*{userName}">
电话:
<input type="text" name="phone" th:value="*{phone}">
性别:
<input type="text" name="sex" th:value="*{sex}">
<br>
<input type="submit" value="提交">
</form>
</body>
</html>
controller层
ller;
ample.springboot.pojo.UserInfo;
ample.springboot.service.ExportService;
ample.springboot.util.Log4j2Util;
import org.springframework.beans.factory.annotation.Autowired;
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.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import java.util.List;
@RestController
@RequestMapping("/users")
public class UserInfoController {
@Autowired
private ExportService userservice;
/**
*获取⽤户列表
* @author qqg
* @date
* @param  * @param model
* @return
*/
private List<UserInfo> getUserList(){
List<UserInfo> lists = UserList();
Log4j2Util.logger.info("查询到的⽤户信息:\n"+lists);
return lists;
}
@GetMapping
public ModelAndView userList(Model model){
model.addAttribute("userList",getUserList());
model.addAttribute("title","⽤户管理");
return new ModelAndView("index","userModel",model);
}
/**
*创建表单
* @author qqg
* @date
* @param  * @param model
* @return
*/
@GetMapping("/form")
public ModelAndView createForm(Model model){
model.addAttribute("user",new UserInfo());
model.addAttribute("title","添加⽤户");
return new ModelAndView("add","userModel",model);  }
/**
*功能描述添加⽤户
* @author qqg
* @date
* @param  * @param user
* @return
*/
@PostMapping("/add")
public ModelAndView addUser(UserInfo user){
int result = userservice.saveUserInfo(user);
Log4j2Util.logger.info("添加结果:\n"+result);
return new ModelAndView("redirect:/users");
}
}
服务层service
ample.springboot.service;
ample.springboot.pojo.UserInfo;
import java.util.List;
public interface ExportService {
List<UserInfo> getUserList();
Integer saveUserInfo(UserInfo user);
}
//服务实现impl
ample.springboot.service.impl;
ample.springboot.dao.UserInfoMapper;
ample.springboot.pojo.UserInfo;
ample.springboot.service.ExportService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class ExportServiceImpl implements ExportService{
@Autowired
private UserInfoMapper userinfomapper;
@Override
public List<UserInfo> getUserList() {
List<UserInfo> lists = AllUserInfo();
return lists;
}
@Override
public Integer saveUserInfo(UserInfo user) {
return userinfomapper.saveUserInfo(user);
}
}
数据持久层dao
ample.springboot.dao;
ample.springboot.pojo.UserInfo;
import java.util.List;
@Mapper
public interface UserInfoMapper {
List<UserInfo> getAllUserInfo();
Integer saveUserInfo(UserInfo user);
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。