Spring Data开发手册|手把手教你简化持久层开发工作

运维 数据库运维
Spring Data,是为数据访问提供熟悉且一致的基于Spring的编程模型,同时仍然保留底层数据存储的特殊特性。

[[354211]]

 Spring Data,是为数据访问提供熟悉且一致的基于Spring的编程模型,同时仍然保留底层数据存储的特殊特性。

它是对于数据访问技术,关系数据库和非关系数据库,map-reduce框架和基于云的数据服务变得容易。Spring Data是一个总括项目,其中包含很多特定于数据库相关的子项目。


首先,先带大家看一下本篇文章的大致介绍。

没目录怎么知道这篇到底有多少干货呢?

  • Spring Data是什么
  • Spring Data能干什么
  • Spring Data的第一个HelloWorld程序
  • 通过名字来确定方法
  • 通过注解的形式来实现查询
  • 写本地的SQL查询
  • 增删改的玩法
  • 使用框架中提供的增删改查的方法
  • 分页和排序
  • JpaRepository的使用

是不是很清晰呢,现在开始进入正文,一个一个来:

Spring Data是什么

我们传统的开发中,我们的整个DAO层的代码上都是相对来说,都是比较复杂的,在这种情况下,Spring团队就考虑到一个问题,能不能开发一个框架,这个框架能够最大限度的减少DAO层的开发呢?

Spring Data就是为了简化DAO层操作的一个框架

传统的增删改查在我们的Spring Data中已经实现了,也就是说大部分的DAO层操作部分不用写了,仅仅只是需要编写复杂的业务的调用就可以啦


写的这部分的代码,是需要写接口的声明就可以啦,不用写实现,这个实现是自动实现的

Spring Data能干什么

主要用途:

  • 传统的增删改查
  • 排序
  • 分页
  • 排序后分页

即使你需要写DAO,也只是写声明就可以啦,不用写实现

Spring Data的第一个HelloWorld程序(JPA、Hibernate、Spring、SpringMVC、Spring Data)

导包


编写配置文件

  1.  <?xml version="1.0" encoding="UTF-8"?> 
  2. <beans xmlns="http://www.springframework.org/schema/beans" 
  3.  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.  xmlns:p="http://www.springframework.org/schema/p" 
  5.  xmlns:context="http://www.springframework.org/schema/context"  
  6.  xmlns:tx="http://www.springframework.org/schema/tx" 
  7.  xmlns:aop="http://www.springframework.org/schema/aop" 
  8.  xmlns:jpa="http://www.springframework.org/schema/data/jpa" 
  9.  xsi:schemaLocation=" 
  10.         http://www.springframework.org/schema/beans 
  11.         http://www.springframework.org/schema/beans/spring-beans.xsd 
  12.         http://www.springframework.org/schema/context 
  13.         http://www.springframework.org/schema/context/spring-context.xsd 
  14.         http://www.springframework.org/schema/tx 
  15.         http://www.springframework.org/schema/tx/spring-tx.xsd 
  16.         http://www.springframework.org/schema/aop 
  17.         http://www.springframework.org/schema/aop/spring-aop.xsd 
  18.         http://www.springframework.org/schema/data/jpa  
  19.         http://www.springframework.org/schema/data/jpa/spring-jpa-1.2.xsd"> 
  20.          
  21.         <!--引入Properties文件--> 
  22.         <context:property-placeholder location="classpath:config/db.properties"/> 
  23.          
  24.         <!--配置c3p0的连接池--> 
  25.         <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
  26.             <property name="driverClass" value="${driverClass}"></property> 
  27.             <property name="jdbcUrl" value="${url}"></property> 
  28.             <property name="user" value="${user}"></property> 
  29.             <property name="password" value="${password}"></property> 
  30.             <property name="acquireIncrement" value="${acquireIncrement}"></property> 
  31.             <property name="maxPoolSize" value="${maxPoolSize}"></property> 
  32.             <property name="minPoolSize" value="${minPoolSize}"></property> 
  33.             <property name="maxStatements" value="${maxStatements}"></property> 
  34.         </bean> 
  35.          
  36.         <!--配置JPA实现产品的适配器--> 
  37.         <bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"
  38.         </bean> 
  39.          
  40.         <!--配置EntityManager对象--> 
  41.          
  42.         <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
  43.            <!--注入数据源--> 
  44.            <property name="dataSource" ref="dataSource"></property> 
  45.            <!--扫描entity包的-->  
  46.            <property name="packagesToScan" value="com.qy.helloworld"></property> 
  47.            <!--注入JPA实现产品的适配器--> 
  48.            <property name="jpaVendorAdapter" ref="jpaVendorAdapter"></property> 
  49.            <!--配置的是Hibernate的其他配置  除了连接数据库4大要素之外的其余配置--> 
  50.            <property name="jpaProperties"
  51.               <props> 
  52.                <!--是否自动创建表 --> 
  53.                <prop key="hibernate.hbm2ddl.auto">update</prop> 
  54.                <!--配置是否展示SQL--> 
  55.                <prop key="hibernate.show_sql">true</prop> 
  56.                <!--是否格式化SQL--> 
  57.                <prop key="hibernate.format_sql">true</prop> 
  58.                <!--连接数据库的方言--> 
  59.                <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop> 
  60.             </props> 
  61.            </property> 
  62.         </bean> 
  63.          
  64.          
  65.         <!--配置事务环境--> 
  66.          
  67.         <bean id="jpaTransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"
  68.            <!--注入dataSource--> 
  69.            <property name="dataSource" ref="dataSource"></property> 
  70.            <!--注入entityManagerFactory对象--> 
  71.            <property name="entityManagerFactory" ref="entityManagerFactory"></property> 
  72.         </bean> 
  73.          
  74.         <!--使用事务--> 
  75.         <tx:annotation-driven transaction-manager="jpaTransactionManager"/> 
  76.          
  77.         <!--配置AOP的自动代理--> 
  78.         <aop:aspectj-autoproxy></aop:aspectj-autoproxy>  
  79.          
  80.         <!--配置Spring的包扫描--> 
  81.         <context:component-scan base-package="com.qy.helloworld"></context:component-scan> 
  82.          
  83.         <!--Spring data的包的扫描  这里的扫描扫描的是DAO层所在的位置--> 
  84.         <jpa:repositories base-package="com.qy.helloworld" entity-manager-factory-ref="entityManagerFactory" transaction-manager-ref="jpaTransactionManager"></jpa:repositories> 
  85.  
  86. </beans>         

编写实体类和映射

  1. @Entity 
  2. @Table(name="t_user"
  3. public class User { 
  4.  
  5.  @Id 
  6.  @GeneratedValue(strategy=GenerationType.IDENTITY) 
  7.  private int userId; 
  8.   
  9.  private String userName; 
  10.   
  11.  private String password

编写Repository类

  1. public interface UserRepository extends Repository<User,Integer>{ 
  2.  /** 
  3.   * 这个的意思是通过id找用户 
  4.   * @Title: getByUserId    
  5.   * @Description: TODO 
  6.   * @param: @param userId 
  7.   * @param: @return       
  8.   * @returnUser       
  9.   * @throws 
  10.   */ 
  11.  public User getByUserId(int userId); 

测试

  1. ClassPathXmlApplicationContext applicationContext=new ClassPathXmlApplicationContext("config/bean-base.xml"); 
  2.   //获取DAO的对象 
  3.   UserRepository userRepository=applicationContext.getBean(UserRepository.class); 
  4.  User users=userRepository.findByUserId(1); 

通过名字来确定方法

代码演示:

举例如下:

  1. public interface UserRepository extends Repository<User,Integer>{ 
  2.  /** 
  3.   * 这个的意思是通过id找用户 
  4.   * @Title: getByUserId    
  5.   * @Description: TODO 
  6.   * @param: @param userId 
  7.   * @param: @return       
  8.   * @returnUser       
  9.   * @throws 
  10.   */ 
  11.  public User getByUserId(int userId); 
  12.   
  13.  /** 
  14.   * 记住查询的开头只能是  get 或者  find 开头   By:通过什么查询 
  15.   * @Title: findByUserId    
  16.   * @Description: TODO 
  17.   * @param: @param userId 
  18.   * @param: @return       
  19.   * @returnUser       
  20.   * @throws 
  21.   */ 
  22.  public User findByUserId(int userId); 
  23.   
  24.  /** 
  25.   * 通过用户名的模糊查询 
  26.   * @Title: findByUserNameLike    
  27.   * @Description: TODO 
  28.   * @param: @param userName 
  29.   * @param: @return       
  30.   * @return: List<User>       
  31.   * @throws 
  32.   */ 
  33.  public List<User> findByUserNameLike(String userName); 
  34.   
  35.  /** 
  36.   * 通过用户名和密码的Like来进行查询 
  37.   * @Title: findByUserNameLikeAndPasswordLike    
  38.   * @Description: TODO 
  39.   * @param: @param userName 
  40.   * @param: @return       
  41.   * @return: List<User>       
  42.   * @throws 
  43.   */ 
  44.  public List<User> findByUserNameLikeAndPasswordLike(String userName,String password); 
  45.  /** 
  46.   * 用户名和密码like 然后id小于一个范围 
  47.   * @Title: findByUserNameLikeAndPasswordLikeAndUserIdLessThan    
  48.   * @Description: TODO 
  49.   * @param: @param userName 
  50.   * @param: @param password 
  51.   * @param: @param userId 
  52.   * @param: @return       
  53.   * @return: List<User>       
  54.   * @throws 
  55.   */ 
  56.  public List<User> findByUserNameLikeAndPasswordLikeAndUserIdLessThan(String userName,String password,int userId); 

注意:一般情况下不会通过名字直接来写相应的方法,因为如果条件过多那么这个时候我们就存在名字特别长的问题

通过注解的模式来实现查询

代码演示:

举例如下:

  1. /** 
  2.     * 查询所有  没有条件直接查询 
  3.     * @Title: findUserAll    
  4.     * @Description: TODO 
  5.     * @param: @return       
  6.     * @return: List<User>       
  7.     * @throws 
  8.     */ 
  9. @Query("from User"
  10. public List<User> findUserAll(); 
  11.  
  12. /** 
  13.  * 通过id来查找数据     参数直接拼接到后面 
  14.  * @Title: findUserById    
  15.  * @Description: TODO 
  16.  * @param: @param userId 
  17.  * @param: @return       
  18.  * @return: List<User>       
  19.  * @throws 
  20.  */ 
  21. @Query("from User u  where u.userId<3"
  22. public List<User> findUserById(); 
  23. /** 
  24.  * 通过id查询存在占位符的情况 
  25.  * @Title: findUserById1    
  26.  * @Description: TODO 
  27.  * @param: @param userId 
  28.  * @param: @return       
  29.  * @return: List<User>       
  30.  * @throws 
  31.  */ 
  32. @Query("from User u  where u.userId<?"
  33. public List<User> findUserById1(int userId); 
  34.  
  35. /** 
  36.  * 多条件的查询  可以指定当前的参数映射的这个位置 
  37.  * @Title: getUserByNameAndId    
  38.  * @Description: TODO 
  39.  * @param: @param userName 
  40.  * @param: @param userId 
  41.  * @param: @return       
  42.  * @returnUser       
  43.  * @throws 
  44.  */ 
  45. @Query("from User u where u.userId=?2 and u.userName=?1"
  46. public User getUserByNameAndId(String userName,int userId); 
  47.  
  48. /** 
  49.  * 模糊查询的时候动态拼接上  %的问题 
  50.  * @Title: findUserByLike1    
  51.  * @Description: TODO 
  52.  * @param: @param userName 
  53.  * @param: @return       
  54.  * @return: List<User>       
  55.  * @throws 
  56.  */ 
  57. @Query("from User u where u.userName like concat ('%',?,'%')"
  58. public List<User> findUserByLike1(String userName); 

写本地的SQL 查询

代码演示:

举例如下:

  1. /** 
  2.  * 通过 
  3.  * @Title: findUserAll11    
  4.  * @Description: TODO 
  5.  * @param: @return       
  6.  * @return: List<User>       
  7.  * @throws 
  8.  */ 
  9. @Query(nativeQuery=true,value="select * from t_user"
  10. public List<User> findUserAll11(); 

增删改的玩法

代码演示:

添加业务逻辑 增加事务环境:

  1.  @Service 
  2. @Transactional                  //提供一个事务的环境 
  3. public class UserService { 
  4.   
  5.  @Autowired 
  6.  private UserRepository userRepository=null
  7.   
  8.  /** 
  9.   * 数据的更新 
  10.   * @Title: update    
  11.   * @Description: TODO 
  12.   * @param: @param userName 
  13.   * @param: @param password 
  14.   * @param: @param userId       
  15.   * @return: void       
  16.   * @throws 
  17.   */ 
  18.  public void update(String userName,String password,int userId){ 
  19.   userRepository.update(userName, password, userId); 
  20.  } 
  21.   
  22.   
  23.  public void delete(int userId){ 
  24.   userRepository.delete(userId); 
  25.  } 
  26.   
  27.  public void insert(String userName,String password){ 
  28.   userRepository.insert(userName, password); 
  29.  } 
  30.  

编写repository的对象:

  1.  public interface UserRepository extends Repository<User,Integer>{ 
  2.  /** 
  3.   * 实现增删改的方法 
  4.   * @Title: add    
  5.   * @Description: TODO 
  6.   * @param: @param userName 
  7.   * @param: @param password       
  8.   * @return: void       
  9.   * @throws 
  10.   */ 
  11.  @Modifying    //这个注解的作用表示的是更新数据 
  12.  @Query("update User u set u.userName=?,u.password=? where u.userId=?"
  13.  public void update(String userName,String password,int userId); 
  14.   
  15.  /** 
  16.   * 更新数据 
  17.   * @Title: delete    
  18.   * @Description: TODO 
  19.   * @param: @param userId       
  20.   * @return: void       
  21.   * @throws 
  22.   */ 
  23.  @Modifying    //这个注解的作用表示的是更新数据 
  24.  @Query("delete User u where u.userId=?"
  25.  public void delete(int userId); 
  26.  /** 
  27.   * 添加数据 
  28.   * @Title: insert    
  29.   * @Description: TODO 
  30.   * @param: @param userName 
  31.   * @param: @param password       
  32.   * @return: void       
  33.   * @throws 
  34.   */ 
  35.  @Modifying    //这个注解的作用表示的是更新数据 
  36.  @Query(nativeQuery=true,value="insert into t_user(userName,password) values(?,?)"
  37.  public void insert(String userName,String password); 
  38.   

测试:

  1. @Test 
  2. public void testHelloWorld() throws Exception { 
  3.   
  4.  ClassPathXmlApplicationContext applicationContext=new ClassPathXmlApplicationContext("config/bean-base.xml"); 
  5.  //获取DAO的对象 
  6.  UserService userService=applicationContext.getBean(UserService.class); 
  7.  
  8.  userService.insert("小羽","做程序的"); 

使用框架中提供的增删改查的方法

代码演示:

提供的是Repository:

  1. public interface UserRepository extends CrudRepository<User,Integer>{ 

分页和排序

代码演示:

提供的Repository:

  1. public interface UserRepository extends PagingAndSortingRepository<User,Integer>{ 

测试:

  1. public class Test001 { 
  2.  
  3.  
  4. @Test 
  5. public void testPaging() throws Exception { 
  6.  ClassPathXmlApplicationContext applicationContext=new ClassPathXmlApplicationContext("config/bean-base.xml"); 
  7.  //获取DAO的对象 
  8.  UserRepository userRepository=applicationContext.getBean(UserRepository.class); 
  9. /** 
  10.  * 第一个参数:当前的页的页数是多少  页数是从0开始的   第二页:2-1 
  11.  * 第二个参数:表示的是每一页条目数 
  12.  */ 
  13.  Page<User> pages=userRepository.findAll(new PageRequest(2-1,2)); 
  14.  
  15.  System.out.println("查询到的数据:"+pages.getContent()); 
  16.  System.out.println("数据的条目数:"+pages.getSize()); 
  17.  System.out.println("页数:"+pages.getNumber()); 
  18.  System.out.println("数据条目的总数:"+pages.getTotalElements()); 
  19.  System.out.println("一共的页数:"+pages.getTotalPages()); 
  20.  System.out.println("排序的规则:"+pages.getSort()); 
  21.  
  22.  
  23. /** 
  24.  * 排序 
  25.  * @Title: testSort    
  26.  * @Description: TODO 
  27.  * @param: @throws Exception       
  28.  * @return: void       
  29.  * @throws 
  30.  */ 
  31. @Test 
  32. public void testSort() throws Exception { 
  33.  ClassPathXmlApplicationContext applicationContext=new ClassPathXmlApplicationContext("config/bean-base.xml"); 
  34.  //获取DAO的对象 
  35.  UserRepository userRepository=applicationContext.getBean(UserRepository.class); 
  36.  /** 
  37.   * 排序 
  38.   * 第一个参数:升序或者降序  Direction.ASC/DESC 
  39.   * 第二个参数: 排序的这个列 
  40.   */ 
  41.  List<User> users=(List<User>) userRepository.findAll(new Sort(Direction.DESC,"userId")); 
  42.   
  43.  System.out.println(users); 
  44.  
  45. /** 
  46.  * 排序后分页 
  47.  * @Title: testSortAndPaging    
  48.  * @Description: TODO 
  49.  * @param: @throws Exception       
  50.  * @return: void       
  51.  * @throws 
  52.  */ 
  53. @Test 
  54. public void testSortAndPaging() throws Exception { 
  55.  ClassPathXmlApplicationContext applicationContext=new ClassPathXmlApplicationContext("config/bean-base.xml"); 
  56.  //获取DAO的对象 
  57.  UserRepository userRepository=applicationContext.getBean(UserRepository.class); 
  58.   
  59.  Page<User> pages=userRepository.findAll(new PageRequest(2-1,2,new Sort(Direction.DESC,"userId"))); 
  60.   
  61.  System.out.println(pages.getContent()); 

JpaRepository的使用

代码演示:

提供的repository:

  1. public interface UserRepository extends JpaRepository<User,Integer>{ 

测试:

  1.  public class Test001 { 
  2.  @Test 
  3.  public void testPaging() throws Exception { 
  4.   ClassPathXmlApplicationContext applicationContext=new ClassPathXmlApplicationContext("config/bean-base.xml"); 
  5.   //获取DAO的对象 
  6.   UserRepository userRepository=applicationContext.getBean(UserRepository.class); 
  7.      
  8. //  long count=userRepository.count(); 
  9.    
  10. //  User user=userRepository.findOne(15); 
  11. //  user.setUserName("小羽"); 
  12.    
  13.   //保存或者更新数据 
  14. //  userRepository.saveAndFlush(user); 
  15.    
  16.   
  17.   List<User> users=userRepository.findAll(); 
  18.    
  19.   //批处理 
  20.   userRepository.deleteInBatch(users); 
  21.     
  22.   //System.out.println("统计:"+count); 
  23.  } 

结语

Spring Data是我们开发中离不开的经常用到的技术,其涉及的技术和知识面其实远不止上面列出的这些。

后续浅羽会继续更新关于Spring Data的开发知识,只希望能对大家有所帮助,谢谢大家的支持!

 

责任编辑:姜华 来源: 浅羽的IT小屋
相关推荐

2021-02-26 11:54:38

MyBatis 插件接口

2024-03-05 18:27:43

2021-11-24 16:02:57

鸿蒙HarmonyOS应用

2022-06-30 08:13:44

PythonWeb编程语言

2011-05-03 15:59:00

黑盒打印机

2011-01-10 14:41:26

2023-03-27 08:28:57

spring代码,starter

2009-06-02 15:38:36

eclipse streclipse开发steclipse str

2024-03-18 18:07:38

VSCode插件文件

2011-05-27 08:41:26

JavascriptFirefox

2022-02-14 09:11:17

视频下载器抖音

2019-05-05 11:47:09

TypeScript开发Node.js

2011-02-22 13:46:27

微软SQL.NET

2021-12-28 08:38:26

Linux 中断唤醒系统Linux 系统

2012-03-06 11:25:40

ibmdw

2021-09-15 07:33:33

Java开发在线

2021-01-13 14:00:24

鸿蒙hi3518开发

2022-12-05 10:47:37

2015-04-02 11:22:29

2021-07-14 09:00:00

JavaFX开发应用
点赞
收藏

51CTO技术栈公众号