Spring Data Jpa接口简介
Repository接口
public interface Repository<T, ID> {....}
提供了按方法名称的查询方式;
提供了@Query的查询方式
可能遇到的错误:
1)Caused by: javax.persistence.TransactionRequiredException: Executing an update/delete query(手动加@Transactional解决)
2)如果test方法里加事务注解数据不更新的话,还需要手动加@Rollback(false)解决,自己测试的时候,只加了一个@Transactional就更新成功了.
CrudRepository接口
public interface CrudRepository<T, ID> extends Repository<T, ID> {....}
public void test1 () { JpaUser jpaUser = new JpaUser("名称1","地址1",10,20,2); Optional<JpaUser> optionalT = userRespotory.findById(1) ; Iterable<JpaUser> list = userRespotory.findAll(); userRespotory.delete(jpaUser); userRespotory.save(jpaUser);//保存or更新
PagingAndSortingRepository接口
public interface PagingAndSortingRepository<T, ID> extends CrudRepository<T, ID> { Iterable<T> findAll(Sort var1); Page<T> findAll(Pageable var1); }
//测试sort
public void test1 () { JpaUser jpaUser = new JpaUser("名称1","地址1",10,20,100); Sort sort = new Sort(Sort.Direction.DESC,"id"); Iterable<JpaUser> list = userRespotory.findAll(sort);
//测试page
public void test1 () {
JpaUser jpaUser = new JpaUser("名称1","地址1",10,20,100);
Pageable pageable = PageRequest.of(0, 2);
Page<JpaUser> page = userRespotory.findAll(pageable);
System.out.println(page.getTotalElements());//总条数
System.out.println(page.getTotalPages());//总页数
System.out.println(page.getContent());//当前页集合
//测试分页+排序
public void test1 () {
JpaUser jpaUser = new JpaUser("名称1","地址1",10,20,100);
Pageable pageable = PageRequest.of(1, 2,Sort.Direction.DESC,"id","age1");
Page<JpaUser> page = userRespotory.findAll(pageable);
System.out.println(page.getTotalElements());//总条数
System.out.println(page.getTotalPages());//总页数
System.out.println(page.getContent());//当前页集合
JpaRepository接口
public interface JpaRepository<T, ID> extends PagingAndSortingRepository<T, ID>, QueryByExampleExecutor<T> {....}
JpaSpecificationExecutor接口
public interface JpaSpecificationExecutor<T> { Optional<T> findOne(@Nullable Specification<T> var1); List<T> findAll(@Nullable Specification<T> var1); Page<T> findAll(@Nullable Specification<T> var1, Pageable var2); List<T> findAll(@Nullable Specification<T> var1, Sort var2); long count(@Nullable Specification<T> var1); }
可多条件查询,分页和排序,和以上接口联合使用。
public interface UserSpecificationExcutor extends JpaRepository<JpaUser,Integer> , JpaSpecificationExecutor<JpaUser> {....}
@Autowired UserSpecificationExcutor userSpecificationExcutor; @Test public void test1 () { Specification<JpaUser> specification = new Specification<JpaUser>() { @Override public Predicate toPredicate(Root<JpaUser> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) { Predicate predicate = criteriaBuilder.equal(root.get("name"), "名称1"); List<Predicate> list = new ArrayList<>(); list.add(predicate); predicate = criteriaBuilder.equal(root.get("age1"), 20); list.add(predicate); return criteriaBuilder.and(list.toArray(new Predicate[list.size()])); } }; List<JpaUser> list = userSpecificationExcutor.findAll(specification); System.out.println(list);
一对对关联关系role-user
@Test public void test1 () { JpaUser jpaUser = new JpaUser("jpaName","diz地址" ,200 , 301); jpaUser.setId(199); UserRole userRole = new UserRole(); userRole.setName("role名字"); userRole.setName1("role名字"); userRole.setName2("role名字"); userRole.getUserSet().add(jpaUser); jpaUser.setUserRole(userRole); userSpecificationExcutor.save(jpaUser);
package com.example.zs.springDataJpa; import javax.persistence.*; import java.util.HashSet; import java.util.Set; @Entity @Table(name="t_role") public class UserRole { @Id( ) @GeneratedValue(strategy=GenerationType.IDENTITY) @Column(name="id") private Integer id ; @Column(name="name") private String name; @Column(name="name1") private String name1; @Column(name="name2") private String name2; @OneToMany(mappedBy="userRole") private Set<JpaUser> userSet = new HashSet<>(); public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Set<JpaUser> getUserSet() { return userSet; } public void setUserSet(Set<JpaUser> userSet) { this.userSet = userSet; } public String getName1() { return name1; } public void setName1(String name1) { this.name1 = name1; } public String getName2() { return name2; } public void setName2(String name2) { this.name2 = name2; } }
package com.example.zs.springDataJpa; import javax.persistence.*; @Entity @Table(name="t_user") public class JpaUser { @Column(name="name") private String name ; @Column(name="address") private String address ; @Column(name="id") @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer id ; @Column(name="age1") private Integer age ; @Column(name="age11") private Integer age1 ; @ManyToOne(cascade = CascadeType.PERSIST) @JoinColumn(name="roleFK") private UserRole userRole; public JpaUser(String name, String address, Integer age, Integer age1 ) { this.name = name; this.address = address; this.age = age; this.age1 = age1; // this.id = id; } public JpaUser() { } @Override public String toString() { return "JpaUser{" + "name='" + name + '\'' + ", address='" + address + '\'' + ", id=" + id + ", age=" + age + ", age1=" + age1 + '}'; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Integer getAge1() { return age1; } public void setAge1(Integer age1) { this.age1 = age1; } public UserRole getUserRole() { return userRole; } public void setUserRole(UserRole userRole) { this.userRole = userRole; } }
public interface UserSpecificationExcutor extends JpaRepository<JpaUser,Integer> , JpaSpecificationExecutor<JpaUser> { }