Spring-Boot+gradle+thymleaf配置过程笔记
刚开始实习,公司目前要我先配置起来一个SpringBoot+gradle+thymleaf的架子,然后在网上也查了些资料,毕竟用gradle的感觉不多,所以准备自己做些整理,也希望能帮助到有需要的人。
-
可以通过修改build.gradle文件中的属性来提高下载速度
buildscript{ repositories{ maven { url "http://maven.aliyun.com/nexus/content/groups/public" } } } repositories{ maven { url "http://maven.aliyun.com/nexus/content/groups/public" } }
-
spring-boot项目中,在application.properties文件中配置数据库连接
spring.datasource.url=jdbc:mysql://localhost:3306/mydatabase?useSSL=false spring.datasource.username=root spring.datasource.password=root #数据库驱动 spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.jpa.hibernate.ddl-auto=create #是否在控制台显示数据库语句 spring.jpa.show-sql=true
-
既然需要使用SpringBoot,那这三个注解肯定会经常用到。
@RequestMapping、@GetMapping、@PostMapping的区别
-
@GetMapping是一个组合注解,是@RequestMapping(method=RequestMethod.GET)的缩写
-
@PostMapping是一个组合注解,是@RequestMapping(method=RequestMethod.POST)的缩写
-
@RequestMapping注解表示的是一个映射关系
@RestController @RequestMapping("/") public class IndexController { @RequestMapping("/home") String get() { //mapped to hostname:port/home/ return "Hello from get"; } @RequestMapping("/index") String index() { //mapped to hostname:port/home/index/ return "Hello from index"; } } 到 /home 的请求会由 get() 方法来处理,而到 /home/index 的请求会由 index() 来处理
-
-
POST和GET的区别
-
GET - 从指定的资源请求数据。
-
POST - 向指定的资源提交要被处理的数据
比较 GET 与 POST
下面的表格比较了两种 HTTP 方法:GET 和 POST。
GET POST 后退按钮/刷新 无害 数据会被重新提交(浏览器应该告知用户数据会被重新提交)。 书签 可收藏为书签 不可收藏为书签 缓存 能被缓存 不能缓存 编码类型 application/x-www-form-urlencoded application/x-www-form-urlencoded 或 multipart/form-data。为二进制数据使用多重编码。 历史 参数保留在浏览器历史中。 参数不会保存在浏览器历史中。 对数据长度的限制 是的。当发送数据时,GET 方法向 URL 添加数据;URL 的长度是受限制的(URL 的最大长度是 2048 个字符)。 无限制。 对数据类型的限制 只允许 ASCII 字符。 没有限制。也允许二进制数据。 安全性 与 POST 相比,GET 的安全性较差,因为所发送的数据是 URL 的一部分。在发送密码或其他敏感信息时绝不要使用 GET ! POST 比 GET 更安全,因为参数不会被保存在浏览器历史或 web 服务器日志中。 可见性 数据在 URL 中对所有人都是可见的。 数据不会显示在 URL 中。
-
-
@Controller和@RestController的区别
@RestController注解相当于@ResponseBody+@Controller合在一起的作用
-
如果只是使用@RestController注解controller,则controller中的方法的返回值都是字符串形式返回
-
@Controller则可以配合Thymleaf实现前端页面展示
-
-
gradle(4.3.1)中配置thymleaf
-
在build.gradle中添加以下代码
dependencies { implementation('org.springframework.boot:spring-boot-starter-thymeleaf') }
-
在前端页面添加标签库
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">
-
thymleaf缓存配置
每次更改页面,如果不配置Thymeleaf 缓存设置为false,那么每次更改html页面都需要重启页面才刷新。解决方法:在application.properties文件中添加以下代码
#Thymeleaf cache set spring.thymeleaf.cache=false
-
暂时就这么多,以后有问题会继续更新。