Spring Data JPA 是 Spring 基于 ORM 框架、JPA 规范的基础上封装的一套JPA应用框架,可使开发者用极简的代码即可实现对数据库的访问和操作
JPA中的主键生成策略
JPA提供的四种标准用法为TABLE,SEQUENCE,IDENTITY,AUTO。
IDENTITY:主键由数据库自动生成(主要是自动增长型)
用法:
|
@Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long custId; |
SEQUENCE:根据底层数据库的序列来生成主键,条件是数据库支持序列。
用法:
|
@Id @GeneratedValue(strategy = GenerationType.SEQUENCE,generator="payablemoney_seq") @SequenceGenerator(name="payablemoney_seq", sequenceName="seq_payment") private Long custId; |
AUTO:主键由程序控制
用法:
|
@Id @GeneratedValue(strategy = GenerationType.AUTO) private Long custId; |
TABLE:使用一个特定的数据库表格来保存主键
用法:
|
@Id @GeneratedValue(strategy = GenerationType.TABLE, generator="payablemoney_gen") @TableGenerator(name = "pk_gen", table="tb_generator", pkColumnName="gen_name", valueColumnName="gen_value", pkColumnValue="PAYABLEMOENY_PK", allocationSize=1 ) private Long custId; |
使用SQL语句查询
Spring Data JPA同样也支持sql语句的查询,如下:
|
/** * nativeQuery : 使用本地sql的方式查询 */ @Query(value="select * from cst_customer",nativeQuery=true) public void findSql(); |
方法命名规则查询
按照Spring Data JPA 定义的规则,查询方法以findBy开头,涉及条件查询时,条件的属性用条件关键字连接,要注意的是:条件属性首字母需大写。框架在进行方法名解析时,会先把方法名多余的前缀截取掉,然后对剩下部分进行解析。
|
//方法命名方式查询(根据客户名称查询客户) public Customer findByCustName(String custName); |
具体的关键字,使用方法和生产成SQL如下表所示
|
|
|
|
|
|
|
Keyword |
Sample |
JPQL |
|
|
|
And |
findByLastnameAndFirstname |
… where x.lastname = ?1 and x.firstname = ?2 |
|
|
|
Or |
findByLastnameOrFirstname |
… where x.lastname = ?1 or x.firstname = ?2 |
|
|
|
Is,Equals |
findByFirstnameIs, findByFirstnameEquals |
… where x.firstname = ?1 |
|
|
|
Between |
findByStartDateBetween |
… where x.startDate between ?1 and ?2 |
|
|
|
LessThan |
findByAgeLessThan |
… where x.age < ?1 |
|
|
|
LessThanEqual |
findByAgeLessThanEqual |
… where x.age ⇐ ?1 |
|
|
|
GreaterThan |
findByAgeGreaterThan |
… where x.age > ?1 |
|
|
|
GreaterThanEqual |
findByAgeGreaterThanEqual |
… where x.age >= ?1 |
|
|
|
After |
findByStartDateAfter |
… where x.startDate > ?1 |
|
|
|
Before |
findByStartDateBefore |
… where x.startDate < ?1 |
|
|
|
IsNull |
findByAgeIsNull |
… where x.age is null |
|
|
|
IsNotNull,NotNull |
findByAge(Is)NotNull |
… where x.age not null |
|
|
|
Like |
findByFirstnameLike |
… where x.firstname like ?1 |
|
|
|
NotLike |
findByFirstnameNotLike |
… where x.firstname not like ?1 |
|
|
|
StartingWith |
findByFirstnameStartingWith |
… where x.firstname like ?1 (parameter bound with appended %) |
|
|
|
EndingWith |
findByFirstnameEndingWith |
… where x.firstname like ?1 (parameter bound with prepended %) |
|
|
|
Containing |
findByFirstnameContaining |
… where x.firstname like ?1 (parameter bound wrapped in %) |
|
|
|
OrderBy |
findByAgeOrderByLastnameDesc |
… where x.age = ?1 order by x.lastname desc |
|
|
|
Not |
findByLastnameNot |
… where x.lastname <> ?1 |
|
|
|
In |
findByAgeIn(Collection ages) |
… where x.age in ?1 |
|
|
|
NotIn |
findByAgeNotIn(Collection age) |
… where x.age not in ?1 |
|
|
|
TRUE |
findByActiveTrue() |
… where x.active = true |
|
|
|
FALSE |
findByActiveFalse() |
… where x.active = false |
|
|
|
IgnoreCase |
findByFirstnameIgnoreCase |
… where UPPER(x.firstame) = UPPER(?1) |
|
|
本文详细介绍了SpringDataJPA中四种主键生成策略:IDENTITY、SEQUENCE、AUTO和TABLE,并给出了示例代码。同时,文章探讨了如何使用SQL查询以及SpringDataJPA的方法命名规则进行条件查询,展示了如And、Or、Like等关键字的使用方法,帮助开发者更高效地操作数据库。
7040

被折叠的 条评论
为什么被折叠?



