springboot 整合jpa一

本文深入探讨了Java Persistence API (JPA)的概念,包括其作为ORM框架的角色,以及如何通过注解或XML实现对象与数据库表的映射。同时,文章详细介绍了JPA与Hibernate的关系,强调了JPA作为规范,而Hibernate则是其实现框架的地位。此外,还讲解了Spring Boot中JPA的使用方法,包括实体关系映射、查询方法及分页功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

jpa 概述

全称Java Persistence API,通过JDK 5.0注解或XML描述对象-关系表的映射关系,并将运行期的实体对象持久化到数据库中
JPA提供的技术:

1)ORM映射元数据:JPA支持XML和JDK 5.0注解两种元数据的形式,元数据描述对象和表之间的映射关系,框架据此将实体对象持久化到数据库表中;

2)JPA 的API:用来操作实体对象,执行CRUD操作,框架在后台替我们完成所有的事情,开发者从繁琐的JDBC和SQL代码中解脱出来。

3)查询语言:通过面向对象而非面向数据库的查询语言查询数据,避免程序的SQL语句紧密耦合。

jpa 和hibernate的关系

JPA是规范,Hibernate是框架,JPA是持久化规范,而Hibernate实现了JPA。

在springboot中的 jpa的使用

##实体对象
实体关系映射(ORM)

对象端数据库端annotion
ClassTable@Entity @Table(name=“tablename”)
propertycolumn@Column(name = “columnname”)
propertyprimary key@Id @GeneratedValue 详见ID生成策略
@Table(name="t_dept")
@Entity
public class Dept {
	@Id
	@GeneratedValue
	int did;
	String dname;

映射关系

JPA定义了
one-to-one、one-to-many、many-to-one、many-to-many 4种关系。

JPA提供的接口

JpaRepository,最顶层的接口,是一个空的接口,目的是为了统一所有Repository的类型
CrudRepository :是Repository的子接口,提供CRUD的功能
PagingAndSortingRepository:是CrudRepository的子接口,添加分页和排序的功能
接口举例

@Repository
public interface JpaDao extends JpaRepository<RiCheng, Integer> {
	Page<RiCheng> findGoodsListBySnameContaining(String name,Pageable page);
}

jap中的默认查询方法

KeywordSampleJPQL snippet
KeywordSampleJPQL snippet
AndfindByLastnameAndFirstname… where x.lastname = ?1 and x.firstname = ?2
OrfindByLastnameOrFirstname… where x.lastname = ?1 or x.firstname = ?2
Is,EqualsfindByFirstname,findByFirstnameIs,findByFirstnameEquals… where x.firstname = ?1
BetweenfindByStartDateBetween… where x.startDate between ?1 and ?2
LessThanfindByAgeLessThan… where x.age < ?1
LessThanEqualfindByAgeLessThanEqual… where x.age ⇐ ?1
GreaterThanfindByAgeGreaterThan… where x.age > ?1
GreaterThanEqualfindByAgeGreaterThanEqual… where x.age >= ?1
AfterfindByStartDateAfter… where x.startDate > ?1
BeforefindByStartDateBefore… where x.startDate < ?1
IsNullfindByAgeIsNull… where x.age is null
IsNotNull,NotNullfindByAge(Is)NotNull… where x.age not null
LikefindByFirstnameLike… where x.firstname like ?1
NotLikefindByFirstnameNotLike… where x.firstname not like ?1
StartingWithfindByFirstnameStartingWith… where x.firstname like ?1(parameter bound with appended %)
EndingWithfindByFirstnameEndingWith… where x.firstname like ?1(parameter bound with prepended %)
Containing1findByFirstnameContaining… where x.firstname like ?1(parameter bound wrapped in%)
OrderByfindByAgeOrderByLastnameDesc… where x.age = ?1 order by x.lastname desc
NotfindByLastnameNot… where x.lastname <> ?1
InfindByAgeIn(Collection ages)… where x.age in ?1
NotInfindByAgeNotIn(Collection age)… where x.age not in ?1
TRUEfindByActiveTrue()… where x.active = true
FALSEfindByActiveFalse()… where x.active = false
IgnoreCasefindByFirstnameIgnoreCase… where UPPER(x.firstame) = UPPER(?1)

jpa的分页

设置分页
PageRequest pageRequest = PageRequest.of(1, 3);

传参:Page findGoodsListBySnameContaining(String name,Pageable page);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值