1、使用IDEA創建springboot項目
2、初次使用,下載jar會很慢,使用阿里雲的鏡像:.m2/settings.xml:
<mirror>
<id>nexus-aliyun</id>
<mirrorOf>*</mirrorOf>
<name>Nexus aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>
3、項目下的pom.xml文件
4、主類:DemoApplication:
@SpringBootApplication //啓動需要的註解
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
5、配置
在appliction.properties: 中進行
#項目運行端口號,默認8080
server.port=8888
#項目名稱
server.context-path=/demo
#其他配置
訪問:http://localhost:8888/demo/xxx
使用application.yml配置。(格式簡便,注意:後面必須有一個空格)
server:
port: 8888
context-path: /demo
#以下是測試數據
level: A
age: 13
#配置文件中變量調用其他變量
content: "level: ${level}, age: ${age}"
6、Controller:
class上使用註解@RestConroller(Spring4之後的註解,相當於@ResponseBody配合@Controller)。
method上使用註解@RequestMapping,我們可以用簡潔的:@GetMapping, @PostMapping。
我們可以使用@Value屬性可以獲取配置文件中的值:
@Value("${level}")
private String level;
@Value("${age}")
private Integer age;
如果獲取多個配置,可以封裝:
application.yml:
test.level: A
test.age: 13
@Componet
@ConfigurationProperties(prefix="test")
public class TestProperties {
private String level;
private Integer age;
// ....
}
// 其他類中使用,直接用@Autowired注入該類
瞭解:使用teymeleaf依賴,在resources/thymeleaf/index.html. 控制器方法中直接返回 return “index”; 可以看到訪問到了界面。
如今開發前後端分離,後端返回json給前端即可。
7、數據庫:
Spring-Data-Jpa:
JPA定義了一系列對象持久化的標準,目前實現這一規範的產品有Hibernate、TopLink等。