spring 与jpa集成

 spring 与jpa集成  , 持久层orm框架还是hibernate 
  //其实也就是改变持久层提供商

	     
	     persistence.xml  

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
   version="1.0">
   <persistence-unit name="persistenceUnit" transaction-type="RESOURCE_LOCAL" ><!-- 本地事务 -->
      <properties>
         <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
         <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
         <property name="hibernate.connection.username" value="root"/>
         <property name="hibernate.connection.password" value="123456"/>
         <property name="hibernate.connection.url" value="jdbc:mysql://127.0.0.1:3306/easier?useUnlcode=true&amp;characterEncoding=UTF-8"/>
         <property name="hibernate.max_fetch_depth" value="3"/>
         <property name="hibernate.show_sql" value="true"/>
         <property name="hibernate.hbm2ddl.auto" value="update"/>
         <!-- hibernate.hbm2ddl.auto
			validate             	加载hibernate时,验证创建数据库表结构
 			create                  每次加载hibernate先删除表,重新创建数据库表结构
 			create-drop        		加载hibernate先删除表后创建,退出是删除表结构
 			update                	加载hibernate自动更新数据库结构  
 		-->
        
      </properties>
     
   </persistence-unit>
</persistence>

  <bean id="entiryManagerFactory" class=""org.springframeword.orm.jpg.LocalEntityManagerFactoryBean>
  <!--这个persistenceUnitName的value等于上面persistence.xml中的persistence-unit的name-->
		<property name="persistenceUnitName" value="persistenceUnit"></property>
  </bean>
  <!-- 使用注解spring事务 -->
   <tx:annotation-driven transaction-manager="transactionManager"/>
      <!--服务层实现类-->
      <bean name="userService"  class="cn.complay.servivce.UserServiceImpl"></bean>

  <!--在UserServiceImp类中得到EntityManager-->
  @PersisterceContext private  EntityManager entityManager; 
  		//可以调用entityManager.persist(obj)方法了
 

你可能感兴趣的:(spring)