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里面去启动了。

相关文章
相关标签/搜索