1. 程式人生 > >使用IDEA和Maven搭建一個Spring Boot專案

使用IDEA和Maven搭建一個Spring Boot專案

Spring Boot是Spring MVC的升級版
嵌入的Tomcat,無需部署War檔案
簡化Maven配置
自動配置Spring

IDEA用過的人再也不想用eclipse了,自從我入坑IDEA之後,就沒怎麼開啟過eclipse了。這就好像和一個從來都沒有使用固態硬碟的人有著同樣的感慨,“再也回不去了“。

Alt text

Alt text

Alt text

GroupId可以改成自己的域名,其他預設,然後一路next,最後finish。

Alt text

這是專案的結構目錄,首次執行maven,編譯器會下載大量的jar包,如果下載太慢,可以在setting.xml檔案裡面配置阿里Maven倉庫映象。

<mirror
>
<!--This sends everything else to /public --> <id>nexus-aliyun</id> <mirrorOf>*</mirrorOf> <name>Nexus aliyun</name> <url>http://maven.aliyun.com/nexus/content/groups/public</url> </mirror>

此時Spring Boot已經預設配置好了pom.xml檔案

專案屬性配置

如下圖所示新建一個HelloController.java檔案

Alt text

Alt text

  1. @Value 從配置檔案讀取引數
  2. @ConfigurationProperties 把yml裡面一組配置引數封裝成一個類
  3. @Component 向SpringBoot註冊一個類
  4. @Autowired 注入一個類
  5. @RestController = @Controller + @ResponseBody,Spring4 之後新加的註解,原來返回json需要@ResponseBody + @Controller

在Test目錄下有一個application.properties

配置檔案,可以配置專案屬性:

Alt text

這裡推薦使用application.yml配置檔案,相對於properties更加簡潔明瞭。

Alt text

方式一:在配置檔案中設定一個name屬性,然後將此name屬性通過註解注入到name屬性變數。

Alt text

Alt text

方式二:也可以這麼來使用,在配置檔案中配置一個content屬性,然後將其他屬性包含在內,在程式return出來。效果如下所示。

Alt text

Alt text

Alt text

方式三:還可以在yml配置檔案中直接給一個物件設定屬性以及屬性值

Alt text

將此屬性注入:

Alt text

Alt text

可以看到上圖有個地方報錯,在pom.xml檔案配置加上:

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-configuration-processor</artifactId>
  <optional>true</optional>
</dependency>

這樣錯誤就消失了。
瀏覽器就可以訪問到student資料了。

有時候需要使用同樣的yml配置,但配置內容不一樣。生成環境application-prod.yml和開發環境application-dev.yml
比如在主配置檔案application.yml中配置:

server:
  profiles:
    active: dev

可以直接呼叫devprod配置屬性,可以來回切換生產環境和開發環境。

Controller的使用

Alt text

這裡寫圖片描述

演示@RequestMapping@PathVariable的作用,在URL位址列中輸入id,然後自己獲取此id並且在瀏覽器上打印出來。

Alt text

Alt text

還有一種就是問號傳值方式,可以設定id預設值,比如預設值為0.

Alt text

Alt text

如果地址中不傳送id,預設為0。

@RequestMapping還可以被@GetMapping@PostMapping取代。

資料庫操作

JAP(Java Persistence API)定義了一系列的物件持久化標準,目前實現這一規範的有Hibernate、TopLink等。
在pom檔案中新增

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>

application.yml配置JPA和資料庫

spring:
  profiles:
    active: a
  datasource:
      driver-class-name: com.mysql.jdbc.Driver
      url: jdbc:mysql://127.0.0.1:3306/db_student
      username: root
      password: root

  jpa:
    hibernate:
      ddl-auto: update
    show-sql: true

建立資料庫表對應的實體類Student,執行之後,資料庫會自動建表student。

建立一個介面PersonRepository,位於dao包下,StudentController呼叫該介面繼承自JpaRepository的方法,來實現和資料庫互動。

    public interface PersonRepository extends JpaRepository<Person,Integer> {

    }
@RestController
public class StudentController {
    @Autowired
    private StudentRepository studentRepository;
    @GetMapping(value = "/student")
    private List<Student> studentList() {
        return studentRepository.findAll();
    }
}

瀏覽器輸入http://localhost:8081/student

事務管理

兩條 sql 語句同時在一個方法中執行,為了防止一個 sql 語句執行成功而另一個 sql 語句執行失敗,引入了事務管理,需要在方法上加 @Transactional事務註解。事務確保了資料庫資料的完整性和一致性。