springdatajpa命名规则_关于JPA中的方法名命名规则

在使用了许久的MyBatis后,了解到了Spring Data JPA,那家伙,这玩意也挺强大,某种程度上比MyBatis还好用,这不,我在使用的时候就发现了一个神奇的地方,我们可以通过自定义的方法名就可以让JPA自动解析出相应的SQL语句,具体这背后是怎么完成的,我还不咋了解,后续了解了肯定回合大家分享的。废话不多说,直接看代码。

首先创建一张Customer表,表的具体结构如下:

5e15e2ec5372178f1278973badc7122e.png

首先创建一个Spring Boot项目(方便),我用的版本是2.1.8.RELEASE。

创建一个映射到数据库的实体类,代码如下:

packagecom.hk.springdatajpa.entity;import javax.persistence.*;/*** 客户的实体类

*@authorby 何坤

* @Classname Customer

* @Description TODO

* @Date 2019/9/23 15:59*/@Entity

@Table(name= "cst_customer")public classCustomer {/*** 自增主键*/@Id

@GeneratedValue(strategy=GenerationType.IDENTITY)

@Column(name= "cust_id", nullable = false, length = 32)privateLong id;/*** 客户的姓名*/@Column(name= "cust_name", nullable = true, length = 32)privateString name;/*** 客户信息的来源*/@Column(name= "cust_source", nullable = true, length = 32)privateString source;/*** 客户所属的行业*/@Column(name= "cust_industry", nullable = true, length = 32)privateString industry;/*** 客户的级别*/@Column(name= "cust_level", nullable = true, length = 32)privateString level;/*** 客户的联系地址*/@Column(name= "cust_address", nullable = true, length = 128)privateString address;/*** 客户的手机号码*/@Column(name= "cust_phone", nullable = true, length = 64)privateString phone;publicLong getId() {returnid;

}public voidsetId(Long id) {this.id =id;

}publicString getName() {returnname;

}public voidsetName(String name) {this.name =name;

}publicString getSource() {returnsource;

}public voidsetSource(String source) {this.source =source;

}publicString getIndustry() {returnindustry;

}public voidsetIndustry(String industry) {this.industry =industry;

}publicString getLevel() {returnlevel;

}public voidsetLevel(String level) {this.level =level;

}publicString getAddress() {returnaddress;

}public voidsetAddress(String address) {this.address =address;

}publicString getPhone() {returnphone;

}public voidsetPhone(String phone) {this.phone =phone;

}

@OverridepublicString toString() {return "Customer{" +

"id=" + id +

", name='" + name + '\'' +

", source='" + source + '\'' +

", industry='" + industry + '\'' +

", level='" + level + '\'' +

", address='" + address + '\'' +

", phone='" + phone + '\'' +

'}';

}

}

再创建一个继承于CrudRepository接口的CustomerRepository接口,声明表对应的java类以及主键的数据类型(java类中的);

packagecom.hk.springdatajpa.dao;importcom.hk.springdatajpa.entity.Customer;importorg.springframework.data.repository.CrudRepository;importjava.util.List;/***@authorby 何坤

* @Classname CustomerRepository

* @Description TODO

* @Date 2019/9/24 9:08*/

public interface CustomerRepository extends CrudRepository{/*** 根据用户的地址进行查询

*@paramaddress

*@returnjava.util.List

* @date 2019/9/24 16:02

*@author何坤*/ListfindByAddress(String address);/*** 将查询到的所有用户降序排列

*@param*@returnjava.util.List

* @date 2019/9/24 16:05

*@author何坤*/ListfindAllByOrderByIdDesc();/*** 将查询到的所有用户升序排列

*@param*@returnjava.util.List

* @date 2019/9/24 16:30

*@author何坤*/ListfindAllByOrderByIdAsc();

}

直接上测试类里面去跑一下。

packagecom.hk.springdatajpa;importcom.hk.springdatajpa.dao.CustomerRepository;importcom.hk.springdatajpa.entity.Customer;importcom.hk.springdatajpa.service.CustomerService;importorg.junit.Test;importorg.junit.runner.RunWith;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.boot.test.context.SpringBootTest;importorg.springframework.test.context.junit4.SpringRunner;importjava.util.List;importjava.util.Optional;/***@authorby 何坤

* @Classname JpaTest

* @Description TODO

* @Date 2019/9/24 12:53*/@SpringBootTest

@RunWith(SpringRunner.class)public classJpaTest {

@AutowiredprivateCustomerRepository customerRepository;/*** 获取所有的用户信息并倒叙排列输出

*@param*@returnvoid

* @date 2019/9/24 16:07

*@author何坤*/@Testpublic voidtestFindAllByOrderByIdDesc(){

List customers =customerRepository.findAllByOrderByIdDesc();for(Customer customer : customers){

System.out.println(customer);

}

}

}

控制台打印了sql信息。

6efd9bdd72ffafb27f4e4b2f92154f85.png

卧槽,我一句sql没写,咋出来的sql语句啊?这就是JPA的强大了。

在JPA中有一个类名解析器,所以将类名按照一定规则进行命名,就可以解析出你想要的SQL语句。具体规则如下(重点啊。。。。。。。。):

77e41df7770f94772ec753368db9c104.png

我们用一个我代码中的一个例子实际的讲解一下。在CustomerRepository接口中的findAllByOrderByIdDesc()方法。

首先findAll代表查询全部的信息。

By:条件查询,我们这里直接查询全部,所以没有条件。

再OrderBy,进行排序。

排序的条件为Id

降序排序Desc

所以最后的SQL语句就为:SELECT * FROM cust_customer ORDER BY cust_id DESC

最后, 我的目录结构如下:

88511532fe5ea89e1e71a775eebc0722.png

以及相关的配置(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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值