这两天开始接触Spring Boot ,也看了不少博客,此篇博客仅为记录作为一个小白的我的逐步成长。
一:Spring Boot 的优势
刚接触不久,仅现在我用到的看来:
1:配置不麻烦
2:搭建速度快
3:现在很火,微服务确实火,大家可以上百度指数上对比spring boot 和spring mvc。
二:搭建过程
搭建spring boot项目主要方式:1,idea 2:
maven创建 3:jar自建 4:网站方式
首先,我用的是sts直接创建的项目,感觉很好用,不过网站自动创建也很不错,我没试过,大家有兴趣可以试试。
也没什么难的,创建完成后会是个jar包,下载解压到工作空间就行。
我用sts搭建的,主要是里面有插件,好用。
1: new---spring start project
solr,redis 无所谓,我加上防止以后用到,DevTools可以热部署,我没做其他配置,但是前后端都可以热部署。
2:创建完成后,我把之前的一个简单ssm框架直接复制上去了,不赘述。配置application.properties
前面四个是数据库连接,mybatis.configuration.map-underscore-to-camel-case=true true表示开启驼峰转换,后面几个是
改端口,和使用thymeleaf,这是官方认可的,jsp也行,但是好像少功能,没试过,网上很多配置使用jsp的,不做赘述。
文件夹位置如图,html放在templates文件夹下,是默认扫描此文件夹,
写个简单的测试:
index.html:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
<title>Hello World!</title>
</head>
<body>
<h1 th:inline="text">i am Strong</h1>
<p th:text="${strong}"></p>
<p th:text="${strong1}"></p>
</body>
</html>
c
然后是controller
Index.java如下
package com.ssm.controller;
import java.util.Map;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class Index {
@RequestMapping(value="/index")
public String test(Map<String,Object> map){
map.put("strong", "fight!");
map.put("strong1", "fighting!");
return "index";
}
}
然后在SpringInitApplication.java中运行,代码如下
@ComponentScan和@MapperScan是扫描指定路径的包。
右键----Run As ---Spring Boot App
Started SpringInitApplication in 5.273 seconds (JVM running for 5.891) 意指启动成功,浏览器打开
端口被占用了,直接就改成9090了,展示效果如图,Spring Boot是不需要加项目名称的。
这就是完整的走完的一个简单流程的Spring Boot。
如有错误,请各位大神指正,谢谢!