简单 spring + jpa 整合

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:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
           http://www.springframework.org/schema/tx
		   http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
		   http://www.springframework.org/schema/aop
		   http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.1.xsd">
	<context:annotation-config />       
	<context:component-scan base-package="ejb.ejb" />
	<aop:aspectj-autoproxy proxy-target-class="true" />
	<tx:annotation-driven transaction-manager="txManager" proxy-target-class="true" /> 
	 
	<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
		<property name="persistenceUnitName" value="test1"></property>
	</bean>
	
	<bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager">
		<property name="entityManagerFactory" ref="entityManagerFactory"></property>
	</bean>
	
</beans>
persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<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="test1" transaction-type="RESOURCE_LOCAL">
       <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <properties>
           <property name="hibernate.connection.username" value="root"/>
           <property name="hibernate.connection.password" value="root"/>
           <property name="hibernate.connection.url" value="jdbc:mysql:///ssh"/> 
           <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
           <property name="hibernate.show_sql" value="true"/>
           <property name="hibernate.hbm2ddl.auto" value="update"/>
           <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
        </properties> 
        
    </persistence-unit>
  
</persistence>

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>ejb</groupId>
    <artifactId>ejb-01</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>ejb</packaging>

    <name>ejb-01</name>

    <properties>
        <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    
    <repositories>
    <repository>
	    <id>maven2-repository.dev.java.net</id>
	    <name>Java.net Repository for Maven</name>
	    <url>http://download.java.net/maven/2/</url>
	    <layout>default</layout>
	</repository>  
    </repositories>
    
    <dependencies>   
    
   	   <dependency>
		  <groupId>org.hibernate</groupId>
		  <artifactId>hibernate-core</artifactId>
		  <version>3.6.2.Final</version>
		</dependency>
		<dependency>
		  <groupId>commons-collections</groupId>
		  <artifactId>commons-collections</artifactId>
		  <version>3.1</version>
		</dependency>
		<dependency>
		  <groupId>org.hibernate</groupId>  
		  <artifactId>hibernate-commons-annotations</artifactId>
		  <version>3.2.0.Final</version>
		</dependency>
		<dependency>
		  <groupId>org.hibernate</groupId>
		  <artifactId>hibernate-entitymanager</artifactId>
		  <version>3.6.2.Final</version>
		</dependency>
		<dependency>
		  <groupId>org.hibernate</groupId>
		  <artifactId>hibernate-validator</artifactId>
		  <version>3.1.0.GA</version>
		</dependency>
		<dependency>
		  <groupId>org.hibernate.javax.persistence</groupId>
		  <artifactId>hibernate-jpa-2.0-api</artifactId>
		  <version>1.0.1.Final</version>
		</dependency>
		<dependency>
		  <groupId>org.slf4j</groupId>
		  <artifactId>slf4j-api</artifactId>
		  <version>1.6.2</version>
		</dependency>
		<dependency>
        	<groupId>dom4j</groupId>
        	<artifactId>dom4j</artifactId>
        	<version>1.6.1</version>
        	<type>jar</type>
        	<scope>compile</scope>
        </dependency>
        <dependency>
        	<groupId>javassist</groupId>
        	<artifactId>javassist</artifactId>
        	<version>3.12.1.GA</version>
        	<type>jar</type>
        	<scope>compile</scope>
        </dependency>
		
    	
		
        <dependency>
        	<groupId>mysql</groupId>
        	<artifactId>mysql-connector-java</artifactId>
        	<version>5.1.21</version>
        	<type>jar</type>
        	<scope>compile</scope>
        </dependency>
        <dependency>
        	<groupId>junit</groupId>
        	<artifactId>junit</artifactId>
        	<version>4.10</version>
        	<type>jar</type>
        	<scope>compile</scope>
        </dependency>
        
        <dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>3.0.6.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context-support</artifactId>
			<version>3.0.6.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>3.0.6.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-beans</artifactId>
			<version>3.0.6.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-asm</artifactId>
			<version>3.0.6.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aop</artifactId>
			<version>3.0.6.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-expression</artifactId>
			<version>3.0.6.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-orm</artifactId>
			<version>3.0.6.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aspects</artifactId>
			<version>3.0.6.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.aspectj</groupId>
			<artifactId>aspectjweaver</artifactId>
			<version>1.6.0</version>
		</dependency>
		<dependency>
			<groupId>javassist</groupId>
			<artifactId>javassist</artifactId>
			<version>3.12.1.GA</version>
			<type>jar</type>
			<scope>compile</scope>
		</dependency>
		<dependency>
		  <groupId>cglib</groupId>
		  <artifactId>cglib</artifactId>
		  <version>2.2</version>
		</dependency>
		
		<dependency>
		  <groupId>org.apache.maven.plugins</groupId>
		  <artifactId>maven-install-plugin</artifactId>
		  <version>2.4</version>
		  <type>pom</type>
		</dependency>
     
        
    </dependencies>

    <build>
        <plugins>
          <plugin>
		        <artifactId>maven-clean-plugin</artifactId>
		        <version>2.5</version>
		        <executions>
		          <execution>
		            <id>auto-clean</id>
		            <phase>initialize</phase>
		            <goals>
		              <goal>clean</goal>
		            </goals>
		          </execution>
		        </executions>
	       </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                    <compilerArguments>
                        <endorseddirs>${endorsed.dir}</endorseddirs>
                    </compilerArguments>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-ejb-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <ejbVersion>3.1</ejbVersion>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.1</version>
                <executions>
                    <execution>
                        <phase>validate</phase>
                        <goals>
                            <goal>copy</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${endorsed.dir}</outputDirectory>
                            <silent>true</silent>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>javax</groupId>
                                    <artifactId>javaee-endorsed-api</artifactId>
                                    <version>6.0</version>
                                    <type>jar</type>
                                </artifactItem>
                            </artifactItems>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

实体

package ejb.ejb.pojo;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Table
@Entity
public class Person {

	@Id  
	@GeneratedValue(strategy = GenerationType.AUTO)
	private int id ;
	
	private String name ;
	
	private String password ;

	public int getId() {
		return id;
	}

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

	public String getName() {
		return name;
	}

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

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
	
	
}


Service

package ejb.ejb.service;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import ejb.ejb.pojo.Person;

@Service("PersonService")
@Transactional(propagation = Propagation.REQUIRED)
public class PersonService {
	
	@PersistenceContext
	private EntityManager em ;
	
	public void savePerson(Person person){
		em.persist(person);
	}

}

测试

package ejb.ejb.test;

import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import ejb.ejb.pojo.Person;
import ejb.ejb.service.PersonService;

public class Test2 {
	
	ApplicationContext context = null ; 
	
	PersonService ps = null ;
			
	
	@Before
	public void init(){
		context = new ClassPathXmlApplicationContext("applicationContext.xml");
		ps = (PersonService) context.getBean("PersonService");
	}
	
	@Test
	public void test(){ 
		Person person = new Person ();
    	person.setName("zhouyang");
    	person.setPassword("123");  
		ps.savePerson(person);
	}

}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值