整合springboot+mybatis+thymeleaf

最近刚在学习这个springboot,刚开始学习,然后自己就迫不及待的去整合了一个小实例

1:因为spring-boot这个案例因为整合的是thymeleaf所以在创建maven项目的时候选择jar的方式,

如果整合的是jsp页面的话,那就要选择war的方式,要不在运行的时候会报错,404;

2:创建好项目之后就需要导入jar包依赖了,下面是pom文件,

 1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 2     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 3     <modelVersion>4.0.0</modelVersion>
 4     <groupId>gh</groupId>
 5     <artifactId>yonyou.com</artifactId>
 6     <version>0.0.1-SNAPSHOT</version>
 7     <build />
 8     
 9     <!-- 关于spring-boot父类的所有依赖 -->
10     <parent>
11         <groupId>org.springframework.boot</groupId>
12         <artifactId>spring-boot-starter-parent</artifactId>
13         <version>1.3.3.RELEASE</version>
14     </parent>
15     <dependencies>
16         <!-- 关于spring>>>web的依赖 -->
17         <dependency>
18             <groupId>org.springframework.boot</groupId>
19             <artifactId>spring-boot-starter-web</artifactId>
20         </dependency>
21 
22         <!-- 关于页面thymeleaf模版的依赖 -->
23         <dependency>
24             <groupId>org.springframework.boot</groupId>
25             <artifactId>spring-boot-starter-thymeleaf</artifactId>
26         </dependency>
27 
28         <!--关于mybatis的依赖 -->
29         <dependency>
30             <groupId>org.mybatis.spring.boot</groupId>
31             <artifactId>mybatis-spring-boot-starter</artifactId>
32             <version>1.1.1</version>
33         </dependency>
34 
35         <!--关于mysql的依赖 -->
36         <dependency>
37             <groupId>mysql</groupId>
38             <artifactId>mysql-connector-java</artifactId>
39             <version>5.1.21</version>
40         </dependency>
41 
42     </dependencies>
43 </project>

3:因为spring-boot是约定优于配置,所以你可以快速的创建一个可以运行的web项目。

    spring-boot默认扫描application.properties文件,所以我们需要创建一个这样的一个资源文件。在这个文件里面我们写一些配置,

 1 #thymeleaf start
 2 spring.thymeleaf.mode=HTML5
 3 spring.thymeleaf.encoding=UTF-8
 4 spring.thymeleaf.content-type=text/html
 5 spring.thymeleaf.cache=false
 6 #thymeleaf end
 7 
 8 #mybatis start
 9 spring.datasource.url=jdbc:mysql://localhost:3306/student
10 spring.datasource.driver-class-name=com.mysql.jdbc.Driver
11 spring.datasource.username=root
12 spring.datasource.password=123456
13 #mybatis end

    在这里我们配置了thymeleaf模版,和mybatis mysql。

    说明一下,thymeleaf的 这些配置不是必须的,如果配置了会覆盖默认的。 在开发时建议将spring.thymeleaf.cache设置为false,否则会有缓存,导致页面没法及时看到更新后的效果。

     1 spring.thymeleaf.prefix=classpath:/templates/

       2 spring.thymeleaf.suffix=.html 

      这两个在thymeleaf里面是可以不配置的,因为他会自动扫描templates文件夹下的所有html文件,如果有特殊需要可以通过上面的语句需改。

  4:spring-boot的静态资源文件都必须放在resources文件夹下的static下面,否者将找不到。

  5:最后结构目录如下:

  

  6:填充内部代码;

  7:运行结果图

源码下载:https://download.csdn.net/download/cengjianggh/10329103

  

原文地址:https://www.cnblogs.com/GH0522/p/8722975.html