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  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章