jpa基本语法

详细语法官网去学习 -->>  http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#reference
                                  摘抄2018.02.19版本:  http://www.cnblogs.com/wangdaijun/p/8482073.html

Query creation

Generally the query creation mechanism for JPA works as described in Query methods. Here’s a short example of what a JPA query method translates into:

Example 43. Query creation from method names
public interface UserRepository extends Repository<User, Long> {    List<User> findByEmailAddressAndLastname(String emailAddress, String lastname); }

We will create a query using the JPA criteria API from this but essentially this translates into the following query:select u from User u where u.emailAddress = ?1 and u.lastname = ?2. Spring Data JPA will do a property check and traverse nested properties as described in Property expressions. Here’s an overview of the keywords supported for JPA and what a method containing that keyword essentially translates to.

Table 4. Supported keywords inside method names
KeywordSampleJPQL snippet

And

findByLastnameAndFirstname

… where x.lastname = ?1 and x.firstname = ?2

Or

findByLastnameOrFirstname

… where x.lastname = ?1 or x.firstname = ?2

Is,Equals

findByFirstname,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<Age> ages)

… where x.age in ?1

NotIn

findByAgeNotIn(Collection<Age> 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)

 

 

转载至:https://www.cnblogs.com/wangdaijun/p/5523923.html

 

 
 
 
 
 
 
 
JPA(Java Persistence API)是Java EE 5规范中定义的一套持久化框架,用于将Java对象映射到关系型数据库中,并提供了一系列的API来管理数据的持久化和访问。JPA的使用方式和语法如下: 1. 定义实体类:使用@Entity注解标注Java中的实体类,并使用@Id注解标注实体类中的主键字段。 ```java @Entity public class User { @Id private Long id; private String name; private Integer age; //...省略getter和setter方法 } ``` 2. 定义Repository接口:使用@Repository注解标注Java中的Repository接口,并继承JpaRepository或其它相关的接口。在接口中定义数据库操作方法,方法名遵循JPA的命名规范。 ```java @Repository public interface UserRepository extends JpaRepository<User, Long> { //根据名称查询用户 User findByName(String name); //根据名称和年龄查询用户 User findByNameAndAge(String name, Integer age); //根据名称模糊查询用户 List<User> findByNameLike(String name); } ``` 3. 配置数据源和JPA相关的属性:在application.properties或application.yml文件中配置数据源和JPA相关的属性,如数据库连接信息、JPA实现类等。 ``` spring.datasource.url=jdbc:mysql://localhost:3306/test spring.datasource.username=root spring.datasource.password=root spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.jpa.hibernate.ddl-auto=update spring.jpa.show-sql=true spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect ``` 4. 使用EntityManager进行操作:除了使用Repository接口进行数据库操作外,也可以使用EntityManager进行操作。EntityManager是JPA中的核心API,用于管理实体对象的生命周期和数据的持久化。 ```java @Autowired private EntityManager entityManager; public void save(User user) { entityManager.persist(user); } public User findById(Long id) { return entityManager.find(User.class, id); } public void delete(User user) { entityManager.remove(user); } ``` 以上是JPA的基本使用方式和语法,JPA还提供了一系列的高级特性,如事务管理、关联关系的管理、动态查询等,需要根据具体的需求来选择使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

LuckyTHP

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值