本节作为主要讲解Spring Data的环境搭建
JPA Spring Data :致力于减少数据访问层(DAO)的开发量。开发者唯一要做的就是声明持久层的接口,其他都交给Spring Data JPA来帮你完成!
使用Spring Data JPA进行持久层开发需要的四个步骤:
- 配置Spring 整合 JPA
- 在Spring配置文件中配置Spring Data,让Spring 为声明的接口创建代理对象。配置了<jpa:repositories>后,Spring 初始化容器时将会扫描base-package 指定的包目录及其子目录,为继承Repository或其子接口的接口创建代理对象,并将代理对象注册为SpringBean,业务层便可以通过Spring的自动封装的特性来直接使用该对象。
- 声明持久层的接口,该接口继承Repository。Repository是一个标记型接口,它不包含任何方法,如必要,Spring Data 可实现Repository其他子接口,其中定义了一些常用的增删改查,以及分页相关的方法。
- 在接口中声明需要的方法。Spring Data将根据给定的策略,来为其生成实现代码。
1.所需要的包
① 加入spring包
- spring-aop-4.3.8.RELEASE.jar
- spring-aspects-4.3.8.RELEASE.jar
- spring-beans-4.3.8.RELEASE.jar
- spring-context-4.3.8.RELEASE.jar
- spring-core-4.3.8.RELEASE.jar
- spring-expression-4.3.8.RELEASE.jar
- spring-jdbc-4.3.8.RELEASE.jar
- spring-orm-4.3.8.RELEASE.jar
- spring-tx-4.3.8.RELEASE.jar
- spring-web-4.3.8.RELEASE.jar
- spring-webmvc-4.3.8.RELEASE.jar
- com.springsource.net.sf.cglib-2.2.0.jar
- com.springsource.org.aopalliance-1.0.0.jar
- com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
- commons-logging-1.1.1.jar
② 加入hibernate包
- antlr-2.7.7.jar
- dom4j-1.6.1.jar
- hibernate-commons-annotations-4.0.5.Final.jar
- hibernate-core-4.3.11.Final.jar
- hibernate-jpa-2.1-api-1.0.0.Final.jar
- jandex-1.1.0.Final.jar
- javassist-3.18.1-GA.jar
- jboss-logging-3.1.3.GA.jar
- jboss-logging-annotations-1.2.0.Beta1.jar
- jboss-transaction-api_1.2_spec-1.0.0.Final.jar
③ 加jpa的包
hibernate-entitymanager-4.3.11.Final.jar
④ 加c3p0的包
- c3p0-0.9.2.1.jar
- hibernate-c3p0-4.3.11.Final.jar
- mchange-commons-java-0.2.3.4.jar
⑤ 加mysql的驱动
mysql-connector-java-5.1.7-bin.jar
⑥加入springData
⑦加入 slf4j-api-1.6.1.jar
2.Spring Bean配置文件
applicationContext.xml
				?
			
| 
								1
 
								2
 
								3
 
								4
 
								5
 
								6
 
								7
 
								8
 
								9
 
								10
 
								11
 
								12
 
								13
 
								14
 
								15
 
								16
 
								17
 
								18
 
								19
 
								20
 
								21
 
								22
 
								23
 
								24
 
								25
 
								26
 
								27
 
								28
 
								29
 
								30
 
								31
 
								32
 
								33
 
								34
 
								35
 
								36
 
								37
 
								38
 
								39
 
								40
 
								41
 
								42
 
								43
 
								44
 
								45
 
								46
 
								47
 
								48
 
								49
 
								50
 
								51
 
								52
 
								53
 
								54
						 | <?xmlversion="1.0"encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:jpa="http://www.springframework.org/schema/data/jpa"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsdhttp://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd"><context:property-placeholderlocation="classpath:db.properties"/><!--配置数据源 --><beanid="dataSource"class="com.mchange.v2.c3p0.ComboPooledDataSource"><propertyname="user"value="${jdbc.user}"></property><propertyname="password"value="${jdbc.password}"></property><propertyname="jdbcUrl"value="${jdbc.jdbcUrl}"></property><propertyname="driverClass"value="${jdbc.driverClass}"></property></bean><!--配置JPA的entityManagerFactory --><beanid="entityManagerFactory"class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"><propertyname="dataSource"ref="dataSource"></property><!-- 配置JPA的实现 --><propertyname="jpaVendorAdapter"><beanclass="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"></bean></property><propertyname="packagesToScan"value="com.ntjr.springdata"></property><propertyname="jpaProperties"><props><!-- 二级缓存相关 --><!-- <prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop> <prop key="net.sf.ehcache.configurationResourceName">ehcache-hibernate.xml</prop> --><!-- 生成的数据表的列的映射策略 --><propkey="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop><!-- hibernate 基本属性 --><propkey="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop><propkey="hibernate.show_sql">true</prop><propkey="hibernate.format_sql">true</prop><propkey="hibernate.hbm2ddl.auto">update</prop></props></property></bean><!--配置事物管理器 --><beanid="transactionManager"class="org.springframework.orm.jpa.JpaTransactionManager"><propertyname="entityManagerFactory"ref="entityManagerFactory"></property></bean><!-- 配置支持注解的事物 --><tx:annotation-driventransaction-manager="transactionManager"/><!--配置springData --><!--base-package:扫描Repository Bean所在的package --><jpa:repositoriesbase-package="com.ntjr.springdata"entity-manager-factory-ref="entityManagerFactory"transaction-manager-ref="transactionManager"></jpa:repositories></beans>applicationContext.xml | 
3.数据库持久类
Person.java
				?
			
| 
								1
 
								2
 
								3
 
								4
 
								5
 
								6
 
								7
 
								8
 
								9
 
								10
 
								11
 
								12
 
								13
 
								14
 
								15
 
								16
 
								17
 
								18
 
								19
 
								20
 
								21
 
								22
 
								23
 
								24
 
								25
 
								26
 
								27
 
								28
 
								29
 
								30
 
								31
 
								32
 
								33
 
								34
 
								35
 
								36
 
								37
 
								38
 
								39
 
								40
 
								41
 
								42
 
								43
 
								44
 
								45
 
								46
 
								47
						 | packagecom.ntjr.springdata; importjava.util.Date;importjavax.persistence.Column;importjavax.persistence.Entity;importjavax.persistence.GeneratedValue;importjavax.persistence.Id;importjavax.persistence.Table;@Table(name = "JPA_PERSONS")@EntitypublicclassPerson {privateInteger id;privateString lastName;privateString email;privateDate birth;@GeneratedValue@IdpublicInteger getId() {returnid;}publicvoidsetId(Integer id) {this.id = id;}publicString getLastName() {returnlastName;}@Column(name = "LAST_NAME")publicvoidsetLastName(String lastName) {this.lastName = lastName;}publicString getEmail() {returnemail;} publicvoidsetEmail(String email) {this.email = email;}publicDate getBirth() {returnbirth;}publicvoidsetBirth(Date birth) {this.birth = birth;}@OverridepublicString toString() {return"Person [id="+ id + ", lastName="+ lastName + ", email="+ email + ", birth="+ birth + "]";}} | 
4.DAO
PersonRepository.java
				?
			
                	
    
	
	
		
		
	
 
	
		
			
	
	 
     
	
			
                 
			
		
		
			
			
			
    
        
        
	
			
						
			
            			
    		
    		
		
	    
    	
    	
        
    	
    
| 
								1
 
								2
 
								3
 
								4
 
								5
 
								6
 
								7
 
								8
 
								9
 
								10
 
								11
						 | packagecom.ntjr.springdata;importorg.springframework.data.repository.Repository; /*** * 1、实现Repository接口 * 2、通过注解的方式@RepositoryDefinition将一个bean定义为Repository接口*/publicinterfacePersonRepsitory extendsRepository<Person, Integer> {// 根据lastName获取对应的personPerson getByLastName(String lastName);} | 
5.测试
SpringDataTest.java
				?
			
	
						
						
						
						
						
						
						
																		
    
        
    
        
                        
                
                    
                
                
                
                    
                
                
                
                    
                
                
                
                    
                
                        
    
 																		
						
																		
    
        
 												
						
																		
	
	
		
				
			
																		
						
						
					
				
				                | 
								1
 
								2
 
								3
 
								4
 
								5
 
								6
 
								7
 
								8
 
								9
 
								10
 
								11
 
								12
 
								13
 
								14
 
								15
 
								16
 
								17
 
								18
						 | packagecom.ntjr.springdata.test;importorg.junit.Test;importorg.springframework.context.ApplicationContext;importorg.springframework.context.support.ClassPathXmlApplicationContext;importcom.ntjr.springdata.Person;importcom.ntjr.springdata.PersonRepsitory;publicclassSpringDataTest {privateApplicationContext ctx = null;{ctx = newClassPathXmlApplicationContext("applicationContext.xml");}@TestpublicvoidgetPersonForLastName() {PersonRepsitory personRepsitory = ctx.getBean(PersonRepsitory.class);Person person = personRepsitory.getByLastName("AA");System.out.println(person);}} | 
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。
原文链接:https://www.cnblogs.com/zhaobingqing/p/6854322.html
相关文章
             猜你喜欢
        
        - 64M VPS建站:怎样优化以提高网站加载速度? 2025-06-10
- 64M VPS建站:是否适合初学者操作和管理? 2025-06-10
- ASP.NET自助建站系统中的用户注册和登录功能定制方法 2025-06-10
- ASP.NET自助建站系统的域名绑定与解析教程 2025-06-10
- 个人服务器网站搭建:如何选择合适的服务器提供商? 2025-06-10
			TA的动态
			
		
				- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
				您的支持,是我们最大的动力!				
			
		
        热门文章
        
    
    - 
            2025-06-04 96
- 
            2025-06-04 80
- 
            2025-05-27 102
- 
            2025-05-29 91
- 
            2025-06-04 39
		热门评论
	
	 
        
 
    		 
            	 
															 
         
        
 
                        