2小时学会SpringBoot

1.课程介绍

2.第一个springBoot应用

2.1新建项目

2.2创建HelloController类

package com.example.demo;

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 {
    @RequestMapping(value = "/hello",method = RequestMethod.GET)
    public String say(){
        return "hello,spring boot!";
    }
}

2.3运行测试

2.4第二种启动方式

2.5第三种启动方式

 

 

3.项目属性配置

3.1在application.properties配置

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

3.2在application.yml中配置

server:
  port: 8082
  servlet:
    context-path: /girl

3.3例子

修改application.yml文件

server:
  port: 8080

cupSize: B

修改HelloController.java

package com.example.demo;

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;

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

 

server:
  port: 8080

cupSize: B

age: 18
package com.example.demo;

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;

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

server:
  port: 8080

cupSize: B

age: 18

content: "cupSize: ${cupSize},age:${age}"
package com.example.demo;

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}")
    private String content;

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

application.yml

server:
  port: 8080

girl:
  cupSize: B
  age: 18

GirlProperties.java

package com.example.demo;

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

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

HelloController.java

package com.example.demo;

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",method = RequestMethod.GET)
    public String say(){
        return girlProperties.getCupSize();
    }
}

   

3.4创建一个生产和一个开发配置文件

 application.yml

spring:
  profiles:
    active: dev

application-dev.yml

server:
  port: 8082

girl:
  cupSize: B
  age: 18

application-prod.yml

server:
  port: 8081

girl:
  cupSize: F
  age: 18

4.Controller的使用

 

4.1@controller

 必须配合模板使用,否则报错

 

 

4.2@RestController

@RestController等同于@Controller和@ResponseBody的组合

4.3@RequestMapping

映射成两个url,或的关系

映射一个类

4.4@PathVariable

获取url中的数据

 

 

4.5@RequestParam

获取请求参数的值

参数缺省

4.6@GetMapping

组合注解:url+请求方式get

5.数据库操作

5.1自动建表

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: 123456
  jpa:
    hibernate:
      ddl-auto: create
    show-sql: true

Girl.java

package com.example.demo;

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() {
    }
}

5.2create和update的区别

若dbgirl表存在,且有数据,配置create会将dbgirl表删除,然后新建一个dbgirl;update则不会删除表

create

update

 

5.3示例

package com.example.demo;

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;
    }
}
package com.example.demo;

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

public interface GirlRepository extends JpaRepository<Girl, Integer> {
}
package com.example.demo;

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

import java.util.List;
import java.util.Map;

@RestController
public class GirlController {
    @Autowired
    private GirlRepository girlRepository;


    @GetMapping(value = "/girls")
    public List<Girl> girlList() {
        List<Girl> ls = girlRepository.findAll();
        for (Girl girl:ls) {
            System.out.println(girl);
        }
        return ls;
    }
}
spring:
  profiles:
    active: dev
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/dbgirl
    username: root
    password: 123456
  jpa:
    hibernate:
      ddl-auto: update
    show-sql: true

5.4增删改和id查询

package com.example.demo;

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

import javax.websocket.server.PathParam;
import java.util.List;

@RestController
public class GirlController {
    @Autowired
    private GirlRepository girlRepository;

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

    /**
     * 添加一个女生
     *
     * @param id
     * @param cupSize
     * @param age
     * @return
     */
    @PostMapping("/girls")
    public Girl girlAdd(@RequestParam("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);
    }

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

    /**
     * 更新一个女生
     */
    @PutMapping("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("girls/{id}")
    public void girlDelete(@PathVariable("id") Integer id){
        girlRepository.deleteById(id);
    }
}

5.5where查询

GirlRepository .java

package com.example.demo;

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

import java.util.List;

public interface GirlRepository extends JpaRepository<Girl, Integer> {
    //通过年龄来查询
    public List<Girl> findByAge(Integer age);
}
package com.example.demo;

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

import javax.websocket.server.PathParam;
import java.util.List;

@RestController
public class GirlController {
    @Autowired
    private GirlRepository girlRepository;


    /**
     * 通过年龄来查询
     */
    @GetMapping("/girls/age/{age}")
    public List<Girl> girlListByAge(@PathVariable("age") Integer age){
        return girlRepository.findByAge(age);
    }
}

6.事务管理

场景:插入两个女生,要么都插入成功,要么都插入失败,不要一个成功一个失败。

为了模拟上述场景,故意将数据库中的cupSize字段设置为长度1

package com.example.demo;

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

@Service
public class GirlService {

    @Autowired
    private GirlRepository girlRepository;

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

        Girl girlB = new Girl();
        girlB.setCupSize("BBBB");
        girlB.setAge(18);
        girlRepository.save(girlB);
    }
}
package com.example.demo;

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

import javax.websocket.server.PathParam;
import java.util.List;

@RestController
public class GirlController {

    @Autowired
    private GirlService girlService;


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

}

 

package com.example.demo;

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

import javax.transaction.Transactional;

@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("BBBB");
        girlB.setAge(18);
        girlRepository.save(girlB);
    }
}

增加了@Transactional注解之后,发现A和BBBB都没增加成功

原文地址:https://www.cnblogs.com/xinmomoyan/p/15111797.html