`

Spring3 +JPA

 
阅读更多

1)META-INF/persistence.xml

This EntityManagerFactory bootstrap is appropriate for standalone applications

 * which solely use JPA for data access. If you want to set up your persistence

 * provider for an external DataSource and/or for global transactions which span

 * multiple resources, you will need to either deploy it into a full Java EE 5

 * application server and access the deployed EntityManagerFactory via JNDI,

 * or use Spring's {@link LocalContainerEntityManagerFactoryBean} with appropriate

 * configuration for local setup according to JPA's container contract.

 

 

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
  <persistence-unit name="WebApplication1PU" transaction-type="RESOURCE_LOCAL">
		<provider>org.hibernate.ejb.HibernatePersistence</provider>
	<class>com.cdjh.access.StudentInfo</class>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties>
      <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/test?zeroDateTimeBehavior=convertToNull"/>
      <property name="javax.persistence.jdbc.password" value="12342256"/>
      <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
      <property name="javax.persistence.jdbc.user" value="root"/>
      <property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider"/>
      <property name="javax.persistence.schema-generation.database.action" value="none"/>
    </properties>
	</persistence-unit>
</persistence>

 

Spring 配置文件

 

<?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:mvc="http://www.springframework.org/schema/mvc"
     xmlns:tx="http://www.springframework.org/schema/tx" 
     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.xsd
         http://www.springframework.org/schema/tx   
         http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
         http://www.springframework.org/schema/mvc  
         http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <!-- 
  <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">  
   <property name="dataSource" ref="dataSource" />  
   <property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml"/>  
 <property name="loadTimeWeaver">  
     <bean  
 class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />  
 </property>  
  </bean>  
  org.springframework.orm.jpa.LocalEntityManagerFactoryBean
  org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean
   -->  
    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">  
       <!--property name="dataSource" ref="dataSource" /-->
       <!--property name="jpaVendorAdapter" ref="hibernateJpaVendorAdapter" /-->
       <property name="persistenceProvider">
           <bean class="org.hibernate.jpa.HibernatePersistenceProvider"/>
       </property>
   </bean>  

<bean id="hibernateJpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
		<!--property name="databasePlatform">
			<bean factory-method="getDialect" class="com.tzdr.common.dao.support.Hibernates">
				<constructor-arg ref="dataSource" />
			</bean>
		</property-->
	</bean>

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">     
     <property name="driverClassName" value="com.mysql.jdbc.Driver" /> 
    <property name="url" value="jdbc:mysql://localhost:3306/test" /> 
    <property name="username" value="root" />      
   <property name="password" value="123456" />     
</bean>    

<!--事务管理Bean-->  
  <bean id="myTxManager" class="org.springframework.orm.jpa.JpaTransactionManager">  
   <property name="entityManagerFactory" ref="entityManagerFactory"/>  
  </bean>  
<!--说明事务的配置使用的注解方式-->  
   <tx:annotation-driven transaction-manager="myTxManager"/>  
    

</beans> 

 

package com.cdjh.access;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="t_stuxxx")
public class StudentInfo {
	
	@Id
	@Column
	private Integer id;
	
	@Column
	private String name;

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	

}

 

执行内容

ClassPathXmlApplicationContext app = new ClassPathXmlApplicationContext("classpath:com/cdjh/access/spring-bean.xml");
    	

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics