springboot基础:
1.特点:就像maven整合了所有的jar包,spring boot整合了所有的框架(不知道这样比喻是否合适)。
2.springboot依赖jar包
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.7</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
<!--用于对注解支持-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
3.启动springboot
@SpringBootApplication
public class GirlApplication {
public static void main(String[] args) {
SpringApplication.run(GirlApplication.class, args);
}
}
4.springboot配置文件(放在src/main/resources目录下)
方式一:application.properties
server.context-path=/shiro
server.port=8080
server.tomcat.max-threads=800
server.tomcat.uri-encoding=UTF-8
spring.datasource.type=com.zaxxer.hikari.HikariDataSource
spring.datasource.url=jdbc:log4jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=
#spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.driver-class-name=net.sf.log4jdbc.DriverSpy
#spring.jpa.show-sql=true
spring.http.encoding.charset=UTF-8
spring.http.encoding.force=true
spring.http.encoding.force-request=true
spring.http.encoding.force-response=true
spring.http.encoding.enabled=true
方式二(可配置多个运行环境):application.yml
#用于设置这个开发环境
spring:
profiles:
active: dev
datasource:
driver-class-name: org.gjt.mm.mysql.Driver
url: jdbc:mysql://localhost:3306/test
data-username: root
data-password:
jpa:
hibernate:
ddl-auto: update
show-sql: true
active: dev 表示使用application-dev.yml运行环境
运行环境一:application-dev.yml
server:
port: 8080
context-path: /springboot
运行环境二:application-debug.yml
server:
port: 8081
创建HelloController.java
@RestController
@RequestMapping("/hello")
public class HelloController {
@GetMapping(value = "/say")
public String say(@RequestParam(value = "id", required = false, defaultValue = "0") Integer myId) {
return "id: " + myId;
}
}
同时启动2个环境:
运行GirlApplication.java类,浏览器访问:http://localhost:8080/springboot/hello/say?id=1
cmd切换到当前项目路径下,使用命令 java -jar grace-0.1.0.jar --spring.profiles.active=debug
浏览器访问:http://localhost:8081/hello/say?id=1
5.使用application-dev.yml设置参数
server:
port: 8080
girl:
name: a
age: 18
public class HelloController {
@Value("${girl.name}")
private String name;//获取application-dev.yml配置文件中的属性
}
@Component //没有这个注解,controller中无法注入bean
@ConfigurationProperties(value = "girl") //获取前缀是girl的配置
public class Girl {
private String name;
private int age;
}
public class HelloController {
@Autowired
private Girl girl;
}
6.注解
使用@Controller注解返回视图
添加依赖
org.springframework.boot
spring-boot-starter-thymeleaf
@Controller
@RequestMapping("/hello")
public class HelloController {
@RequestMapping(value = {"/index"}, method = RequestMethod.GET)
public String index() {
return "index";
}
}
页面放置目录:
相当于@Controller+@ResponseBody
@RestController
@RequestMapping("/hello")
public class HelloController {
@RequestMapping(value = "/hello", method = RequestMethod.GET)
public String Hello() {
return "name" + name+ "age" + age;
}
}
@RequestMapping(value = {"/index","/index1","/index2"}, method = RequestMethod.GET)
@RequestMapping(value = "/{id}/hello3")
public String Hello03(@PathVariable(name = "id",required = false) int id){
return "id:"+id;
}
http://localhost:8080/hello/100/hello3
@RequestMapping(value = "/hello3")
public String Hello04(@RequestParam(name = "id",required = false) int id){
return "id:"+id;
}
http://localhost:8080/hello/hello3?id=1212
@RequestMapping(value = "/get", method = RequestMethod.GET)等同于@GetMapping(value="/get")
@RequestMapping(value = "/post", method = RequestMethod.POST)等同于@PostMapping(value="/post")
@RequestMapping(value = "/put", method = RequestMethod.PUT)等同于@PutMapping(value="/put")
@RequestMapping(value = "/delete", method = RequestMethod.DELETE)等同于@DeleteMapping(value="/delete")
下一篇:http://www.itstu.club/topic/d8be9faf7fee411697100f149aff0e9f