springboot+mybatis+thymeleaf增删改查demo

依赖添加参考

参考文档

entity

package per.jarping.blog.entity;

import com.fasterxml.jackson.annotation.JsonFormat;

import java.util.Date;

public class BlogTag {
    private Integer tagId;

    private String tagName;

    private Byte isDeleted;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date createTime;

    public Integer getTagId() {
        return tagId;
    }

    public void setTagId(Integer tagId) {
        this.tagId = tagId;
    }

    public String getTagName() {
        return tagName;
    }

    public void setTagName(String tagName) {
        this.tagName = tagName == null ? null : tagName.trim();
    }

    public Byte getIsDeleted() {
        return isDeleted;
    }

    public void setIsDeleted(Byte isDeleted) {
        this.isDeleted = isDeleted;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(getClass().getSimpleName());
        sb.append(" [");
        sb.append("Hash = ").append(hashCode());
        sb.append(", tagId=").append(tagId);
        sb.append(", tagName=").append(tagName);
        sb.append(", isDeleted=").append(isDeleted);
        sb.append(", createTime=").append(createTime);
        sb.append("]");
        return sb.toString();
    }
}

修改mapper,service,impl

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="per.jarping.blog.dao.BlogTagMapper">
    <resultMap id="BaseResultMap" type="per.jarping.blog.entity.BlogTag">
        <id column="tag_id" jdbcType="INTEGER" property="tagId"/>
        <result column="tag_name" jdbcType="VARCHAR" property="tagName"/>
        <result column="is_deleted" jdbcType="TINYINT" property="isDeleted"/>
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
    </resultMap>
    <resultMap id="BaseCountResultMap" type="per.jarping.blog.entity.BlogTagCount">
        <id column="tag_id" jdbcType="INTEGER" property="tagId"/>
        <result column="tag_count" jdbcType="INTEGER" property="tagCount"/>
        <result column="tag_name" jdbcType="VARCHAR" property="tagName"/>
    </resultMap>
    <sql id="Base_Column_List">
    tag_id, tag_name, is_deleted, create_time
  </sql>

   

    <select id="listTags" parameterType="per.jarping.blog.entity.BlogTag" resultMap="BaseResultMap">
        select
        *
        from tb_blog_tag
        where is_deleted = 0
    </select>
</mapper>

public interface TagService {
    List<BlogTag> listTags();
}
package per.jarping.blog.service.impl;

import per.jarping.blog.dao.BlogTagMapper;
import per.jarping.blog.dao.BlogTagRelationMapper;
import per.jarping.blog.entity.BlogTag;
import per.jarping.blog.entity.BlogTagCount;
import per.jarping.blog.service.TagService;
import per.jarping.blog.util.PageQueryUtil;
import per.jarping.blog.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Service
public class TagServiceImpl implements TagService {

    @Autowired
    private BlogTagMapper blogTagMapper;
    
    @Override
    public List<BlogTag> listTags() {
        return blogTagMapper.listTags();
    }
}

编写controller

@Controller
@RequestMapping("/admin")
public class BlogController {

    @Resource
    private TagService tagService;

    @GetMapping("/blogs/edit")
    public String edit(HttpServletRequest request) {
        request.setAttribute("tags",tagService.listTags());
        return "admin/edit";
    }
}

修改edit.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<header th:replace="admin/header::header-fragment"></header>
<body class="hold-transition sidebar-mini">
<link th:href="@{/admin/plugins/editormd/css/editormd.css}" rel="stylesheet"/>
<link th:href="@{/admin/plugins/tagsinput/jquery.tagsinput.css}" rel="stylesheet"/>
<link th:href="@{/admin/plugins/select2/select2.css}" rel="stylesheet"/>
<div class="wrapper">
    <!-- 引入页面头header-fragment -->
    <div th:replace="admin/header::header-nav"></div>
    <!-- 引入工具栏sidebar-fragment -->
    <div th:replace="admin/sidebar::sidebar-fragment(${path})"></div>
    <!-- Content Wrapper. Contains page content -->
    <div class="content-wrapper">
        <!-- Content Header (Page header) -->
        <div class="content-header">
            <div class="container-fluid">
            </div><!-- /.container-fluid -->
        </div>
        <!-- Main content -->
        <div class="content">
            <div class="container-fluid">
                <div class="card card-primary card-outline">
                    <div class="card-header">
                        <h3 class="card-title">发布文章</h3>
                    </div>
                    <div class="card-body">
                        <!-- 几个基础的输入框,名称、分类等输入框 -->
                        <form id="blogForm" onsubmit="return false;">
                            <div class="form-group" style="display:flex;">
                                <input type="hidden" id="blogId" name="blogId" th:value="${blog!=null and blog.blogId!=null }?${blog.blogId}: 0">
                                <input type="text" class="form-control col-sm-6" id="blogName" name="blogName"
                                       placeholder="*请输入文章标题(必填)"
                                       th:value="${blog!=null and blog.blogTitle!=null }?${blog.blogTitle}: ''"
                                       required="true">
                                &nbsp;&nbsp;
                                
                            

-----------------------------------------修改------------

<select class="form-control select2" style="width: 100%;" id="blogTags"
                                    data-placeholder="请输入文章标签">
                                <th:block th:if="${null == tags}">
                                    <option value="0" selected="selected">默认</option>
                                </th:block>
                                <th:block th:unless="${null == tags}">
                                    <th:block th:each="tag : ${tags}">
                                        <option th:value="${tag.tagId}" th:text="${tag.tagName}"
                                                th:selected="${null !=blog and null !=blog.blogTags.tagId and blog.blogTags.tagId==tag.tagId} ?true:false">
                                            >
                                        </option>
                                    </th:block>
                                </th:block>
                            </select>

-----------------------------------------修改------------


                            </div>
                            <div class="form-group" style="display:flex;">
                                <input type="text" class="form-control col-sm-6" id="blogSubUrl"
                                       name="blogSubUrl"
                                       th:value="${blog!=null and blog.blogSubUrl!=null }?${blog.blogSubUrl}: ''"
                                       placeholder="请输入自定义路径,如:springboot-mybatis,默认为id"> &nbsp;&nbsp;
                                <select class="form-control select2" style="width: 100%;" id="blogCategoryId"
                                        data-placeholder="请选择分类...">
                                    <th:block th:if="${null == categories}">
                                        <option value="0" selected="selected">默认分类</option>
                                    </th:block>
                                    <th:block th:unless="${null == categories}">
                                        <th:block th:each="c : ${categories}">
                                            <option th:value="${c.categoryId}" th:text="${c.categoryName}"
                                                    th:selected="${null !=blog and null !=blog.blogCategoryId and blog.blogCategoryId==c.categoryId} ?true:false">
                                                >
                                            </option>
                                        </th:block>
                                    </th:block>
                                </select>
                            </div>
                            <div class="form-group" id="blog-editormd">
                                <textarea style="display:none;"
                                          th:utext="${blog!=null and blog.blogContent !=null}?${blog.blogContent}: ''"></textarea>
                            </div>
                            <div class="form-group">
                                <!-- 按钮 -->
                                &nbsp;<button class="btn btn-info float-right" style="margin-left: 5px;"
                                              id="confirmButton">保存文章
                            </button>&nbsp;
                                &nbsp;<button class="btn btn-secondary float-right" style="margin-left: 5px;"
                                              id="cancelButton">返回文章列表
                            </button>&nbsp;
                            </div>
                        </form>
                    </div>

                </div>
            </div><!-- /.container-fluid -->
        </div>
        <div class="content">
            <!-- 模态框(Modal) -->
            <div class="modal fade" id="articleModal" tabindex="-1" role="dialog" aria-labelledby="articleModalLabel">
                <div class="modal-dialog" role="document">
                    <div class="modal-content">
                        <div class="modal-header">
                            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                                    aria-hidden="true">&times;</span></button>
                            <h6 class="modal-title" id="articleModalLabel">信息完善</h6>
                        </div>
                        <div class="modal-body">
                            <form onsubmit="return false;">
                                <div class="form-group">
                                    <div class="col-sm-4">
                                        <th:block th:if="${null == blog}">
                                            <img id="blogCoverImage" src="/admin/dist/img/img-upload.png"
                                                 style="height: 64px;width: 64px;">
                                        </th:block>
                                        <th:block th:unless="${null == blog}">
                                            <img id="blogCoverImage" th:src="${blog.blogCoverImage}"
                                                 style="width:160px ;height: 120px;display:block;">
                                        </th:block>
                                    </div>
                                </div>
                                <br>
                                <div class="form-group">
                                    <div class="col-sm-4">
                                        <button class="btn btn-info" style="margin-bottom: 5px;" id="uploadCoverImage">
                                            <i class="fa fa-picture-o"></i>&nbsp;上传封面
                                        </button>
                                        <button class="btn btn-secondary" style="margin-bottom: 5px;"
                                                id="randomCoverImage"><i
                                                class="fa fa-random"></i>&nbsp;随机封面
                                        </button>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="control-label">文章状态:&nbsp;</label>
                                    <input name="blogStatus" type="radio" id="publish"
                                           checked=true
                                           th:checked="${null==blog||(null !=blog and null !=blog.blogStatus and blog.blogStatus==1)} ?true:false"
                                           value="1"/>&nbsp;发布&nbsp;
                                    <input name="blogStatus" type="radio" id="draft" value="0"
                                           th:checked="${null !=blog and null !=blog.blogStatus and blog.blogStatus==0} ?true:false"/>&nbsp;草稿&nbsp;
                                </div>
                                <div class="form-group">
                                    <label class="control-label">是否允许评论:&nbsp;</label>
                                    <input name="enableComment" type="radio" id="enableCommentTrue" checked=true
                                           th:checked="${null==blog||(null !=blog and null !=blog.enableComment and blog.enableComment==0)} ?true:false"
                                           value="0"/>&nbsp;是&nbsp;
                                    <input name="enableComment" type="radio" id="enableCommentFalse" value="1"
                                           th:checked="${null !=blog and null !=blog.enableComment and blog.enableComment==1} ?true:false"/>&nbsp;否&nbsp;
                                </div>
                            </form>
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                            <button type="button" class="btn btn-primary" id="saveButton">确认</button>
                        </div>
                    </div>
                </div>
            </div>
            <!-- /.modal -->
        </div>
    </div>
    <!-- /.content-wrapper -->
    <!-- 引入页脚footer-fragment -->
    <div th:replace="admin/footer::footer-fragment"></div>
</div>
<!-- jQuery -->
<script th:src="@{/admin/plugins/jquery/jquery.min.js}"></script>
<!-- jQuery UI 1.11.4 -->
<script th:src="@{/admin/plugins/jQueryUI/jquery-ui.min.js}"></script>
<!-- Bootstrap 4 -->
<script th:src="@{/admin/plugins/bootstrap/js/bootstrap.bundle.min.js}"></script>
<!-- AdminLTE App -->
<script th:src="@{/admin/dist/js/adminlte.min.js}"></script>
<!-- editor.md -->
<script th:src="@{/admin/plugins/editormd/editormd.min.js}"></script>
<!-- tagsinput -->
<script th:src="@{/admin/plugins/tagsinput/jquery.tagsinput.min.js}"></script>
<!-- Select2 -->
<script th:src="@{/admin/plugins/select2/select2.full.min.js}"></script>
<!-- sweetalert -->
<script th:src="@{/admin/plugins/sweetalert/sweetalert.min.js}"></script>
<!-- ajaxupload -->
<script th:src="@{/admin/plugins/ajaxupload/ajaxupload.js}"></script>
<script th:src="@{/admin/dist/js/public.js}"></script>
<script th:src="@{/admin/dist/js/edit.js}"></script>
</body>
</html>

效果
在这里插入图片描述

这是入门级别的查询列表,thymeleaf交互


广告。。。

博主个人博客http://blog.jathamcloud.top

博主公众号
求关注
在这里插入图片描述


版权声明:本文为qq_42784606原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。