spring boot 的 JPA 操作

注意的幾點:

spring.datasource.url = jdbc:mysql://localhost:3306/testcharacterEncoding=utf8&;useSSL=true 原因:由於我的MySQL版本較高,如不加此配置,會報如下錯誤: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.

import javax.persistence.Entity;

import javax.persistence.Id;

@Entity

public class Demo {

@Id

private int id;

private String name;

private int age;

public long getId() { return id; }

public void setId(int id) { this.id = id; }

public String getName() { return name; }

public void setName(String name) { this.name = name; }

public int getAge() { return age; }

public void setAge(int age) { this.age = age; }

 

原因:這裏是一個完整的entity類,目的是看上面import的包,在import包的時候,一定使用javax.persistence.*,否則將會出現錯誤,如下: org.hibernate.AnnotationException: No identifier specified for entity這個錯誤看上去是這張表沒有主鍵,在使用hibernate的映射表的時候entity類是必須要主鍵的,否則就會報出這個異常。但是我們已經配置了@ID,還會報這個錯誤是因爲import錯包了,錯誤的使用了org.springframework.data.annotation.Id包。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章