整合springboot+mybatis+thymeleaf

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介: 最近刚在学习这个springboot,刚开始学习,然后自己就迫不及待的去整合了一个小实例   1:因为spring-boot这个案例因为整合的是thymeleaf所以在创建maven项目的时候选择jar的方式, 如果整合的是jsp页面的话,那就要选择war的方式,要不在运行的时候会报错,4...

最近刚在学习这个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

  

 

欢迎大家一起说出自己的想法。
相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
28天前
|
前端开发 Java 关系型数据库
SpringBoot+MyBatis 天猫商城项目
SpringBoot+MyBatis 天猫商城项目
47 1
|
27天前
|
SQL Java 数据库连接
springboot中配置mybatis别名该怎么写?
springboot中配置mybatis别名该怎么写?
20 0
|
1月前
|
XML Java 关系型数据库
【SpringBoot系列】SpringBoot集成Fast Mybatis
【SpringBoot系列】SpringBoot集成Fast Mybatis
|
27天前
|
SQL JavaScript Java
springboot+springm vc+mybatis实现增删改查案例!
springboot+springm vc+mybatis实现增删改查案例!
22 0
|
8天前
|
SQL Java 数据库连接
【mybatis】第一篇,Springboot中使用插件PageHelper不生效解决方案
【mybatis】第一篇,Springboot中使用插件PageHelper不生效解决方案
|
20天前
|
JavaScript Java 关系型数据库
SpringBoot + Mybatis + Vue的代码生成器
SpringBoot + Mybatis + Vue的代码生成器
31 2
|
27天前
|
Java 关系型数据库 MySQL
springboot+mybatis-plus实例demo
springboot+mybatis-plus实例demo
28 0
|
29天前
|
SQL Java 关系型数据库
MyBatisPlus学习笔记(SpringBoot版)
MyBatisPlus学习笔记(SpringBoot版)
98 0
|
30天前
|
前端开发 JavaScript Java
springboot+mybatis plus+vue+elementui+axios 表格分页查询demo
springboot+mybatis plus+vue+elementui+axios 表格分页查询demo
32 0
|
1月前
|
Oracle Java 关系型数据库
SpringBoot整合Mybatis连接Oracle数据库
SpringBoot整合Mybatis连接Oracle数据库
SpringBoot整合Mybatis连接Oracle数据库