在使用了許久的MyBatis后,了解到了Spring Data JPA,那家伙,這玩意也挺強大,某種程度上比MyBatis還好用,這不,我在使用的時候就發現了一個神奇的地方,我們可以通過自定義的方法名就可以讓JPA自動解析出相應的SQL語句,具體這背后是怎么完成的,我還不咋了解,后續了解了肯定回合大家分享的。廢話不多說,直接看代碼。
- 首先創建一張Customer表,表的具體結構如下:
- 首先創建一個Spring Boot項目(方便),我用的版本是2.1.8.RELEASE。
- 創建一個映射到數據庫的實體類,代碼如下:
package com.hk.springdatajpa.entity; import javax.persistence.*; /** * 客戶的實體類 * @author by 何坤 * @Classname Customer * @Description TODO * @Date 2019/9/23 15:59 */ @Entity @Table(name = "cst_customer") public class Customer { /** * 自增主鍵 */ @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "cust_id", nullable = false, length = 32) private Long id; /** * 客戶的姓名 */ @Column(name = "cust_name", nullable = true, length = 32) private String name; /** * 客戶信息的來源 */ @Column(name = "cust_source", nullable = true, length = 32) private String source; /** * 客戶所屬的行業 */ @Column(name = "cust_industry", nullable = true, length = 32) private String industry; /** * 客戶的級別 */ @Column(name = "cust_level", nullable = true, length = 32) private String level; /** * 客戶的聯系地址 */ @Column(name = "cust_address", nullable = true, length = 128) private String address; /** * 客戶的手機號碼 */ @Column(name = "cust_phone", nullable = true, length = 64) private String phone; 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 String getSource() { return source; } public void setSource(String source) { this.source = source; } public String getIndustry() { return industry; } public void setIndustry(String industry) { this.industry = industry; } public String getLevel() { return level; } public void setLevel(String level) { this.level = level; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } @Override public String toString() { return "Customer{" + "id=" + id + ", name='" + name + '\'' + ", source='" + source + '\'' + ", industry='" + industry + '\'' + ", level='" + level + '\'' + ", address='" + address + '\'' + ", phone='" + phone + '\'' + '}'; } }
- 再創建一個繼承於CrudRepository接口的CustomerRepository接口,聲明表對應的java類以及主鍵的數據類型(java類中的);
package com.hk.springdatajpa.dao; import com.hk.springdatajpa.entity.Customer; import org.springframework.data.repository.CrudRepository; import java.util.List; /** * @author by 何坤 * @Classname CustomerRepository * @Description TODO * @Date 2019/9/24 9:08 */ public interface CustomerRepository extends CrudRepository<Customer, Long> { /** * 根據用戶的地址進行查詢 * @param address * @return java.util.List<com.hk.springdatajpa.entity.Customer> * @date 2019/9/24 16:02 * @author 何坤 */ List<Customer> findByAddress(String address); /** * 將查詢到的所有用戶降序排列 * @param * @return java.util.List<com.hk.springdatajpa.entity.Customer> * @date 2019/9/24 16:05 * @author 何坤 */ List<Customer> findAllByOrderByIdDesc(); /** * 將查詢到的所有用戶升序排列 * @param * @return java.util.List<com.hk.springdatajpa.entity.Customer> * @date 2019/9/24 16:30 * @author 何坤 */ List<Customer> findAllByOrderByIdAsc(); }
- 直接上測試類里面去跑一下。
package com.hk.springdatajpa; import com.hk.springdatajpa.dao.CustomerRepository; import com.hk.springdatajpa.entity.Customer; import com.hk.springdatajpa.service.CustomerService; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import java.util.List; import java.util.Optional; /** * @author by 何坤 * @Classname JpaTest * @Description TODO * @Date 2019/9/24 12:53 */ @SpringBootTest @RunWith(SpringRunner.class) public class JpaTest { @Autowired private CustomerRepository customerRepository; /** * 獲取所有的用戶信息並倒敘排列輸出 * @param * @return void * @date 2019/9/24 16:07 * @author 何坤 */ @Test public void testFindAllByOrderByIdDesc(){ List<Customer> customers = customerRepository.findAllByOrderByIdDesc(); for (Customer customer : customers){ System.out.println(customer); } } }
- 控制台打印了sql信息。
- 卧槽,我一句sql沒寫,咋出來的sql語句啊?這就是JPA的強大了。
- 在JPA中有一個類名解析器,所以將類名按照一定規則進行命名,就可以解析出你想要的SQL語句。具體規則如下(重點啊。。。。。。。。):
- 在JPA中有一個類名解析器,所以將類名按照一定規則進行命名,就可以解析出你想要的SQL語句。具體規則如下(重點啊。。。。。。。。):
- 我們用一個我代碼中的一個例子實際的講解一下。在CustomerRepository接口中的findAllByOrderByIdDesc()方法。
-
- 首先findAll代表查詢全部的信息。
- By:條件查詢,我們這里直接查詢全部,所以沒有條件。
- 再OrderBy,進行排序。
- 排序的條件為Id
- 降序排序Desc
- 所以最后的SQL語句就為:SELECT * FROM cust_customer ORDER BY cust_id DESC
最后, 我的目錄結構如下:
以及相關的配置(application.yml)如下:
server:
port: 8000
---
spring:
thymeleaf:
cache: false
datasource:
username: root
password: Hk123456?
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/jpa?serverTimezone=UTC
jpa:
show-sql: true
hibernate:
ddl-auto: update
---
logging:
level:
com.hk.springdatajpa.mapper: debug