spring boot 學習一 springboot項目初步搭建

環境:eclipse+jdk1.8+tomcate8.03html

參考學習地址:http://www.ityouknow.com/spring-boot.htmljava

springboot項目能夠直接在https://start.spring.io/網上建立項目mysql

下載後,經過maven導入項目(項目空白區右鍵-Import-Import-Maven-Existing Maven Projects) 導入項目web

包下面有一個配置類Application.javaspring

能夠直接右鍵啓動,輸出sql

表明成功!數據庫

若是依賴包添加了數據庫依賴,則須要在resources下面的application.properties填寫數據庫配置,以mysql爲例tomcat

1 spring.datasource.url=jdbc:mysql://localhost:3306/zz-erp?useUnicode=true&characterEncoding=utf8&useSSL=false
2 spring.datasource.username=root 3 spring.datasource.password=root

若是咱們想寫controllerspringboot

直接建立controller便可。app

若是前面沒有添加web依賴則須要在pom.xml中添加依賴

<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

  

注意:全部文件必須在Application.java所在包以及子包下面,不然沒法加載

 例如建立hello

@RestController
public class HelloController {

	@RequestMapping("/hello")
	public String getHello() {
		return "hello!my friend.";
	}
}

 而後在Application.java裏右鍵執行便可啓動。

    緣由:spring boot內置了tomcate,因此不須要使用eclipse的tomcate插件直接訪問 http://localhost:8080/項目名/hello

若是咱們不要他內置的tomcate,則須要調整

1.pom.xml 中添加 <packaging>war</packaging>

 如圖

2.移除內置tomcate依賴

3.Application.java類實現接口SpringBootServletInitializer

@SpringBootApplication
public class FmInterfaceApplication extends SpringBootServletInitializer {

	@Override
	protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
		return builder.sources(FmInterfaceApplication.class);
	}

	public static void main(String[] args) {
		SpringApplication.run(FmInterfaceApplication.class, args);
	}

}

  而後就能夠將項目添加到tomcate裏面去啓動了。

相關文章
相關標籤/搜索