☞ 添加依賴
一個 SpringBoot 項目想要使用 SpringDataJPA 必須先添加 SpringDataJPA 相關依賴,其次,SpringDataJPA 會用到資料庫驅動,所以也需要導入資料庫驅動的依賴
<!-- springBoot JPA的起步依賴 --><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId></dependency><!-- MySQL連接驅動 --><dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId></dependency>1234567891011
☞ 配置資料庫與 JPA 相關內容
2.2.5.RELEASE 以上依賴的 mysql-connect-java 的版本不同。2.2.5 版本的 mysql-connect 的版本超過6.0。針對超過 6.0 的版本。配置 application.properties 的文件,有幾點不同: ♞ 在連接資料庫的 url 需要配置時區 serverTimezone,否則會報 The server time zone value &39; is unrecognized or represents more than one time zone. ♞ spring.datasource.driverClassName 需要使用 com.mysql.cj.jdbc.Driver,繼續使用 com.mysql.jdbc.Driver 會提示 Loading class &39;. This is deprecated. The new driver class is &39;
JPA Configuration:spring.jpa.database=MySQLspring.jpa.show-sql=truespring.jpa.generate-ddl=truespring.jpa.hibernate.ddl-auto=update 命名策略123456789101112
☞ 配置實體類
所有的註解都是使用 JPA 的規範提供的註解,所以在導入註解包的時候,一定要導入 javax.persistence 下的註解。
/** * Created with IntelliJ IDEA. * * @author Demo_Null * @date 2020/7/30 * @description 實體類 */@Entitypublic class Student implements Serializable { private static final long SerialVersionUID = 1L; @Id // 聲明 id 為主鍵 @GeneratedValue(strategy= GenerationType.IDENTITY) // 配置主鍵的生成策略 private Long id; private String Name; private Integer age; private Boolean sex; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return Name; } public void setName(String name) { this.Name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Boolean getSex() { return sex; } public void setSex(Boolean sex) { this.sex = sex; } @Override public String toString() { return &34; + &34; + id + &39;&39;\&39; + &34; + age + &34; + sex + &39;; }}12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
☞ repository
SpringDataJPA 是 Spring 提供的一款對於數據訪問層的框架,使用 SpringDataJPA,只需要按照框架的規範提供 DAO 接口,不需要實現類就可以完成資料庫的增刪改查、分頁查詢等方法的定義,極大的簡化了我們的開發過程。在 SpringDataJPA 中,對於定義符合規範的 DAO 層接口,我們只需要遵循以下幾點就可以了: ♞ 創建一個 DAO 層接口,並繼承 JpaRepository 接口 ♞ 提供相應的泛型
/** * Created with IntelliJ IDEA. * * @author Demo_Null * @date 2020/7/30 * @description student repository 接口 */public interface StudentRepository extends JpaRepository<Student, Long> { public List<Student> findAll();}12345678910
☞ 測試類
/** * Created with IntelliJ IDEA. * * @author gaohu9712@163.com * @date 2020/7/30 * @description 測試類 */// SpringRunner.class 也可以@RunWith(SpringJUnit4ClassRunner.class)// 有些會讓加上 classes=啟動類.class,我加上會報 Failed to load ApplicationContext @SpringBootTest() public class Demo { @Autowired private StudentRepository studentRepository; @Test public void test() { List<Student> list = studentRepository.findAll(); System.out.println(list); }}123456789101112131415161718192021
☞ 運行結果
還是先創建 Springboot 項目,在填寫好項目信息之後選擇需要使用 SpringDataJPA 就會自動幫我們導入相關依賴。剩下的就和上面的一樣了。