创建maven项目
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>springboot</groupId>
<artifactId>springboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>springboot</name>
<description/>
<properties>
<webVersion>3.0</webVersion>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.7.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--spring-boot-starter-test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
启动类Application
启动application有以下方法:
1.运行Application
2.maven编译后,调用Java -jar springboot-0.0.1-SNAPSHOT.war
package com.wangh.main;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Application.yml配置文件
在src/main/resource下创建Application.yml
server:
port: 8080
context-path: /wh
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/dbgirl
username: root
password: easted2013
jpa:
show-sql: true
hibernate:
ddl-auto: update #create(每次运行删除旧表)、update(不删除旧表)、create-drop(程序终止后删除表)
#自定义属性
girl:
age: 18
size: B
content: "age: ${girl.age},size: ${girl.size}" #引用配置文件
GirlProperties
package com.wangh.main;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix="girl")
public class GirlProperties {
private Integer age;
private String size;
private String content;
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getSize() {
return size;
}
public void setSize(String size) {
this.size = size;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
HelloController
package com.wangh.main;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@Resource
private GirlProperties girlProperties;
@RequestMapping(value="/hello",method=RequestMethod.GET)
public String say(){
return "Hello Spring boot!";
}
//引入配置文件
@RequestMapping(value="/girl",method=RequestMethod.GET)
public String girl(){
return girlProperties.getContent();
}
}