JSF2.1+Spring3.1+JPA(Hibernate3.6)配置实例

      首先搭建环境,就不具体说了,我用的是eclipse(不是myeclipse),导入必要的jar包,需要特别注意,Spring的两个AOP的jar包也要倒进去,一开始我认为Aop跟我都应用无关,所以没有导入这两个包,带来不少麻烦。

      这里主要讲配置。有两种情况:Pure JPA + Spring和Spring JpaDaoSupport。这两种情况大同小异,主要是细节上有些差别。

      一、Pure JPA + Spring 四个配置文件分别如下:

     1、applicationContext.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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-3.0.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="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/testcases"/>
<property name="username" value="root"/>
<property name="password" value="beijing123"/>
</bean>

<!-- 这里不需要配置 entityFactory -->

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<!--
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"></bean>
</property>
-->
<property name="dataSource" ref="dataSource" />
<property name="persistenceUnitName" value="testcasemanager"/>
</bean>

<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<!--
<tx:annotation-driven transaction-manager="transactionManager" />
-->

<bean id="atcDao" class="com.semc.conversation.DAO.AutoTestCaseDaoImpl">
</bean>

<bean id="caseManager" class="com.semc.conversation.service.CaseManager">
<property name="atcDao" ref="atcDao"/>
</bean>

</beans>

    2、face-config.xml

<faces-config
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
version="2.0">

<application>

<variable-resolver> org.springframework.web.jsf.DelegatingVariableResolver</variable-resolver>
<!--
<view-handler>com.sun.facelets.FaceletViewHandler</view-handler>
<el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
-->
</application>


<managed-bean>
<managed-bean-name>LoginBean</managed-bean-name>
<managed-bean-class>com.semc.conversation.control.LoginBean</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>

<managed-bean>
<managed-bean-name>coBean</managed-bean-name>
<managed-bean-class>com.semc.conversation.control.CaseOperatorBean</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
<managed-property>
<property-name>caseManager</property-name>
<value>#{caseManager}</value>
</managed-property>
</managed-bean>
</faces-config>

    在这里直接引用了Spring Context中的bean。

    3、persistence.xml(在META-INF路径下)

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" 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_2_0.xsd">

<persistence-unit name="testcasemanager" transaction-type="RESOURCE_LOCAL">

<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.semc.conversation.model.Auto_cases</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLInnoDBDialect"/>
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.hbm2ddl.auto" value="update" />
</properties>
</persistence-unit>
</persistence>

   4.创建工程的时候选择支持JSF,可以在web.xml中自动产生关于JSF的配置,然后在web.xml中加入以下配置:

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>

 

    5、entity

@Entity
@Table(name = "auto_cases")
public class Auto_cases implements Serializable{

private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="id")
private int id;

@Column(name="case_name")
private String case_name;

@Column(name="in_class")
private String in_class;

@Column(name="in_package")
private String in_package;

@Column(name="function_tested")
private String function_tested;

@Column(name="test_steps")
private String test_steps;

@Column(name="covered_manual_case_id")
private String covered_manual_case_id;

@Column(name="comments")
private String comments;
......
geter and setter
}

    6.Dao的实现类

@Repository
@Transactional
public class AutoTestCaseDaoImpl implements AutoTestCaseDao{
    private EntityManager em = null;
    @PersistenceContext
    public void setEntityManager(EntityManager em) {
        this.em = em;
    }

    @Override
    public void addCase(Auto_cases atc) {
        try{ 
            em.persist(atc);
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}

   7. testcase:

public class AutoTestCaseDaoImplTest{

private static ApplicationContext ctx;
private static AutoTestCaseDao atcd ;

@Before //在每个测试用例方法之前都会执行
public void init(){
// ctx=new ClassPathXmlApplicationContext("WEB-INF/applicationContext.xml");

String path="WebContent/WEB-INF/applicationContext.xml";
ctx = new FileSystemXmlApplicationContext(path);
atcd=(AutoTestCaseDao)ctx.getBean("atcDao");
}

@After //在每个测试用例执行完之后执行
public void destory(){
}

@Test
public void testAdd(){

for(int i=0;i<3;i++){
Auto_cases atc = new Auto_cases();
atc.setCase_name("Auto test case "+i);
atc.setComments("comments "+i);
atc.setCovered_manual_case_id(String.valueOf(626722+i));
atc.setFunction_tested("function "+i);
atc.setIn_class("ConversationListActivity");
atc.setIn_package("com.sonyericsson.conversations");
atc.setTest_steps("no steps");
atcd.addCase(atc);
}
assertTrue(atcd.count()==3);
}
}

      在这种情况下,Dao所需要的entityManager是通过Dao的实现类中的setter方法注入的,而不是通过在applicationContext.xml中Dao的配置注入的。

 

      二、JPA+JpaDaoSuppert

      除了以下两点,其它与前面的情况都一样:

      1、Dao的实现类

@Repository
@Transactional
public class AutoTestCaseDaoImpl extends JpaDaoSupport implements AutoTestCaseDao{ 
   @Override
    public void addCase(Auto_cases atc) {
        logger.info("saving Player instance");
        try {
            getJpaTemplate().persist(atc);
            logger.info("save successful");
        } catch (RuntimeException re) {
            logger.error("save failed", re);
            throw re;
     }
}
}

 

      2、在applicationContext.xml中,通过对Dao的配置注入EntityManager

 

<bean id="atcDao" class="com.semc.conversation.DAO.AutoTestCaseDaoImpl">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>

 

 

你可能感兴趣的:(hibernate3)