当前位置 博文首页 > F_xiao_chou的博客:Springboot整合thyemleaf(crud ||page)

    F_xiao_chou的博客:Springboot整合thyemleaf(crud ||page)

    作者:[db:作者] 时间:2021-09-15 16:36

    使用工具:??mysql??

    ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? idea 2018

    第一步 :创建项目

    ? ? ? ? ?

    ? ? ? ??

    配置pom.xml

    <dependency>

    ????????????<groupId>org.springframework.boot</groupId>

    ????????????<artifactId>spring-boot-starter-web</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-tomcat</artifactId>

    ?????????????

    ????????</dependency>

    ????????<dependency>

    ??????????????<groupId>junit</groupId>

    ??????????????<artifactId>junit</artifactId>

    ??????????????<version>3.8.1</version>

    ??????????????<scope>test</scope>

    ????????</dependency>

    ????????<!-- servlet依赖. -->

    ????????<dependency>

    ??????????????<groupId>javax.servlet</groupId>

    ??????????????<artifactId>javax.servlet-api</artifactId>

    ???????????????

    ????????</dependency>

    ??????????????<dependency>

    ?????????????????????<groupId>javax.servlet</groupId>

    ?????????????????????<artifactId>jstl</artifactId>

    ??????????????</dependency>

    ????????<!-- tomcat的支持.-->

    ????????<dependency>

    ???????????????<groupId>org.apache.tomcat.embed</groupId>

    ???????????????<artifactId>tomcat-embed-jasper</artifactId>

    ????????????????

    ????????</dependency>????

    ????????<dependency>

    ????????????<groupId>org.springframework.boot</groupId>

    ????????????<artifactId>spring-boot-devtools</artifactId>

    ????????????<optional>true</optional>?<!-- 这个需要为 true 热部署才有效 -->

    ????????</dependency>???

    ????????<!-- mybatis -->

    ????????<dependency>

    ????????????<groupId>org.mybatis.spring.boot</groupId>

    ????????????<artifactId>mybatis-spring-boot-starter</artifactId>

    ????????????<version>1.1.1</version>

    ????????</dependency>

    ????????<!-- mysql -->

    ????????<dependency>

    ????????????<groupId>mysql</groupId>

    ????????????<artifactId>mysql-connector-java</artifactId>

    ????????????<version>5.1.21</version>

    ????????</dependency>????

    ????????<!-- pageHelper -->

    ????????<dependency>

    ????????????<groupId>com.github.pagehelper</groupId>

    ????????????<artifactId>pagehelper</artifactId>

    ????????????<version>4.1.6</version>

    ????????</dependency>

    在application.properties 中配置

    #thymeleaf 配置

    spring.thymeleaf.mode=HTML5

    spring.thymeleaf.encoding=UTF-8

    spring.thymeleaf.content-type=text/html

    #缓存设置为false, 这样修改之后马上生效,便于调试

    spring.thymeleaf.cache=false

    #上下文

    server.context-path=/thymeleaf

    #数据库

    spring.datasource.url=jdbc:mysql://127.0.0.1:3306/how2java?characterEncoding=UTF-8

    spring.datasource.username=root

    spring.datasource.password=admin

    spring.datasource.driver-class-name=com.mysql.jdbc.Driver

    8版本数据库要加时区? 这个网上搜就可以了 (一搜一大把)

    数据库语句

    CREATE TABLE users (
    ? id INT(11) NOT NULL AUTO_INCREMENT,
    ? Uname VARCHAR(30),
    ? Age INT,
    ? Nmain VARCHAR(50),
    ? PRIMARY KEY (id)
    ) DEFAULT CHARSET=UTF8;

    写完自己增加几条数据(看这个的? 大多是新手? 建议手敲一遍? ?尝试加加约束?)

    x项目结构(注意 :结构并不规范 -- 但对于初学者来说 你先写的出来这最重要 )

    pojo实体类

    加上 get set tostring 方法(这些我觉得你写这个项目? 小的你应该明白? ?)

    mapper接口

    controller层

    config

    html

    <!DOCTYPE HTML>
    <html xmlns:th="http://www.thymeleaf.org">
    <head>
        <title>hello</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    </head>
    <body>
    
    <div style="width:500px;margin:20px auto;text-align: center">
        <table align='center' border='1' cellspacing='0'>
            <tr>
                <td>序号</td>
                <td>姓名</td>
                <td>年龄</td>
                <td>描述</td>
                <td>编辑</td>
                <td>删除</td>
            </tr>
            <tr th:each="c:${page.list}">
                <td th:text="${c.id}"></td>
                <td th:text="${c.getUname()}"></td>
                <td th:text="${c.getAge()}"></td>
                <td th:text="${c.getNmain()}"></td>
                <td><a th:href="@{/listInfo(id=${c.id})}">编辑</a></td>
                <td><a th:href="@{/deleteUser(id=${c.id})}">删除</a></td>
            </tr>
        </table>
        <br/>
        <div>
            <a th:href="@{/listCategory(start=0)}">[首  页]</a>
            <a th:href="@{/listCategory(start=${page.pageNum-1})}">[上一页]</a>
            <a th:href="@{/listCategory(start=${page.pageNum+1})}">[下一页]</a>
            <a th:href="@{/listCategory(start=${page.pages})}">[末  页]</a>
        </div>
        <br/>
        <form action="addCategory" method="post">
    
            name: <input name="name"/> <br/>
            <button type="submit">提交</button>
    
        </form>
    </div>
    
    </body>
    </html>

    <!DOCTYPE HTML>
    <html xmlns:th="http://www.thymeleaf.org">
    <head>
        <title>hello</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    </head>
    <body>
    <div style="margin:0px auto; width:500px">
    
        <form action="updateUsers" method="post">
    
            name: <input name="Uname" th:value="${c.getUname()}"/> <br/>
            <input name="Age" th:value="${c.getAge()}"/> <br/>
            <input name="Nmain" th:value="${c.getNmain()}"/> <br/>
            <input name="id" type="hidden" th:value="${c.id}"/>
    
            <button type="submit">提交</button>
    
        </form>
    </div>
    </body>
    
    </html>

    http://127.0.0.1:8080/thymeleaf/

    最后 :

    ? ?所有的大项目都是从小项目起的? 诸君请勿好高骛远

    且行且珍惜?

    如果有疑问 请评论联系?

    制作不易? 点个赞把? ?谢谢了您嘞

    cs
    下一篇:没有了