Spring Data環(huán)境搭建實現(xiàn)過程解析
本節(jié)作為主要講解Spring Data的環(huán)境搭建
JPA Spring Data :致力于減少數(shù)據(jù)訪問層(DAO)的開發(fā)量。開發(fā)者唯一要做的就是聲明持久層的接口,其他都交給Spring Data JPA來幫你完成!
使用Spring Data JPA進行持久層開發(fā)需要的四個步驟:
- 配置Spring 整合 JPA
- 在Spring配置文件中配置Spring Data,讓Spring 為聲明的接口創(chuàng)建代理對象。配置了<jpa:repositories>后,Spring 初始化容器時將會掃描base-package 指定的包目錄及其子目錄,為繼承Repository或其子接口的接口創(chuàng)建代理對象,并將代理對象注冊為SpringBean,業(yè)務層便可以通過Spring的自動封裝的特性來直接使用該對象。
- 聲明持久層的接口,該接口繼承Repository。Repository是一個標記型接口,它不包含任何方法,如必要,Spring Data 可實現(xiàn)Repository其他子接口,其中定義了一些常用的增刪改查,以及分頁相關(guān)的方法。
- 在接口中聲明需要的方法。Spring Data將根據(jù)給定的策略,來為其生成實現(xiàn)代碼。
環(huán)境搭建
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的驅(qū)動
mysql-connector-java-5.1.7-bin.jar
?、藜尤雜pringData
- spring-data-commons-1.6.2.RELEASE.jar
- spring-data-jpa-1.4.2.RELEASE.jar
?、呒尤?slf4j-api-1.6.1.jar
2.Spring Bean配置文件
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd"> <context:property-placeholder location="classpath:db.properties" /> <!--配置數(shù)據(jù)源 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="user" value="${jdbc.user}"></property> <property name="password" value="${jdbc.password}"></property> <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property> <property name="driverClass" value="${jdbc.driverClass}"></property> </bean> <!--配置JPA的entityManagerFactory --> <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="dataSource" ref="dataSource"></property> <!-- 配置JPA的實現(xiàn) --> <property name="jpaVendorAdapter"> <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"></bean> </property> <property name="packagesToScan" value="com.ntjr.springdata"></property> <property name="jpaProperties"> <props> <!-- 二級緩存相關(guān) --> <!-- <prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop> <prop key="net.sf.ehcache.configurationResourceName">ehcache-hibernate.xml</prop> --> <!-- 生成的數(shù)據(jù)表的列的映射策略 --> <prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop> <!-- hibernate 基本屬性 --> <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> </props> </property> </bean> <!--配置事物管理器 --> <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="entityManagerFactory" ref="entityManagerFactory"></property> </bean> <!-- 配置支持注解的事物 --> <tx:annotation-driven transaction-manager="transactionManager" /> <!--配置springData --> <!--base-package:掃描Repository Bean所在的package --> <jpa:repositories base-package="com.ntjr.springdata" entity-manager-factory-ref="entityManagerFactory" transaction-manager-ref="transactionManager"> </jpa:repositories> </beans> applicationContext.xml
3.數(shù)據(jù)庫持久類
Person.java
package com.ntjr.springdata; import java.util.Date; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Table; @Table(name = "JPA_PERSONS") @Entity public class Person { private Integer id; private String lastName; private String email; private Date birth; @GeneratedValue @Id public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getLastName() { return lastName; } @Column(name = "LAST_NAME") public void setLastName(String lastName) { this.lastName = lastName; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public Date getBirth() { return birth; } public void setBirth(Date birth) { this.birth = birth; } @Override public String toString() { return "Person [id=" + id + ", lastName=" + lastName + ", email=" + email + ", birth=" + birth + "]"; } }
4.DAO
PersonRepository.java
package com.ntjr.springdata; import org.springframework.data.repository.Repository; /** * * 1、實現(xiàn)Repository接口 * 2、通過注解的方式@RepositoryDefinition將一個bean定義為Repository接口 */ public interface PersonRepsitory extends Repository<Person, Integer> { // 根據(jù)lastName獲取對應的person Person getByLastName(String lastName); }
5.測試
SpringDataTest.java
package com.ntjr.springdata.test; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.ntjr.springdata.Person; import com.ntjr.springdata.PersonRepsitory; public class SpringDataTest { private ApplicationContext ctx = null; { ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); } @Test public void getPersonForLastName() { PersonRepsitory personRepsitory = ctx.getBean(PersonRepsitory.class); Person person = personRepsitory.getByLastName("AA"); System.out.println(person); } }
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
基于Comparator對象集合實現(xiàn)多個條件按照優(yōu)先級的比較
這篇文章主要介紹了基于Comparator對象集合實現(xiàn)多個條件按照優(yōu)先級的比較,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07java使用xfire搭建webservice服務的過程詳解
這篇文章主要介紹了java使用xfire搭建webservice服務的過程。使用xfire搭建webService的服務,可以在瀏覽器訪問。對此感興趣的可以了解一下2020-07-07Springboot整合Netty實現(xiàn)RPC服務器的示例代碼
這篇文章主要介紹了Springboot整合Netty實現(xiàn)RPC服務器的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-01-01Spring Boot3整合Mybatis Plus的詳細過程(數(shù)據(jù)庫為MySQL)
這篇文章主要介紹了Spring Boot3整合Mybatis Plus的詳細過程(數(shù)據(jù)庫為MySQL),本文給大家介紹的非常詳細,感興趣的朋友跟隨小編一起看看吧2024-07-07如何使用intellij IDEA搭建Spring Boot項目
這篇文章主要介紹了如何使用intellij IDEA搭建Spring Boot項目,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-07-07