Spring Data JPA

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)

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值