SpringBoot項目開發初步
1、建立SpringBoot項目
file - new - project -spring initiallizr
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsIyZuBnLyM2MjJTNycTOwMmMmJDMhBDMjRDZmJjY1UmZzImNxQzLc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.png)
選擇下一步,在group中填寫我們的項目辨別,類似于根目錄
java version 選擇17
直接下一步next,這一步我們需要選擇需要那些插件,用的是mysql,選擇
lombok 提供了實體類用的get,set方法
spring web,
spring data jpa (封裝的用crud方法),
mysql driver.
輸入項目名稱: demo
項目位置選擇适當的本地硬碟路徑。DemoApplication類是我們程式入口
将application.properties删掉,我們用的是application.yml檔案,這個更清晰,内容很簡單,隻配置了一些資料庫連結,以及執行腳本的輸出控制
application.yml源代碼:
server:
port: 8181
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/demo2022?useUnicode=true&characterEncoding=utf-8
username: root
password: 123456
jpa:
show-sql: true
properties:
hibernate:
format_sql: true
因為vue項目中端口用的是8080,是以我們這裡需要改動下,這裡就手動設定為8181,以便能和vue友善互動
datasource下配置的為資料庫連接配接相關資訊
show-sql用來控制,控制台是否列印sql語句,format_sql用于列印的腳本日志格式化操作,預設是列印一行
以上我們項目就建立完成了,下面開始去和資料庫做互動
2:springboot的crud
1.在demo包下建立一個entity包,用來存放實體類
2.在entity包下建立一個實體類,因為我在資料庫中建立的表名為payment,是以實體類名我以Payment建立一個實體類,
下面是表結構
下面是實體類:
在src-main-java-com.example.demo下建立四個package包
entity
controller
config
repository
entity 包下面建立payment類
payment類
package com.example.demo.entity;
import lombok.Data;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
@Data
public class Payment {
@Id
private Integer id;
private String serial;
}
Entity注解用于和表映射
Data注解用于給id,serial添加get,set方法
Id注解用于表主鍵
package com.example.demo.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class CrosConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
//本應用的所有方法都會去處理跨域請求
registry.addMapping("/**")
//允許遠端通路的域名
.allowedOriginPatterns("*")
//允許請求的方法
.allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
.allowCredentials(true)
.maxAge(3600)
.allowedHeaders("*");
}
}
建立一個config包,并在config包下建立一個CrosConfig類,并實作WebMvcConfigurer接口,該類主要用于處理跨域問題,如果不處理的話,目前端在調用後端的接口時,若不在同一個域,就會無法通路。
package com.example.demo.repository;
import com.example.demo.entity.Payment;
import org.springframework.data.jpa.repository.JpaRepository;
public interface PaymentRepository extends JpaRepository<Payment,Integer> {
}
建立一個repository包
在該包下建立一個接口interface PaymentRepository,繼承JpaRepository
package com.example.demo.controller;
import com.example.demo.entity.Payment;
import com.example.demo.repository.PaymentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/payment")
public class PaymentHandler {
@Autowired
private PaymentRepository paymentRepository;
@GetMapping("/findAll")
public List<Payment> findAll(){
return paymentRepository.findAll();
}
}
建立一個controller包,并在controller包下建立一個PaymentHandler類
findAll方法就是jpa插件封裝的查詢方法,該方法會将對應表的所有内容查出來。
3. 在DemoApplication類中,執行main方法編譯并執行
以上springboot項目完成