首先在网上找到了一个写的比较好的文章-关于spring-boot
点击打开链接
maven相关的配置:
4.0.0
ProjectMaven
excel
war
0.0.1-SNAPSHOT
excel Maven Webapp
http://maven.apache.org
junit
junit
3.8.1
test
org.springframework.boot
spring-boot-starter-web
org.apache.commons
commons-compress
1.8.1
excel
org.springframework.boot
spring-boot-maven-plugin
UTF-8
1.7
7.0.55
org.springframework.boot
spring-boot-starter-parent
1.2.2.RELEASE
jdk1.7,tomcat1,7
启动服务器:
import java.util.HashMap;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
@EnableAutoConfiguration
public class FirstController {
@RequestMapping(value="/")//是springmvc中的注解
@ResponseBody
HashMap home(){
HashMap hasm = new HashMap();
for(int i=0;i<16;i++)
{
hasm.put("i"+i, "feng-"+i);
}
return hasm;
}
public static void main(String[] args) throws Exception {
SpringApplication.run(FirstController.class, args);
SpringApplication sap = new SpringApplication(FirstController.class);
//sap.setShowBanner(false);//关闭一些日子记录
}
}
spring boot有两种配置文件,
application.properties
server.port=9090
server.context-path=/girl
application.yml
server:
port: 8082
cupSize: B
application.yml
同时有几种启动方式,1,直接右键run application 2,移动到当前项目下,执行 mvn spring-boot:run 命令即可。
@RestController
public class HelloControl {
@Value("${server.cupSize}")
private String cupSize;
@RequestMapping(value = "/hello",method = RequestMethod.GET)
public String hello(){
return "cupSize="+cupSize;
}
}
自动从配置文件中读取前缀为girl的属性
@Component
@ConfigurationProperties(prefix = "girl")
public class GirlProperties {
private String cupSize;
private Integer age;
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getCupSize() {
return cupSize;
}
public void setCupSize(String cupSize) {
this.cupSize = cupSize;
}
}
springboot可用同时启动测试环境和正式环境
springboot 中的@RestController 代表返回json数据,是spring4新增的注解。等于之前的@Controller+@responseBody.
pathvariable 获取url中的数据 比如http://localhost:9090/hello/23 可用获取到23
@RequestMapping(value ="/hello/{id}",method = RequestMethod.GET)
public String hello(@PathVariable("id") int id){
return id+"";
}
RequestParam 获取请求参数的值
http://localhost:8082/hello?id=3234
public String hello(@RequestParam("id") int id){
return id+"====";
}
@GetMapping(value="/hello")=====@RequestMapping(value ="/hello",method = RequestMethod.GET)
@PostMapping(value="/hello")=====@RequestMapping(value ="/hello",method = RequestMethod.POST)
是一种组合注解,简化
JAP是一种对象持久化的规范。
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/xf
username: root
password: xiongfeng
配置数据源,并且写全。
yml配置文件,一定要注意格式对其 。
AOP:面向切面编程,通用逻辑从业务逻辑中分离出来。