2小时学会spring Root(慕课网)

1-1.特点

1.化繁为简,简化配置
2.备受关注,下一代框架
3.入门级别的框架


SpringBoot<-SpringCloud<-微服务


2-1.第一个SpringBoot的应用

java

版本:1.8.0

maven

版本:3.3.9



intelliJ IDEA 
Community:社区版
Ultimate:旗县版
下载Ultimate版本


2小时学会spring Root(慕课网)_第1张图片


启动方式
方式1:在intelliJ工具中启动
方式2:用命令启动,到项目目录下面。运行:mvn spring-boot:run
方式3:mvn install,
然后可以在项目下面的"target"目录下面看到项目de jar文件
java -jar girl-0.0.0.1-SNAPSHOT.jar


3-1.项目属性配置

GirlApplication.java
每次可以通过运行该方法,启动服务。
package com.imooc;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class GirlApplication {

	public static void main(String[] args) {
		SpringApplication.run(GirlApplication.class, args);
	}
}



配置文件application.properties和application.yml任意一个就行

application.properties

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

application.yml

server:
  port: 8081
  context-path: /girl


项目访问路径是:http://localhost:8080/girl/hello


情况一:

application.xml

server:
  port: 8080
cupSize: B
age: 18
content: "cupSize:${cupSize},age:${age}"

HelloController.java

package com.imooc;

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;

@RestController
public class HelloController {

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

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

    @Value("${content}")

    @RequestMapping(value="/hello")
    public String say(){
        return cupSize + age;
    }
}

访问路径是:http://localhost:8080/hello


情况二:

application.yml

server:
  port: 8080
girl:
  cupSize: B
  age: 18
增加java方法定义girl开头的变量

GirlProperties
package com.imooc;

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

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

    /**下面省略get/set方法**/
    /**... ...**/
}

HelloController.java

package com.imooc;

import org.springframework.beans.factory.annotation.Autowired;
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;


@RestController
public class HelloController {

    @Autowired
    private GirlProperties girlProperties;


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


访问路径是:http://localhost:8080/hello

情况三

application复制出来两个结构相同的文件

application-dev.yml

application-prod.yml

然后在application.yml指定用哪个配置文件启动

2小时学会spring Root(慕课网)_第2张图片



用命令方式启动

1.进入项目目录下,执行

mvn install

2.根据一个项目文件启动项目

java -jar target/girl-0.0.1-SNAPSHOT.jar --spring.profiles.active=prod


4-1.Controller的使用

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


1. @Controller使用举例

pom.xml增加

<dependency>
   <groupId>org.springframework.bootgroupId>
   <artifactId>spring-boot-starter-thymeleafartifactId>
dependency>

Controller里


2小时学会spring Root(慕课网)_第3张图片


@RequestMapping(value={"/hello","/hi"},method=RequestMethod.GET)
映射了两个地址


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

情况一

Controller的写法

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

访问路径:http://localhost:8081/hello/44

情况二

Controller的写法

@RequestMapping(value="/hello",method=RequestMethod.GET)
public String say(@RequestParam("id") Integer id){
    return "id:"+id;
}
访问路径:http://localhost:8081/hello?id=2

id为空会报错。那么下面设置可以避免

/*@RequestMapping(value="/hello",method=RequestMethod.GET)*/
/*简化RequestMethod.GET方式。同理还有@PostMapping*/
@GetMapping(value="/hello")
public String say( @RequestParam(value= "id",required= false,defaultValue = "0") Integer id){ return "id:"+id;}





5-1.数据库操作(上)

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

2小时学会spring Root(慕课网)_第4张图片

5-2.数据库操作(下)

pom.xml

<dependency>
   <groupId>org.springframework.bootgroupId>
   <artifactId>spring-boot-starter-data-jpaartifactId>
dependency>
<dependency>
   <groupId>mysqlgroupId>
   <artifactId>mysql-connector-javaartifactId>
dependency>

application.yml

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

girl.java

package com.imooc;

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

@Entity
public class Girl {

    @Id
    @GeneratedValue
    private Integer id;
    private String cupSize;
    private Integer age;

    public Girl() {
    }

    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;
    }
}

GirlRepository.java

package com.imooc;

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

import java.util.List;

public interface GirlRepository extends JpaRepository {

    //通过年龄来查询,扩展接口
    public List findByAge(Integer age);
}

GirlController.java

package com.imooc;

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

import java.util.List;


@RestController
public class GirlController {

    @Autowired
    private GirlRepository girlRepository;

    /**
     * 查询所有女生列表
     * @return
     */
    @GetMapping(value="/girls")
    public List girlList(){
        return girlRepository.findAll();
    }

    /**
     * 添加一个女生
     * @param cupSize
     * @param age
     * @return
     */
    @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);

    }

    /**
     * 查询一个女生
     * @return
     */
    @GetMapping(value="/girls/{id}")
    public Girl girlFindOne(@PathVariable("id") Integer id){
        return girlRepository.findOne(id);
    }

    /**
     * 更新
     */
    @PutMapping(value="/girls/{id}")
    public Girl girlUpdate(@PathVariable("id") Integer id,
                           @RequestParam("cupSize") String cupSize,
                           @RequestParam("age") Integer age){
        Girl girl = new Girl();
        girl.setId(id);
        girl.setCupSize(cupSize);
        girl.setAge(age);
        return girlRepository.save(girl);
    }

    /**
     * 删除
     */
    @DeleteMapping(value="/girls/{id}")
    public void girlDelete(@PathVariable("id") Integer id){
        girlRepository.delete(id);

    }

    //通过年龄查询女生列表
    @GetMapping(value="/girls/age/{age}")
    public List girlListByAge(@PathVariable("age") Integer age){
        return girlRepository.findByAge(age);
    }
}



6-1.事务管理

@Transactional


你可能感兴趣的:(2小时学会spring Root(慕课网))