spring data jpa操作数据库的实现方式
spring data jpa接口:
- 继承JpaRepository
1.1.新建基础接口
1 @NoRepositoryBean 2 public interface BaseReposittory <T,ID extends Serializable> extends JpaRepository<T,ID>{ 3 }
1.2.创建要操作的类型接口,ID为数据库中主键的类型
1 public interface EmployeeRepository extends BaseReposittory<Employee,Long>{}
1.3.查询
1.3.1.按照jpa已有的方法名创建查询方法
表达式 |
例子 |
hql查询语句 |
And |
findByLastnameAndFirstname |
… where x.lastname = ?1 and x.firstname = ?2 |
Or |
findByLastnameOrFirstname |
… where x.lastname = ?1 or x.firstname = ?2 |
Is,Equals |
findByFirstname,findByFirstnameIs,findByFirstnameEqual |
… where x.firstname = 1? |
Between |
findByStartDateBetween |
… where x.startDate between 1? and ?2 |
LessThan(lt) |
findByAgeLessThan |
… where x.age < ?1 |
LessThanEqual(le) |
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) |
1.3.2.@Query注解查询
1 //根据用户名查询(这里写必需在问号后加顺序) 2 @Query("select e from Employee e where e.username = ?1") 3 Employee query01(String name); 4 //根据用户名模糊查询(这里写必需在问号后加顺序) 5 @Query("select e from Employee e where e.username like ?1") 6 List<Employee> query02(String name); 7 //根据用户名与邮件进行模糊查询(这里写必需在问号后加顺序) 8 @Query("select e from Employee e where e.username like ?1 and e.email like ?2") 9 List<Employee> query03(String name, String email);
如果想用原生SQL,在@Query加nativeQuery =true
1 //查询所有 2 @Query(nativeQuery =true,value = "select * from employee where username=?;") 3 Employee query04(String name);
1.3.3.继承JpaSpecificationExecutor
1.3.3.1.创建基础接口
1 @NoRepositoryBean 2 public interface BaseReposittory <T,ID extends Serializable> extends JpaRepository<T,ID>,JpaSpecificationExecutor<T> { 3 }
1.3.3.2.查询
1 /** 2 * 根据相应的规则(Specification) 查询对应数据 3 * Predicate: where username=? and email =? 4 * root : 根 -> 可以获取到类中的属性(username,email) 5 * criteriaQuery: 如select,from,where,group by ,order by 等 6 * criteriaBuilder:解决 username=? / username like ? / age > ? 7 * 多个条件结合 username=? and/or age > ? 8 */ 9 //多条件查询+分页+排序 10 public void testJpaSpecificationExecutor() throws Exception{ 11 //先定义规范 12 Specification spec= new Specification<Employee>() { 13 @Override 14 public Predicate toPredicate(Root<Employee> root, CriteriaQuery<?> cq, CriteriaBuilder cd) { 15 Path usernamePath = root.get("username"); 16 Predicate p1 = cd.like(usernamePath, "%1%"); 17 Path emailPath = root.get("email"); 18 Predicate p2 = cd.like(emailPath, "%2%"); 19 Path agePath = root.get("age"); 20 Predicate p3 = cd.ge(agePath, 18);//le 表示小于 ge表示大于 21 //多个条件连接起来 22 Predicate p = cd.and(p1, p2, p3); 23 return p; 24 } 25 }; 26 //进行排序 27 Sort sort = new Sort(Sort.Direction.ASC,"username"); 28 //进行分页 29 Pageable page=new PageRequest(0,10,sort); 30 //查询数据库 31 Page p = employeeRepository.findAll(spec, page); 32 p.forEach(e->{ 33 System.out.println(e); 34 }); 35 }
1.3.4.jpa-spec插件
1 // 多个条件查询 +分页+排序 2 public void testSpec() throws Exception{ 3 Specification<Employee> spec = Specifications.<Employee>and() 4 .like("username","%1%") 5 .like("email","%2%") 6 .ge("age", 20) 7 .build(); 8 Sort sort = new Sort(Sort.Direction.DESC,"age");//根据年龄排序 9 Pageable page = new PageRequest(0,10,sort); 10 Page<Employee> list = employeeRepository.findAll(spec, page); 11 list.forEach(e->{ 12 System.out.println(e); 13 }); 14 }