spring-data-jpa初步開始的helloworld

1.在Spring Data的核心接口裏面Repository是最基本的接口了, spring提供了很多實現了該接口的基本接口,如: CrudRepository, PagingAndSortingRepository,SimpleJpaRepository,QueryDslJpaRepository等大量查詢接口 

2.其中CrudRepository實現基本的增刪查改 

Java代碼  收藏代碼
  1. public interface CrudRepository<T, ID extends Serializable>  
  2.     extends Repository<T, ID> {  
  3.                                                                                                                          
  4.     <S extends T> S save(S entity);  
  5.                                                                                                                          
  6.     T findOne(ID primaryKey);  
  7.                                                                                                                          
  8.     Iterable<T> findAll();  
  9.   
  10.     Long count();  
  11.                                                                                                                          
  12.     void delete(T entity);  
  13.                                                                                                                          
  14.     boolean exists(ID primaryKey);  
  15.     .....                                                                                                                   
  16. }  
  17.   
  18. 1.保存該對象  
  19. 2.根據該對象的id查詢該對象  
  20. 3.返回該對象的一個集合  
  21. 4.返回該對象的數量  
  22. 5.刪除該對象  
  23. 6.根據id驗證該對象是否存在  
  24.   
  25. 詳見該接口CrudRepository方法  


3.PagingAndSortingRepository該接口主要用來提供分頁和排序查詢 

Java代碼  收藏代碼
  1. public interface PagingAndSortingRepository<T, ID extends Serializable>   
  2.   extends CrudRepository<T, ID> {  
  3.   
  4.   Iterable<T> findAll(Sort sort);  
  5.   
  6.   Page<T> findAll(Pageable pageable);  
  7. }  
  8.   
  9. 如:  
  10. Page<StudentEntity> users = repository.findAll(new PageRequest(120));  


4.配置spring-boot啓動項目 
Java代碼  收藏代碼
  1. @EnableAutoConfiguration  
  2. @ComponentScan("com.lance")  
  3. @EntityScan("com.lance.entity")  
  4. @EnableJpaRepositories("com.lance.repository")  
  5. public class WebAppConfig {  
  6.       
  7.     public static void main(String[] args) {  
  8.         SpringApplication.run(WebAppConfig.class, args);  
  9.     }  
  10. }  


5.配置數據庫連接以及其他配置項application.properties 
Java代碼  收藏代碼
  1. #DB properties:  
  2. spring.datasource.url=jdbc:mysql://localhost:3306/test  
  3. spring.datasource.username=root  
  4. spring.datasource.password=123456  
  5. spring.datasource.driverClassName=com.mysql.jdbc.Driver  
  6.   
  7. #JPA Configuration:  
  8. spring.jpa.show-sql=true  
  9. #spring.jpa.generate-ddl=true  
  10. spring.jpa.hibernate.ddl-auto=update  
  11. #spring.jpa.database-platform=org.hibernate.dialect.MySQL5Dialect  
  12. #spring.jpa.hibernate.naming_strategy=org.hibernate.cfg.ImprovedNamingStrategy  
  13. #spring.jpa.database=org.hibernate.dialect.MySQL5InnoDBDialect  
  14.   
  15. #view Configuration:  
  16. spring.view.prefix=/WEB-INF/views/  
  17.   
  18. #Server Configuration:  
  19. server.port=8080  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章