SpringBoot可以用application.properties来加载环境,但是在SpringBoot下有一个更方便的叫做application.yml文件来加载配置。。。
spring:
profiles:
active: prod
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/test
username: root
password: root
jpa:
hibernate:
ddl-auto: create //加载程序的时候将会重新在数据库中建立一张新的表
show-sql: true
执行新表的语句是Hibernate: drop table if exists db_girl
Hibernate: create table db_girl (id integer not null auto_increment, age integer, cup_size varchar(255), primary key (id))
server:
port: 8080
context-path: /imooc
girl:
cupSize: B
age: 18
content: "cupSize:${cupSize} and age is ${age}"
程序通过配置取值的方法
@Value("${cupSize}")
private String cupSize;
@Value("${content}")
private String content;
package com.example.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* Created by chenqiuchang on 2017/3/11.
*/
@Controller
public class TemplateController {
@GetMapping(value = {"/hiTemplate"})
public String say(){
return "index";
}
}
如果该controller只是用来跳转页面,在SpringBoot中用template存放,需要导入相关jar包
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-thymeleaf -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
<version>1.5.1.RELEASE</version>
</dependency>
在resources中templates下增加index.html的页面,当访问http://localhost:8081/imooc/hiTemplate 的时候将会跳转到index.html上