首页 > 作文

JPA

更新时间:2023-04-03 22:32:48 阅读: 评论:0

目录
前言jpa的语法分为如下5种:1、count相关,返回值为int 或 long2、exists相关,返回值只能是 boolean3、find相关,返回值是数组list<aaa>4、findfirst相关,返回值是aaa5、delete相关,返回值是int,删除行数

前言

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

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

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

import lombok.data;import javax.persistence.entity;import javax.persistence.id;@entity@datapublic 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;@repositorypublic interface aaarepository extends jparepository<aaa, long> {    int countbydishhourandrestid(int hour, long restid);    boolean existsbydishhourandrestid(int hour, long restid);    list<aaa> 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如下:

lect count(id) from aaa where dishhour=? and restid=?

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

int countdistinctbydishhourandrestid(int hour, long restid);

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

lect distinct count(distinct id) from aaa where dishhour=? and restid=?

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

boolean existsbydishhoura呼吸作用的公式ndrestid(int hour, long restid);boolean existsaaabydishhourandrestid(int hour, long restid);boolean existsaaasbydishhourandrestid(int hour, long restid);boolean existsallbydishhourandrestid(int hour, long restid);

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

lect id from aaa where dishhour=? and restid=? limit 1

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

boolean existsdistinctbydishhourandrestid(int hour, long restid);

对应sql如下,功能跟existsby是一致的,多余:

lect distinct id from aaa wher初任公务员培训心得e dishhour=? and restid=? limit 1

3、find相关,返回值是数组list<aaa>

list<aaa> findbydishhourandrestid(int hour, long restid);list<aaa> findaaabydishhourandrestid(int hour, long restid);list<aaa> findaaasbydishhourandrestid(int hour, long restid);list<aaa> findallbydishhourandrestid(int hour, long restid);list<aaa> getbydishhourandrestid(int hour, long restid);list<aaa> getaaabydishhourandrestid(int hour, long restid);list<aaa> getaaasbydishhourandrestid(int hour, long restid);list<aaa> getallbydishhourandrestid(int hour, long restid);list<aaa> querybydishhourandrestid(int hour, long restid);list<aaa> queryaaabydishhourandrestid(int hour, long restid);list<aaa> queryaaasbydishhourandrestid(int hour, long restid);list<aaa> queryallbydishhourandrestid(int hour, long restid);list<aaa> readbydishhourandrestid(int hour, long restid);list<aaa> readaaabydishhourandrestid(int hour, long restid);list<aaa> readaaasbydishhourandrestid(int hour, long restid);list<aaa> readallbydishhourandrestid(int hour, long restid);list<aaa> streambydishhourandrestid(int hour, long restid);list<aaa> streamaaabyd马嘉丽ishhourandrestid(int hour, long restid);list<aaa> streamaaasbydishhourandrestid(int hour, long restid);list<aaa> streamallbydishhourandrestid(int hour, long restid);

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

lect id,dishhour,num,restid from 暑假见闻作文aaa where dishhour=? and restid=?

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

list<aaa> finddistinctbydishhourandrestid(int hour, long restid);

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

lect 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 streamfirstbydishhourandrestid(int hour, long restid);aaa streamtopbydishhourandrestid(int a, long b);

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

lect id,dishhour,num,restid from aaa where dishhour=? and restid=? limit 1

注:返回值也可以改成list<aaa>,但是sql不变,返回的数组也只有一条数据

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

list<aaa> finddistinctfirstbydishh安心的近义词ourandrestid(int hour, long restid);

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

lect distinct id,dishhour,num,restid from aaa where dishhour=? and restid=? limit 1

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

@transactionalint deleteaaabydishhourandrestid(int a, long b);@transactionalint deleteaaasbydishhourandrestid(int a, long b);@transactionalint deleteallbydishhourandrestid(int a, long b);@transactionalint deletebydishhourandrestid(int a, long b);@transactionalint removeaaabydishhourandrestid(int a, long b);@transactionalint removeaaasbydishhourandrestid(int a, long b);@transactionalint removeallbydishhourandrestid(int a, long b);@transactionalint removebydishhourandrestid(int a, long b);

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

lect id,dishhour,num,restid from aaa where dishhour=? and restid=?delete from aaa where id=?

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

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

int deletedistinctbydishhourandrestid(int hour, long restid);

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

lect distinct id,dishhour,num,restid from aaa where dishhour=? and restid=?

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

注2:jpa query注解问题:

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

@query(value = "lect * from #{#entityname} where 1=2", nativequery = true)aaa lectxxx();

inrt、update、delete这3种dml操作,返回值只能是void、int、long,且必须增加2个注解,例如:

// 返回值不是void、int、long,报错:// modifying queries can only u 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} t num=num+1 where id=6", nativequery = true)int doupdate();

注3:jpa原生方法列表:

list<t> findall();list<t> findall(sort var1);list<t> findallbyid(iterable<id> var1);<s extends t> list<s> saveall(iterable<s> var1);void flush();<s extends t> s saveandflush(s var1);void deleteinbatch(iterable<t> var1);void deleteallinbatch();t getone(id var1);<s extends t> list<s> findall(example<s> var1);<s extends t> list<s> findall(example<s> var1, sort var2);

以上为个人经验,希望能给大家一个参考,也希望大家多多支持www.887551.com。

本文发布于:2023-04-03 22:32:47,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/zuowen/9ef3d083b82496e13144a1537f440a68.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

本文word下载地址:JPA.doc

本文 PDF 下载地址:JPA.pdf

标签:返回值   方法   就好   定义
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图