JPA

网友投稿 637 2022-11-20

JPA

JPA

目录前言JPA的语法分为如下5种:1、count相关,返回值为int 或 long2、exists相关,返回值只能是 boolean3、find相关,返回值是数组List4、findFirst相关,返回值是aaa5、delete相关,返回值是int,删除行数

前言

梳理了一遍JPA的方法命名语法,记录一下,以便后续备查。

注:本文不介绍JPL语法,版本为spring-data-jpa-2.3.0.RELEASE。

假设实体类名为 aaa,且定义如下:

import lombok.Data;

import javax.persistence.Entity;

import javax.persistence.Id;

@Entity

@Data

public class aaa {

@Id

private long id;

private long restId;

private int dishHour;

private int num;

}

对应的仓储层接口定义:

import org.springframework.data.jpa.repository.JpaRepository;

import org.springframework.stereotype.Repository;

import javax.transaction.Transactional;

import java.util.List;

@Repository

public interface aaaRepository extends JpaRepository {

int countByDishHourAndRestId(int hour, long restId);

boolean existsByDishHourAndRestId(int hour, long restId);

List findByDishHourAndRestId(int hour, long restId);

aaa findTopByDishHourAndRestId(int hour, long restId);

@Transactional

int deleteByDishHourAndRestId(int hour, long restId);

}

JPA的语法分为如下5种:

1、count相关,返回值为int 或 long

int countByDishHourAndRestId(int hour, long restId);

int countaaaByDishHourAndRestId(int hour, long restId);

int countaaasByDishHourAndRestId(int hour, long restId);

int countAllByDishHourAndRestId(int hour, long restId);

上面这4个方法是一样的,对应的SQL如下:

select count(id) from aaa where dishHour=? and restId=?

下面这种定义,没有意义,知晓一下就好:

int countDistinctByDishHourAndRestId(int hour, long restId);

对应SQL如下,如果表中有主键,功能跟countBy是一致的,浪费性能:

select distinct count(distinct id) from aaa where dishHour=? and restId=?

2、exists相关,返回值只能是 boolean

boolean existsByDishHourAndRestId(int hour, long restId);

boolean existsaaaByDishHourAndRestId(int hour, long restId);

boolean existsaaasByDishHourAndRestId(int hour, long restId);

boolean existsAllByDishHourAndRestId(int hour, long restId);

上面这4个方法是一样的,对应的SQL如下:

select id from aaa where dishHour=? and restId=? limit 1

下面这种定义,没有意义,知晓一下就好:

boolean existsDistinctByDishHourAndRestId(int hour, long restId);

对应SQL如下,功能跟existsBy是一致的,多余:

select distinct id from aaa where dishHour=? and restId=? limit 1

3、find相关,返回值是数组List

List findByDishHourAndRestId(int hour, long restId);

List findaaaByDishHourAndRestId(int hour, long restId);

List findaaasByDishHourAndRestId(int hour, long restId);

List findAllByDishHourAndRestId(int hour, long restId);

List getByDishHourAndRestId(int hour, long restId);

List getaaaByDishHourAndRestId(int hour, long restId);

List getaaasByDishHourAndRestId(int hour, long restId);

List getAllByDishHourAndRestId(int hour, long restId);

List queryByDishHourAndRestId(int hour, long restId);

List queryaaaByDishHourAndRestId(int hour, long restId);

List queryaaasByDishHourAndRestId(int hour, long restId);

List queryAllByDishHourAndRestId(int hour, long restId);

List readByDishHourAndRestId(int hour, long restId);

List readaaaByDishHourAndRestId(int hour, long restId);

List readaaasByDishHourAndRestId(int hour, long restId);

List readAllByDishHourAndRestId(int hour, long restId);

List streamByDishHourAndRestId(int hour, long restId);

List streamaaaByDishHourAndRestId(int hour, long restId);

List streamaaasByDishHourAndRestId(int hour, long restId);

List streamAllByDishHourAndRestId(int hour, long restId);

上面这20个方法是一样的,对应的SQL如下:

select id,dishHour,num,restId from aaa where dishHour=? and restId=?

下面这种定义,没有意义,知晓一下就好:

List findDistinctByDishHourAndRestId(int hour, long restId);

对应SQL如下,如果表中有主键,功能跟findBy是一致的,多余:

select distinct id,dishHour,num,restId from aaa where dishHour=? and restId=?

4、findFirst相关,返回值是aaa

aaa findFirstByDishHourAndRestId(int hour, long restId);

aaa findTopByDishHourAndRestId(int a, long b);

aaa getFirstByDishHourAndRestId(int hour, long restId);

aaa getTopByDishHourAndRestId(int a, long b);

aaa queryFirstByDishHourAndRestId(int hour, long restId);

aaa queryTopByDishHourAndRestId(int a, long b);

aaa readFirstByDishHourAndRestId(int hour, long restId);

aaa readTopByDishHourAndRestId(int a, long b);

aaa streamFirstByDishHohttp://urAndRestId(int hour, long restId);

aaa streamTopByDishHourAndRestId(int a, long b);

上面这10个方法是一样的,对应的SQL如下:

select id,dishHour,num,restId from aaa where dishHour=? and restId=? limit 1

注:返回值也可以改成List,但是SQL不变,返回的数组也只有一条数据

下面这种定义,没有意义,知晓一下就好:

List findDistinctFirstByDishHourAndRestId(int hour, long restId);

对应SQL如下,如果表中有主键,功能跟countBy是一致的,多余:

select distinct id,dishHour,num,restId from aaa where dishHour=? and restId=? limit 1

5、delete相关,返回值是int,删除行数

@Transactional

int deleteaaaByDishHourAndRestId(int a, long b);

@Transactional

int deleteaaasByDishHourAndRestId(int a, long b);

@Transactional

int deleteAllByDishHourAndRestId(int a, long b);

@Transactional

int deleteByDishHourAndRestId(int a, long b);

@Transactional

int removeaaaByDishHourAndRestId(int a, long b);

@Transactional

int removeaaasByDishHourAndRestId(int a, long b);

@Transactional

int removeAllByDishHourAndRestId(int a, long b);

@Transactional

int removeByDishHourAndRestId(int a, long b);

上面这8个方法是一样的,对应有2条SQL,如下:

select id,dishHour,num,restId from aaa where dishHour=? and restId=?

delete from aaa where id=?

注:先SELECT查找主键,再进行删除,所以必须在方法前加注解Transactional,提供事务,否则会抛异常。

下面这种定义,没有意义,知晓一下就好:

int deleteDistinctByDishHourAndRestId(int hour, long restId);

对应SQL如下,如果表中有主键,功能跟deleteBy是一致的,多余:

select distinct id,dishHour,num,restId from aaa where dishHour=? and restId=?

注1:方法By后面的语法,可以参考下图,或官方文档:

注2:JPA Query注解问题:

SQL里可以用 #{#entityName} 占位符,替代手写表名,如:

@Query(value = "select * from #{#entityName} where 1=2", nativeQuery = true)

aaa selectXXX();

INSERT、UPDATE、DELETE这3种DML操作,返回值只能是void、int、long,且必须增加2个注解,例如:

// 返回值不是void、int、long,报错:

// Modifying queries can only use void or int/Integer as return type!

// 不加 Transactional 报错:

// javax.persistence.TransactionRequiredException: Executing an update/delete query

@Transactional

// 不加Modifing 报错:

// Can not issue data manipulation statements with executeQuery().

@Modifying

@Query(value = "update #{#entityName} set num=num+1 where id=6", nativeQuery = true)

int doupdate();

注3:JPA原生方法列表:

List findAll();

List findAll(Sort var1);

List findAllById(Iterable var1);

List saveAll(Iterable var1);

void flush();

S saveAndFlush(S var1);

void deleteInBatch(Iterable var1);

void deleteAllInBatch();

T getOne(ID var1);

List findAll(Example var1);

List findAll(Example var1, Sort var2);

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:CSP-S 模拟 19/10/09
下一篇:CSP-S 复习 ---- 高精度全家桶
相关文章

 发表评论

暂时没有评论,来抢沙发吧~