java流行框架之Springboot基础篇
•发布于   •作者 王家明  •577 次浏览  •最后一次编辑是   •来自 博客

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个环境:

  • 启动application-dev.yml环境

        运行GirlApplication.java类,浏览器访问:http://localhost:8080/springboot/hello/say?id=1

  • 启动application-debug.yml环境

        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
  • 通过属性获取application-dev.yml配置文件中的参数
public class HelloController {
    @Value("${girl.name}")
    private String name;//获取application-dev.yml配置文件中的属性
}
  • 通过对象获取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注解

        使用@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";
    }
}

页面放置目录:

  • @RestController注解 返回json

        相当于@Controller+@ResponseBody

@RestController
@RequestMapping("/hello")
public class HelloController {
    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public String Hello() {
        return "name" + name+ "age" + age;
    }
}
  • @RequestMapping配置多个路径
@RequestMapping(value = {"/index","/index1","/index2"}, method = RequestMethod.GET)
  • @PathVariable :获取url中的数据
@RequestMapping(value = "/{id}/hello3")
public String Hello03(@PathVariable(name = "id",required = false) int id){
    return "id:"+id;
}

http://localhost:8080/hello/100/hello3

  • @RequestParam :获取请求参数的值
@RequestMapping(value = "/hello3")
public String Hello04(@RequestParam(name = "id",required = false) int id){
    return "id:"+id;
}

http://localhost:8080/hello/hello3?id=1212

  • REST风格注解:
@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

【详解】为什么选择Spring Boot作为微服务的入门级微框架:http://www.csdn.net/article/a/2016-05-12/15838098


1 回复
Jencks



回到顶部

©2017 Powered by 三十三行伪代码
皖ICP备17005175号-3