1. 在http://www.eclipse.org/eclipselink/downloads/ 下载EclipseLink Installer Zip
在文件夹jlib及其子文件夹jpa下找到jar包:
eclipselink.jar
javax.persistence_2.0.3.v201010191057.jar
2. 在myeclipse中创建一下工程,将上面jar包导入WEB-INF/lib文件夹中
需要用到的jar包:
eclipselink.jar
javax.persistence_2.0.3.v201010191057.jar
aopalliance-alpha1.jar
com.springsource.org.aopalliance-1.0.0.jar
commons-dbcp-1.4.jar
commons-logging-1.1.1.jar
commons-pool-1.5.5.jar
mysql-connector-java-5.1.7-bin.jar
3. 创建实体类和DAO:
实体类Todo.java
@Entity public class Todo { @Id @GeneratedValue private int id; private String summary; private String description; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getSummary() { return summary; } public void setSummary(String summary) { this.summary = summary; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } @Override public String toString() { return "Todo [summary=" + summary + ", description=" + description + "]"; } }
DAO类:public class TodoDAO { private EntityManager em; @PersistenceContext public void setEm(EntityManager em) { this.em = em; } private EntityManager getEntityManager() { return em; } public List<Todo> findAll() { Query query = getEntityManager().createQuery("select s FROM Todo s"); return query.getResultList(); } }
4. 配置文件:
a) 在src文件夹下创建META-INF,并在该文件夹下创建persistence.xml
<?xml version="1.0" encoding="UTF-8" ?>
<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_2_0.xsd"
version="2.0" xmlns="http://java.sun.com/xml/ns/persistence">
<persistence-unit name="myPersistenceUnit" transaction-type="RESOURCE_LOCAL">
<class>jpa.simple.model.Todo</class>
</persistence-unit>
</persistence>
b) 在src文件夹下创建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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
<bean
class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
<bean id="todoDAO" class="jpa.simple.dao.TodoDAO"/>
<bean id="myEmf"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="myPersistenceUnit" />
<property name="dataSource" ref="mydataSource" />
<!--
<property name="persistenceXmlLocation"
value="classpath:my/test/dao/jpa/persistence.xml"/>
-->
<property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml" />
<property name="loadTimeWeaver">
<bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />
</property>
<property name="jpaVendorAdapter">
<bean
class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter">
<property name="generateDdl" value="false" />
<property name="database" value="MYSQL" />
<property name="databasePlatform" value="org.eclipse.persistence.platform.database.MySQLPlatform" />
</bean>
</property>
</bean>
<bean id="mydataSource" 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="sa" />
</bean>
<bean id="myTxManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="myEmf" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>
5. 创建测试类TodoDAOTest.java
public class TodoDAOTest {
@Test
public void test() {
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
TodoDAO ud =(TodoDAO)ctx.getBean("todoDAO");
for (Todo td : ud.findAll()) {
System.out.println(td);
}
}
}
6. 在运行测试案例的时候配置:
7. 运行,成功!
有可能出现的问题:
1. 出现:
org.springframework.beans.factory.BeanCreationException:Error creating bean with name 'myEmf' defined in class path resource[applicationContext.xml]: Invocation of init method failed; nested exception isjava.lang.IllegalStateException: Must start with Java agent to useInstrumentationLoadTimeWeaver. See Spring documentation.
解决:
记得配置运行参数:-javaagent:F:\spring-agent.jar