spring jpa测试

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());
             
        }

 

 

 

 

 

 

 

转载于:https://www.cnblogs.com/b422/archive/2012/06/04/spring_jpa.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值