SpringBoot+MyBatisPlus+thymeleaf增删改查
文章目录
- SpringBoot+MyBatisPlus+thymeleaf增删改查
- 前言
- 🎁作者简介:在校学生一枚,Java领域新星创作者,Java正在学习中,期待和大家一起学习一起进步~ 📕自我提醒:多学多练多思考,编程能力才能节节高!
- 一、基本介绍
- 二、数据层
- 1.导入SQL
- 三、构建
- 4. 页面
- index.html
- add.html 新增页面
- update.html 修改页面
- 5.SpringBoot配置文件
- 6.实体类
- 7.数据访问层
- 8.服务层
- ①.服务接口
- ②. 实现类
- 9.控制层
前言
🎁作者简介:在校学生一枚,Java领域新星创作者,Java正在学习中,期待和大家一起学习一起进步~
📕自我提醒:多学多练多思考,编程能力才能节节高!
一、基本介绍
建议在写之前会熟练使用vue脚手架,以及掌握vue相关知识
本文章会教你构建一个springboot+mybatisplus+thymeleaf+mysql
的增删改查。
二、数据层
1.导入SQL
数据库名称 edocmanagement
/*
Navicat Premium Data Transfer
Source Server : localhost_3306
Source Server Type : MySQL
Source Server Version : 80027
Source Host : localhost:3306
Source Schema : edocmanagement
Target Server Type : MySQL
Target Server Version : 80027
File Encoding : 65001
Date: 21/11/2022 10:52:04
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for edoc_category
-- ----------------------------
DROP TABLE IF EXISTS `edoc_category`;
CREATE TABLE `edoc_category` (
`id` int(0) NOT NULL AUTO_INCREMENT,
`name` varchar(25) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 4 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of edoc_category
-- ----------------------------
INSERT INTO `edoc_category` VALUES (1, '全部');
INSERT INTO `edoc_category` VALUES (2, 'IT计算机');
INSERT INTO `edoc_category` VALUES (3, '聊天');
INSERT INTO `edoc_category` VALUES (4, '科幻');
-- ----------------------------
-- Table structure for edoc_entry
-- ----------------------------
DROP TABLE IF EXISTS `edoc_entry`;
CREATE TABLE `edoc_entry` (
`id` int(0) NOT NULL AUTO_INCREMENT,
`categoryId` int(0) NOT NULL,
`title` varchar(25) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`summary` varchar(200) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`uploadUser` varchar(25) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`createDate` date NOT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 707067329 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of edoc_entry
-- ----------------------------
INSERT INTO `edoc_entry` VALUES (2, 2, '王', '321', '2323', '2022-10-11');
INSERT INTO `edoc_entry` VALUES (3, 2, 'java学习', 'java学习精品', '小王', '2022-10-03');
INSERT INTO `edoc_entry` VALUES (7, 2, 'ppt学习', 'ppt精品', '小兰', '2022-10-04');
INSERT INTO `edoc_entry` VALUES (8, 3, '撩妹100条', '撩妹100条', '李小明', '2022-10-04');
INSERT INTO `edoc_entry` VALUES (9, 3, '网络用语100', '网络用语100', '小王', '2022-10-03');
INSERT INTO `edoc_entry` VALUES (10, 3, '美国大片学口语', '美国大片学口语', '小兰', '2022-10-04');
INSERT INTO `edoc_entry` VALUES (11, 4, '三体', '科幻大作', '李小明', '2022-10-04');
INSERT INTO `edoc_entry` VALUES (12, 4, '球形闪电', '牛逼', '小王', '2022-10-03');
INSERT INTO `edoc_entry` VALUES (13, 4, '星际穿越', '最好看的科幻片', '小兰', '2022-10-04');
SET FOREIGN_KEY_CHECKS = 1;
三、构建
打开idea 选择新建 -》新建项目-》Spring Initializr 编辑好相关信息后下一步
选择这些包然后完成
导入druid
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.17</version>
</dependency>
4. 页面
index.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
ul{
display: inline-block;
padding-left: 0;
margin: 20px 0;
border-radius: 4px;
}
ul li {
display: inline;
}
ul li:first-child>a{
margin-left: 0;
border-top-left-radius: 4px;
border-bottom-left-radius: 4px;
}
ul a{
position: relative;
float: left;
padding: 6px 12px;
margin-left: -1px;
line-height: 1.42857143;
color: #337ab7;
text-decoration: none;
background-color: #fff;
border: 1px solid #ddd;
}
* {
margin: 0 auto;
text-align: center;
}
table{
/*width: 100%;*/
/*max-width: 100%;*/
margin-bottom: 20px;
background-color: transparent;
border-collapse: collapse;
border-spacing: 0;
display: table;
box-sizing: border-box;
text-indent: initial;
border-color: grey;
}
</style>
</head>
<body>
<div style="margin: 0 auto;">
<strong>文档分类</strong>
<select id="se" name="ecId" onchange="c(this.value)">
<div th:each="ec:${ecList}">
<option th:value="${ec.id}" th:text="${ec.name}" th:attr="select=${ec.id==ecId}"></option>
</div>
</select> <br>
<script>
function c(id) {
window.location = "http://localhost:8080/index?id=" + id;
}
</script>
</div>
<a href="/toAdd">新增电子文档</a>
<table border="1px" >
<tr>
<td colspan="6"><h1>电子文档列表</h1></td>
</tr>
<tr>
<td>文档编号</td>
<td>文档名称</td>
<td>文档摘要</td>
<td>上传人</td>
<td>上传时间</td>
<td>操作</td>
</tr>
<tr th:each="ee,index:${pageInfo.records}">
<td th:text="${index.count}">${ee.id}</td>
<td th:text="${ee.title}">${ee.title}</td>
<td th:text="${ee.summary}">${ee.summary}</td>
<td th:text="${ee.uploaduser}">${ee.uploadUser}</td>
<!-- <td th:text="${#dates.format(ee,'yyyy-MM-dd HH:mm:ss')}">${ee.createDate}</td>-->
<td th:text="${ee.createdate}"></td>
<td><a th:href="@{/toUpdate(id=${ee.id})}">修改</a>
<a th:href="@{/delete(id=${ee.id})}">删除</a></td>
</tr>
</table>
<!-- 显示分页信息 -->
<!-- 分页文字信息-->
<div class="col-md-6 col-md-offset-3 text-center" style="text-align: center">
当前第[[${pageInfo.current}]]页,总页数:[[${pageInfo.pages}]],
总记录数:[[${pageInfo.total}]]
</div>
<div class="row">
<!--分页条信息-->
<div class="col-md-6 col-md-offset-3 text-center">
<nav aria-label="Page navigation">
<ul class="pagination">
<li><a th:href="@{/?page=1}">首页</a></li>
<th:block th:if="${pageInfo.current>1}">
<li>
<a th:href="@{/(page= ${pageInfo.current - 1}) }" aria-label="Previous">
<span aria-hidden="true">«</span>
</a>
</li>
</th:block>
<li th:each="info:${#numbers.sequence(1,pageInfo.pages)}">
<a th:if="${pageInfo.current == info}" th:href="@{/(page=${info})}" th:text="${info}"></a>
<a th:if="${pageInfo.current != info}" th:href="@{/(page=${info})}" th:text="${info}"></a>
</li>
<th:block th:if="${pageInfo.current<pageInfo.pages}">
<li>
<a th:href="@{/(page=${pageInfo.current+1})}" aria-label="Next">
<span aria-hidden="true">»</span>
</a>
</li>
</th:block>
<li><a th:href="@{/(page=${pageInfo.pages})}">末页</a></li>
</ul>
</nav>
</div>
</div>
</body>
</html>
add.html 新增页面
<!DOCTYPE html>
<html>
<head>
<title>Title</title>
<style>
* {
margin: 0 auto;
text-align: center;
}
</style>
</head>
<body>
<form action="/add" method="get">
<table>
<tr>
<td colspan="2"><h1>新增电子文档</h1></td>
</tr>
<tr>
<td>文档名称</td>
<td><input type="text" name="title"></td>
</tr>
<tr>
<td>文档摘要</td>
<td><textarea name="summary"></textarea></td>
</tr>
<tr>
<td>上传人</td>
<td><input type="text" name="uploaduser"></td>
</tr>
<tr>
<td>上传时间</td>
<td><input type="datetime-local" name="createdate"></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="提交">
<a href="javascript:history(-1);">返回</a>
</td>
</tr>
</table>
</form>
</body>
</html>
update.html 修改页面
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<title>Title</title>
<style>
* {
margin: 0 auto;
text-align: center;
}
</style>
</head>
<body>
<h1>更新文档</h1>
<form th:action="@{/update}" method="get">
<table>
<input type="hidden" name="id" th:value="${ee.id}">
<input type="hidden" name="categoryid" th:value="${ee.categoryid}">
<tr>
<td>文档名称</td>
<td><input type="text" name="title" th:value="${ee.title}"></td>
</tr>
<tr>
<td>文档摘要</td>
<td><textarea name="summary" th:text="${ee.summary}" >${ee.summary}</textarea></td>
</tr>
<tr>
<td>上传人</td>
<td><input type="text" name="uploaduser" th:value="${ee.uploaduser}"></td>
</tr>
<tr>
<td>上传时间</td>
<td><input type="datetime-local" name="createdate" ></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="提交">
<a href="javascript:history(-1);">返回</a>
</td>
</tr>
</table>
</form>
</body>
</html>
5.SpringBoot配置文件
application.yml
spring:
thymeleaf:
cache: true
check-template: true
check-template-location: true
enabled: true
encoding: UTF-8
mode: HTML5
prefix: classpath:/templates/
suffix: .html
servlet:
content-type: text/html
application:
name: crud_boot_th
servlet:
multipart:
max-file-size: 10MB
max-request-size: 100MB
datasource:
druid:
url: jdbc:mysql://localhost:3306/edocmanagement?serverTimezone=UTC
username: root
password: password
driver-class-name: com.mysql.cj.jdbc.Driver
filters:
stat,wall
stat-view-servlet:
enabled: true
login-username: admin
login-password: 123123
reset-enable: false
web-stat-filter:
enabled: true
url-pattern: /*
exclusions: '*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*'
mvc:
hiddenmethod:
filter:
enabled: true
server:
port: 8080
mybatis-plus:
type-aliases-package: com.boot.pojo
configuration:
map-underscore-to-camel-case: true
logging:
level:
com.boot: debug
pattern:
dateformat: MM-dd HH:mm:ss:SSS
6.实体类
EdocEntry
EdocCategory
7.数据访问层
由于是基于mybatisplus所以代码非常简单
EdocEntryMapper
EdocCategoryMapper
8.服务层
①.服务接口
EdocEntryService
EdocCategoryService
②. 实现类
EdocEntryServiceImpl
EdocCategoryServiceImpl
9.控制层
IndexController
package com.boot.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.boot.pojo.EdocCategory;
import com.boot.pojo.EdocEntry;
import com.boot.service.EdocCategoryService;
import com.boot.service.EdocEntryService;
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.RequestParam;
import java.util.List;
import java.util.UUID;
@Controller
public class IndexController {
@Autowired
private EdocCategoryService ecService;
@Autowired
private EdocEntryService eeService;
@RequestMapping({"/", "/index"})
public String toIndex(@RequestParam(value = "id", defaultValue = "1") Integer id,
@RequestParam(value = "page", defaultValue = "1") Integer page,
@RequestParam(value = "pageSize", defaultValue = "3") Integer pageSize,
Model model) {
System.out.println(id + "=======================");
List<EdocCategory> ecList = ecService.list();
Page<EdocEntry> pageInfo = new Page<>(page, pageSize);
LambdaQueryWrapper<EdocEntry> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(id != 1, EdocEntry::getCategoryid, id);
eeService.page(pageInfo, wrapper);
model.addAttribute("ecList", ecList);
model.addAttribute("pageInfo", pageInfo);
model.addAttribute("ecId", id);
return "/html/index";
}
@RequestMapping("/add")
public String add(EdocEntry edocEntry) {
int id = UUID.randomUUID().toString().replaceAll("-", "").hashCode();
id = id < 0 ? -id : id;
edocEntry.setId(id);
edocEntry.setCategoryid(1);
System.out.println("add=============" + edocEntry);
eeService.save(edocEntry);
return "redirect:/";
}
@RequestMapping("/toAdd")
public String toAdd() {
return "/html/add";
}
@RequestMapping("/toUpdate")
public String toUpdate(Integer id, Model model) {
EdocEntry ee = eeService.getById(id);
model.addAttribute("ee", ee);
return "/html/update";
}
@RequestMapping("/update")
public String update(EdocEntry edocEntry) {
System.out.println("update===============" + edocEntry);
eeService.updateById(edocEntry);
return "redirect:/";
}
@RequestMapping("/delete")
public String delete(Integer id) {
eeService.removeById(id);
return "redirect:/";
}
}
最后就是运行springboot项目就行啦