且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Spring boot 学习笔记

更新时间:2022-06-19 09:02:32


1.spring boot 特点

Spring boot 学习笔记

 

2. 创建工程

Spring boot 学习笔记

 

最后的文件结构如下图

Spring boot 学习笔记

 

3. Gril.java

  

4. ComApplication

1
2
3
4
5
6
7
8
9
10
11
12
package girl;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
 
@SpringBootApplication
public class ComApplication {
 
    public static void main(String[] args) {
        SpringApplication.run(ComApplication.class, args);
    }
}

  

5. 创建接口

1
2
3
4
5
6
7
8
9
package girl;
 
import org.springframework.data.jpa.repository.JpaRepository;
 
/**
 * Created by Think on 2017/10/3.
 */
public interface GirlRepository extends JpaRepository<Girl, Integer>{
}

  

5. GirlProperties.java

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package girl;
 
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
 
/**
 * Created by Think on 2017/10/3.
 */
@Component
@ConfigurationProperties(prefix = "girl")
public class GirlProperties {
    private String cupSize;
 
    public String getCupSize() {
        return cupSize;
    }
 
    public void setCupSize(String cupSize) {
        this.cupSize = cupSize;
    }
 
    private Integer age;
 
    public Integer getAge() {
        return age;
    }
 
    public void setAge(Integer age) {
        this.age = age;
    }
}

  

6. 配置文件

使用的数据库为MySql

application.yml

1
2
3
4
5
6
7
8
9
10
11
12
spring:
  profiles:
    active: prod
  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

  

application-dev.yml

1
2
3
4
5
server:
  port: 8080
girl:
  cupSize: B
  age: 18

  

application-prod.yml

1
2
3
4
5
server:
  port: 8082
girl:
  cupSize: F
  age: 18

  

7. Controller文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package girl;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
 
import java.util.List;
 
/**
 * Created by Think on 2017/10/3.
 */
@RestController
public class GirlController {
 
    @Autowired
    private GirlRepository girlRepository;
 
    @Autowired
    private GirlService girlService;
 
    /**
     * 查詢所以女生列表
     * @return
     */
    @GetMapping(value = "/girls")
    public List<Girl> 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);
 
    }
 
    @PostMapping(value = "/girls/two")
    public void  girlTwo(){
        girlService.insertTow();
    }
}

   

8. pom配置

  


本文转自Work Hard Work Smart博客园博客,原文链接:http://www.cnblogs.com/linlf03/p/7629418.html,如需转载请自行联系原作者