SpringBoot+MyBatisPlus+thymeleaf增删改查

SpringBoot+MyBatisPlus+thymeleaf增删改查

一、基本介绍

建议在写之前会熟练使用vue脚手架,以及掌握vue相关知识
本文章会教你构建一个springboot+mybatisplus+thymeleaf+mysql
的增删改查

SpringBoot+MyBatisPlus+thymeleaf增删改查_第1张图片

二、数据层

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 编辑好相关信息后下一步
选择这些包然后完成

SpringBoot+MyBatisPlus+thymeleaf增删改查_第2张图片导入druid

  
            com.alibaba
            druid-spring-boot-starter
            1.1.17
  

4. 页面

index.html





    
    Title
    


文档分类
新增电子文档

电子文档列表

文档编号 文档名称 文档摘要 上传人 上传时间 操作
${ee.id} ${ee.title} ${ee.summary} ${ee.uploadUser} 修改 删除
当前第[[${pageInfo.current}]]页,总页数:[[${pageInfo.pages}]], 总记录数:[[${pageInfo.total}]]

add.html 新增页面




    Title
    


新增电子文档

文档名称
文档摘要
上传人
上传时间
返回

update.html 修改页面




    Title
    


更新文档

文档名称
文档摘要
上传人
上传时间
返回

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
SpringBoot+MyBatisPlus+thymeleaf增删改查_第3张图片
EdocCategory

SpringBoot+MyBatisPlus+thymeleaf增删改查_第4张图片

7.数据访问层

由于是基于mybatisplus所以代码非常简单
EdocEntryMapper

SpringBoot+MyBatisPlus+thymeleaf增删改查_第5张图片EdocCategoryMapper
SpringBoot+MyBatisPlus+thymeleaf增删改查_第6张图片

8.服务层

①.服务接口

EdocEntryService
SpringBoot+MyBatisPlus+thymeleaf增删改查_第7张图片
EdocCategoryService
SpringBoot+MyBatisPlus+thymeleaf增删改查_第8张图片

②. 实现类

EdocEntryServiceImpl
SpringBoot+MyBatisPlus+thymeleaf增删改查_第9张图片
EdocCategoryServiceImpl
SpringBoot+MyBatisPlus+thymeleaf增删改查_第10张图片

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 ecList = ecService.list();
        Page pageInfo = new Page<>(page, pageSize);
        LambdaQueryWrapper 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项目就行啦

你可能感兴趣的:(mybatis,vue.js,mysql)