一、Spring data JPA簡介
Spring data JPA是Spring在ORM框架,以及JPA規(guī)范的基礎(chǔ)上,封裝的一套JPA應(yīng)用框架,并提供了一整套的數(shù)據(jù)訪問層解決方案。
二、Spring data JPA的功能
Spring data JPA的功能非常的強(qiáng)大,這里我們先跳過環(huán)境搭建這一步,來一睹Spring data JPA的“芳容”。
Spring data JPA提供給用戶使用的,主要有以下幾個接口:
Repository:僅僅是一個標(biāo)識,表明任何繼承它的均為倉庫接口類,方便Spring自動掃描識別
CrudRepository:繼承Repository,實現(xiàn)了一組CRUD相關(guān)的方法
PagingAndSortingRepository:繼承CrudRepository,實現(xiàn)了一組分頁排序相關(guān)的方法
JpaRepository:繼承PagingAndSortingRepository,實現(xiàn)一組JPA規(guī)范相關(guān)的方法
JpaSpecificationExecutor:比較特殊,不屬于Repository體系,實現(xiàn)一組JPA Criteria查詢相關(guān)的方法
三、Spring data JPA的接口
1、CrudRepository接口
建立一個Entity類:
@Entity @Table(name="USER") public class User { @Id @GeneratedValue private Integer id; //賬號 private String account; //姓名 private String name; //密碼 private String password; // 郵箱 private String email; }
編寫接口,并繼承CrudRepository接口:
public interface UserRepository extends CrudRepository<User, Integer> { }
編寫測試類
public class UserRepositoryTest { @Autowired private UserRepository dao; @Test//保存 public void testSave(){ User user = new User(); user.setName("chhliu"); user.setAccount("10000"); user.setEmail("[email protected]"); user.setPassword("123456"); dao.save(user); } @Test//批量保存 public void testSave1(){ List<User> users = new ArrayList<User>(); User user = new User(); user.setName("tanjie"); user.setAccount("10000"); user.setEmail("[email protected]"); user.setPassword("123456"); users.add(user); user = new User(); user.setName("esdong"); user.setAccount("10000"); user.setEmail("[email protected]"); user.setPassword("123456"); users.add(user); user = new User(); user.setName("qinhongfei"); user.setAccount("10000"); user.setEmail("[email protected]"); user.setPassword("123456"); users.add(user); user = new User(); user.setName("huizhang"); user.setAccount("10000"); user.setEmail("[email protected]"); user.setPassword("123456"); users.add(user); user = new User(); user.setName("caican"); user.setAccount("10000"); user.setEmail("[email protected]"); user.setPassword("123456"); users.add(user); dao.save(users); } @Test//更新 public void testUpdate(){ User user = dao.findOne(1); user.setPassword("123890");// 要想這樣實現(xiàn)更新的功能,需要在service層加上@Transaction事物注解 } @Test//刪除 public void testDelete(){ dao.delete(2); } @Test//查詢所有 public void testFindAll(){ List<User> users = (List<User>) dao.findAll(); System.out.println(JSON.toJSONString(users)); } @Test//判斷指定的id對象是否存在 public void testIsExist(){ boolean isExist = dao.exists(8); System.out.println(isExist); } @Test//通過id列表來查詢 public void testFindUserByIds(){ List<Integer> listIds = new ArrayList<Integer>(); listIds.add(2); listIds.add(4); listIds.add(7); List<User> users = (List<User>) dao.findAll(listIds); System.out.println(JSON.toJSONString(users)); } }
大家可以看出,到這里,我就只寫了一個接口類,并沒有實現(xiàn)這個接口類,就可以完成基本的CRUD操作。因為這個接口會自動為域?qū)ο髣?chuàng)建增刪改查方法,供業(yè)務(wù)層直接使用。
該接口的定義如下,總共提供了11個方法,基本上可以滿足簡單的CRUD操作以及批量操作:
@NoRepositoryBean public interface CrudRepository<T, ID extends Serializable> extends Repository<T, ID> { <S extends T> S save(S entity);//保存 <S extends T> Iterable<S> save(Iterable<S> entities);//批量保存 T findOne(ID id);//根據(jù)id查詢一個對象 boolean exists(ID id);//判斷對象是否存在 Iterable<T> findAll();//查詢所有的對象 Iterable<T> findAll(Iterable<ID> ids);//根據(jù)id列表查詢所有的對象 long count();//計算對象的總個數(shù) void delete(ID id);//根據(jù)id刪除 void delete(T entity);//刪除對象 void delete(Iterable<? extends T> entities);//批量刪除 void deleteAll();//刪除所有 }