亲宝软件园·资讯

展开

Spring Data环境搭建 Spring Data环境搭建实现过程解析

IT-執念 人气:0
想了解Spring Data环境搭建实现过程解析的相关内容吗,IT-執念在本文为您仔细讲解Spring Data环境搭建的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Spring,Data,环境搭建,下面大家一起来学习吧。

 本节作为主要讲解Spring Data的环境搭建

JPA Spring Data :致力于减少数据访问层(DAO)的开发量。开发者唯一要做的就是声明持久层的接口,其他都交给Spring Data JPA来帮你完成!

使用Spring Data JPA进行持久层开发需要的四个步骤:

环境搭建

   1.所需要的包

    ① 加入spring包

    ② 加入hibernate包

    ③ 加jpa的包

      hibernate-entitymanager-4.3.11.Final.jar

    ④ 加c3p0的包

    ⑤ 加mysql的驱动

      mysql-connector-java-5.1.7-bin.jar

    ⑥加入springData

    ⑦加入 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" />
   <!--配置数据源 -->
   <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的实现 -->
     <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>
         <!-- 二级缓存相关 -->
         <!-- <prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop> 
           <prop key="net.sf.ehcache.configurationResourceName">ehcache-hibernate.xml</prop> -->
         <!-- 生成的数据表的列的映射策略 -->
         <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.数据库持久类

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、实现Repository接口 
  * 2、通过注解的方式@RepositoryDefinition将一个bean定义为Repository接口
  */
 public interface PersonRepsitory extends Repository<Person, Integer> {
   // 根据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);
   }
 }

加载全部内容

相关教程
猜你喜欢
用户评论