spring-data-jpa使用总结
2020-06-19 本文已影响0人
A徐小帅
官方指引教程https://spring.io/guides/gs/accessing-data-jpa/
官方英文文档https://docs.spring.io/spring-data/jpa/docs/current/
官方使用示例https://github.com/spring-projects/spring-data-examples/tree/master/jpa
https://github.com/xkcoding/spring-boot-demo
注解
以下注解是Hibernate的
@Entity // 标志着当前类为一个实体 bean,必须含有一个没有参数的构造函数。
@Table // 明确表的详细信息保证实体在数据库中持续存在。
@Id // 标识当前字段为表的主键
@GeneratedValue // 默认情况下,@Id 注解将自动确定最合适的主键生成策略,通过使用 @GeneratedValue 注释来覆盖掉
@Column
/**
@Column 注解用于指定某一列与某一个字段或是属性映射的细节信息。可以使用下列注释的最常用的属性:
name 属性允许显式地指定列的名称。
length 属性为用于映射一个值,特别为一个字符串值的列的大小。
nullable 属性允许当生成模式时,一个列可以被标记为非空。
unique 属性允许列中只能含有唯一的内容
**/
自定义简单查询
根据方法名中的关键字与字段自动生成SQL,如方法名findByName
中findBy
就为关键字,name
为对应表中的字段。
User findByUserName(String userName);
User findByUserNameOrEmail(String username, String email);
Long deleteById(Long id);
Long countByUserName(String userName);
List<User> findByEmailLike(String email);
User findByUserNameIgnoreCase(String userName);
List<User> findByUserNameOrderByEmailDesc(String email);
具体生成的方法的SQL列表:
关键字 | 例子 | SQL片段 |
---|---|---|
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 (x%) |
EndingWith | findByFirstnameEndingWith | … where x.firstname like ?1 (%x) |
Containing | findByFirstnameContaining | … where x.firstname like ?1 (%x%) |
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) |
分页
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
Integer page = 0; // 从第0页开始
Integer size = 10;
Pageable pageable = PageRequest.of(page, size, Sort.by("age").descending());
return equipmentRepository.findAll(pageable);