springboot入门

SpringBoot

编码 配置 部署 监控 -> 简单

SpringBoot和SpringMVC的关系

SpringMVC->SpringBoot

SpringBoot的特点

1.化简为简,简化配置
2.备受关注,是下一代框架

微服务->SpringCloud->SpringBoot

前置知识
利用maven构建项目 ->《项目管理利器》
Spring注解。 -> 《Spring入门篇》
RESTful API

课程学到
第一个SpringBoot程序
自定义属性配置
Controller的使用
spring-data-jpa
事务管理

第一个SpringBoot应用

New Project -> Spring Initializr -> next -> Artifact:girl Group:com.binperson -> next -> Web web


    
        
            nexus-aliyun
            *
            Nexus aliyun
            http://maven.aliyun.com/nexus/content/goups/public
        
    

spring-boot-starter-web作为web项目必须引入的依赖
spring-boot-starter-test单元测试

启动

方法一

GirlApplication run

http://localhost:8080/hello

package com.binperson;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by binperson on 2017/5/14.
 */
@RestController
public class HelloController {

    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public String say() {
        return "hello Spring Boot!";
    }
}

方法二

cd gril
mvn spring-boot:run

方法三

mvn install
cd target
ll
java -jar girl-0.0.1-SNAPSHOT.jar

属性配置

spring.datasource.url:jdbc:mysql://127.0.0.1:3306/
spring.datasoucce.username:root
spring.datasource.password:
spring.datasource.driver-class-name: com.mysql.jdbc

delete static templates

resources -> application.properties

server.port=8081
server.content-path=/girl


application.yml
server:
    port: 8081
    context-path: /girl
    
http://localhost:8081/girl/hello    
server:
    port: 8080
cupSize: A
age: 18
content: "supSize: ${cupSize}, age: ${age}"

package com.binperson;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by binperson on 2017/5/14.
 */
@RestController
public class HelloController {
    @Value("${cupSize}")
    private String cupSize;

    @Value("${age}")
    private Integer age;

    @Value("${content}")
    private String content;

    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public String say() {
        return cupSize + age + content;
    }

}
server:
  port: 8080
girl:
  cupSize: B
  age: 18
  
新建一个类GirlProperties
package com.binperson;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * Created by binperson on 2017/5/14.
 */
@Component
@ConfigurationProperties(prefix = "girl")
public class GirlProperties {
    private String cupSize;
    private Integer age;

    public String getCupSize() {
        return cupSize;
    }

    public void setCupSize(String cupSize) {
        this.cupSize = cupSize;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}

package com.binperson;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by binperson on 2017/5/14.
 */
@RestController
public class HelloController {
    @Autowired
    private GirlProperties girlProperties;

    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public String say() {
        return girlProperties.getCupSize();
    }

}

application-dev.yml
server:
  port: 8080
girl:
  cupSize: B
  age: 18
application-prod.yml
server:
  port: 8080
girl:
  cupSize: F
  age: 18
application.yml
spring:
  profiles:
    active: dev

Controller的使用

@Controller 处理http请求
@RestController Spring4之后新加的注解,原来返回json需要@ResponseBody配合@Controller
@RequestMapping 配置url映射

package com.binperson;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;


/**
 * Created by binperson on 2017/5/14.
 */
@Controller
public class HelloController {
    @Autowired
    private GirlProperties girlProperties;

    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public String say() {
        return girlProperties.getCupSize();
    }

}

resources -> templates -> index.html



    
    Title


    

hello sb

package com.binperson; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; /** * Created by binperson on 2017/5/14. */ @Controller public class HelloController { @Autowired private GirlProperties girlProperties; @RequestMapping(value = "/hello", method = RequestMethod.GET) public String say() { return "index"; } }
package com.binperson;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by binperson on 2017/5/14.
 */
@RequestMapping("/bin")
@RestController
public class HelloController {
    @Autowired
    private GirlProperties girlProperties;

    @RequestMapping(value = {"/hello", "/hi"}, method = RequestMethod.POST)
    public String say() {
        return girlProperties.getCupSize();
    }

}

@PathVariable 获取url中的数据
@RequestParam 获取请求参数的值
@GetMapping 组合注解

http://localhost:8080/bin/say/13

package com.binperson;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by binperson on 2017/5/14.
 */
@RequestMapping("/bin")
@RestController
public class HelloController {
    @Autowired
    private GirlProperties girlProperties;

    @RequestMapping(value = {"/say/{id}"}, method = RequestMethod.GET)
    public String say(@PathVariable("id") Integer id) {
        return "id:" + id;
        //return girlProperties.getCupSize();
    }

}
http://localhost:8080/bin/say/11?id=111

package com.binperson;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Created by binperson on 2017/5/14.
 */
@RequestMapping("/bin")
@RestController
public class HelloController {
    @Autowired
    private GirlProperties girlProperties;

    @RequestMapping(value = {"/say/{id}"}, method = RequestMethod.GET)
    public String say(@RequestParam("id") Integer myid) {
        return "id:" + myid;
        //return girlProperties.getCupSize();
    }

}


http://localhost:8080/bin/say/11?id=

package com.binperson;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Created by binperson on 2017/5/14.
 */
@RequestMapping("/bin")
@RestController
public class HelloController {
    @Autowired
    private GirlProperties girlProperties;

    @RequestMapping(value = {"/say/{id}"}, method = RequestMethod.GET)
    public String say(@RequestParam(value = "id", required = false, defaultValue = "0") Integer myid) {
        return "id:" + myid;
        //return girlProperties.getCupSize();
    }

}

package com.binperson;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Created by binperson on 2017/5/14.
 */
@RequestMapping("/bin")
@RestController
public class HelloController {
    @Autowired
    private GirlProperties girlProperties;

    //@RequestMapping(value = {"/say/{id}"}, method = RequestMethod.GET)
    @GetMapping(value = "/say")
    public String say(@RequestParam(value = "id", required = false, defaultValue = "0") Integer myid) {
        return "id:" + myid;
        //return girlProperties.getCupSize();
    }

}

数据库操作

Spring-Data-Jpa

JPA(Java Persistence API)定义了一系列对象持久化的标准,目前实现这一规划的产品有Hibernate、TopLink等。

RESTful API设计

请求类型 请求路径 功能
GET /girls 获取女生列表
POST /girls 创建一个女生
GET /girl/id 通过id查询一个女生
PUT /girl/id 通过id更新一个女生
DELETE /girls/id 通过id删除一个女生


    org.springframework.boot
    spring-boot-starter-data-jpa



    mysql
    mysql-connector-java


application.yml

spring:
  profiles:
    active: dev
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/dbgirl
    username: root
    password:
  jpa:
    hibernate:
      ddl-auto: create
    show-sql: true
    
    
package com.binperson;


import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

/**
 * Created by binperson on 2017/5/14.
 */
@Entity
public class Girl {
    @Id
    @GeneratedValue
    private Integer id;
    private String cupSize;
    private Integer age;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getCupSize() {
        return cupSize;
    }

    public void setCupSize(String cupSize) {
        this.cupSize = cupSize;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Girl() {

    }
}


ddl-auto: update    

数据库

package com.binperson;

import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

/**
 * Created by binperson on 2017/5/16.
 */
public interface GirlRepository extends JpaRepository {
    //通过年龄查询
    public List findByAge(Integer age);
}

事务管理

查询的时候不需要事务

package com.binperson;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created by binperson on 2017/5/16.
 */
@Service
public class GirlService {
    @Autowired
    private GirlRepository girlRepository;

    @Transactional
    public void insertTwo() {
        Girl girlA = new Girl();
        girlA.setCupSize("A");
        girlA.setAge(18);
        girlRepository.save(girlA);

        Girl girlB = new Girl();
        girlB.setCupSize("A");
        girlB.setAge(19);
        girlRepository.save(girlB);
    }
}


package com.binperson;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by binperson on 2017/5/16.
 */
@RestController
public class GirlController {

    @Autowired
    private GirlRepository girlRepository;

    @Autowired
    GirlService girlService;

    @GetMapping(value = "/girls")
    public List girlList() {
        return girlRepository.findAll();
    }

    @PostMapping(value = "girls")
    public Girl girlAdd(@RequestParam("cupSize") String cupSize,
                          @RequestParam("age") Integer age) {
        Girl girl = new Girl();
        girl.setCupSize(cupSize);
        girl.setAge(age);

        return girlRepository.save(girl);
    }

    @GetMapping(value = "/girls/{id}")
    public Girl girlFindOne(@RequestParam("id") Integer id) {

        return girlRepository.findOne(id);
    }

    @PutMapping(value = "/girls/{id}")
    public Girl girlUpdate(@RequestParam("id") Integer id,
                           @RequestParam("age") Integer age,
                           @RequestParam("cupSize") String cupSize){
        Girl girl = new Girl();
        girl.setId(id);
        girl.setCupSize(cupSize);
        girl.setAge(age);
        return girlRepository.save(girl);
    }
    @DeleteMapping(value = "/girls/{id}")
    public void girlDelete(@RequestParam("id") Integer id){
        girlRepository.delete(id);
    }

    @GetMapping(value = "/girls/age/{age}")
    public List girlListByAge(@PathVariable("age") Integer age){
        return girlRepository.findByAge(age);
    }

    @PostMapping(value = "/girls/two")
    public void girlTwo() {
        girlService.insertTwo();
    }
}

你可能感兴趣的:(springboot入门)