spring配置文件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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" 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.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd "> <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean"> <property name="persistenceUnitName" value="SpringJpaPU" /> </bean> <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="entityManagerFactory" ref="entityManagerFactory" /> </bean> <tx:annotation-driven transaction-manager="transactionManager" /> <context:annotation-config></context:annotation-config> <bean id="personDao" class="org.pxw.dao.imple.PersonDaoImple"> <property name="entityManagerFactory" ref="entityManagerFactory"></property> </bean> <bean id="personService" class="org.pxw.service.imple.PersonServiceImple"></bean> </beans>
jpa的数据源配置persistence.xml用eclipse自动生成会在src下创建一个META-INF文件下:
<?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="SpringJpaPU" transaction-type="RESOURCE_LOCAL"> <provider>org.hibernate.ejb.HibernatePersistence</provider> <class>org.pxw.entity.Person</class> <properties> <property name="hibernate.connection.driver_class" value="oracle.jdbc.driver.OracleDriver" /> <property name="hibernate.connection.url" value="jdbc:oracle:thin:@localhost:1521:orcl" /> <property name="hibernate.connection.username" value="scott" /> <property name="hibernate.connection.password" value="tiger" /> <property name="hibernate.show_sql" value="true" /> <!-- 格式化显示的SQL --> <property name="hibernate.format_sql" value="true" /> <property name="hibernate.hbm2ddl.auto" value="update" /> </properties> </persistence-unit> </persistence>
PersonDaoImple.java代码:
import java.util.List; import org.pxw.dao.PersonDao; import org.pxw.entity.Person; import org.springframework.orm.jpa.support.JpaDaoSupport; public class PersonDaoImple extends JpaDaoSupport implements PersonDao { @Override public List<Person> findall() { return getJpaTemplate().find("select p from Person p"); } }
PersonService.java代码:
import java.util.List; import javax.annotation.Resource; import org.pxw.dao.PersonDao; import org.pxw.entity.Person; import org.pxw.service.PersonService; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; @Transactional(propagation=Propagation.REQUIRED,readOnly=false) public class PersonServiceImple implements PersonService { @Resource(name="personDao") private PersonDao personDao; @Override public List<Person> findall() { // TODO Auto-generated method stub return personDao.findall(); } }
测试代码:
public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); PersonService personService = (PersonService)context.getBean("personService"); List<Person> persons = personService.findall(); System.out.println(persons.size()); for (Person person : persons) { System.out.println(person.getPname()); }