Springboot整合thyemleaf(crud ||page)

使用工具:  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/

最后 :

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

且行且珍惜 

如果有疑问 请评论联系 

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

学跑的猿

制作不易

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值