SpringBoot+Jpa+MySql 經常使用配置及請求

這篇文章,介紹SpringBoot經常使用的配置和請求處理。大體分紅三個部分介紹:經常使用的請求方式、Jpa配置、MySql配置。java


經常使用的請求方式

以下是經常使用的幾種請求方式: get請求:通常用於查詢數據,獲取一些非重要性的信息。 post請求:通常用於插入數據。 put請求:通常用於數據更新。 delete請求:通常用於數據刪除。mysql

那麼,在SpringBoot中,怎麼對這些請求方式進行處理。(這裏以最經常使用的get、post請求爲例,其餘的相似)spring

get請求:sql

//get請求,獲取url路徑上的參數 @PathVariable
//注:localhost:8080/test/11/hans
@RequestMapping(value = "/test/{id}/{name}", method = RequestMethod.GET)
public String sayHello(@PathVariable("id") Integer id, @PathVariable("name") String name) {
    return "id:" + id + " name:" + name;
}

//get請求,獲取url請求參數的值 @RequestParam
//localhost:8080/test?id=99
@RequestMapping(value = "/test", method = RequestMethod.GET)
public String sayHello(@RequestParam Integer id) {
    return "id:" + id;
}

//get請求,獲取url請求參數的值,增長參數映射,默認值 @RequestParam
//required=false 表示url中能夠無id參數,此時就使用默認參數
@RequestMapping(value = "/test2", method = RequestMethod.GET)
public String sayHello2(@RequestParam(value = "id", required = false, defaultValue = "1") Integer id) {
    return "id:" + id;
}
複製代碼

post請求:數據庫

//post請求
//表單參數
@RequestMapping(value= "/getMessage", method = RequestMethod.POST)
public String getMessage(int code, String message)  {
    return "success";
}

//post請求
//json raw參數
@PostMapping(value= "/getMessageBody")
public String getMessagePost(@RequestBody HolidayEntity bean)  {
    return "success";
}

//匹配參數
//password若是匹配對,@RequestParam不寫都ok
public void login(@RequestParam("account") String name, @RequestParam String password) {
    System.out.println(name + ":" + password);
}

//@RequestHeader註解用來將請求頭的內容綁定到方法參數上。
@PostMapping(value = "login")
public void login2(@RequestHeader("access_token") String accessToken,@RequestParam String name) {
    System.out.println("accessToken:" + accessToken);
}
複製代碼

補充:json

組合註解(RequestMapping的變形)
@GetMapping = @RequestMapping(method = RequestMethod.GET)
@PostMapping = @RequestMapping(method = RequestMethod.POST)
@PutMapping = @RequestMapping(method = RequestMethod.PUT)
@DeleteMapping = @RequestMapping(method = RequestMethod.DELETE)
複製代碼

Jpa配置

1.pom文件加入Jpa配置tomcat

<!--jpa-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
複製代碼

2.Application入口類增長@EnableJpaRepositories註解bash

@EnableJpaRepositories
public class Application extends SpringBootServletInitializer {
複製代碼

3.dao接口app

@Repository
public interface HolidayRepository extends JpaRepository<HolidayEntity, Long> {

    @Query(value = "SELECT p FROM HolidayEntity p")
    List<HolidayEntity> queryHoliday();

}
複製代碼

4.entity類ide

@Entity
@Table(name = "holiday_scheme")
@EntityListeners(AuditingEntityListener.class)
public class HolidayEntity extends AbstractPersistable<Long> {
    @Column(name = "date")
    public String date;
    @Column(name = "hour")
    public String hour;
    @Column(name = "holiday")
    public String holiday;
    @Column(name = "holiday_explain")
    public String holiday_explain;
    @Column(name = "type")
    public String type;//SUB假期,ADD調休

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public String getHour() {
        return hour;
    }

    public void setHour(String hour) {
        this.hour = hour;
    }

    public String getHoliday() {
        return holiday;
    }

    public void setHoliday(String holiday) {
        this.holiday = holiday;
    }

    public String getHoliday_explain() {
        return holiday_explain;
    }

    public void setHoliday_explain(String holiday_explain) {
        this.holiday_explain = holiday_explain;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    @Override
    public String toString() {
        return "HolidayEntity{" +
                "date='" + date + '\'' + ", hour='" + hour + '\'' + ", holiday='" + holiday + '\'' +
                ", holiday_explain='" + holiday_explain + '\'' + ", type='" + type + '\'' + '}'; } } 複製代碼

5.執行,獲取數據

@Autowired
private HolidayRepository holidayRepository;

@RequestMapping("/test")
@ResponseBody
public List<HolidayEntity> test() {
    return holidayRepository.findAll();
}
複製代碼

MySql配置

1.pom文件加入MySql配置

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.46</version>
</dependency>
複製代碼

2.application.properties文件配置MySql相關

# tomcat配置
server.port=8090

# 數據庫配置
#Mysql屬性配置文件,Spring-boot系統配置
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://*.*.*.*:3306/db?autoReconnect=true&amp;useUnicode=true&amp;characterEncoding=UTF-8&amp;useSSL=false&amp;useLegacyDatetimeCode=false&amp;serverTimezone=Asia/Shanghai
spring.datasource.username=***
spring.datasource.password=***

#配置自動建表:updata:沒有表新建,有表更新操做,控制檯顯示建表語句
#spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
#以下的配置會致使報錯 Unable to build Hibernate SessionFactory
#spring.jpa.properties.hibernate.hbm2ddl.auto=validate複製代碼
相關文章
相關標籤/搜索