關於struts2+spring3+jpa專案的構建

bill1315發表於2012-09-03

環境:eclipse  Juno

ssh的版本:

 
 專案的結構如下:
 
其中: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"
 xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd">
    <!--以下Service採用自動注入-->
    <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
    <!--在Dao層使用@PersistenceContext注入實體管理器-->
    <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>
   
<!--jpa支援-->
 <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  <property name="locations">
   <list>
    <value>classpath:jdbc.properties</value>
   </list>
  </property>
 </bean>
 <bean id="dataSource"
  class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  <property name="driverClassName" value="${jdbc.driver}" />
  <property name="url" value="${jdbc.url}" />
  <property name="username" value="${jdbc.username}" />
  <property name="password" value="${jdbc.password}" />
 </bean>
 <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
  <property name="persistenceUnitName" value="jpaUnit"/>
        <property name="dataSource" ref="dataSource"/>
  <property name="persistenceXmlLocation" value="classpath:persistence.xml" />
  <property name="jpaVendorAdapter">
    <bean
     class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
     <property name="databasePlatform" value="org.hibernate.dialect.SQLServerDialect"/>
                    <property name="generateDdl" value="true"/>
     <property name="showSql" value="true" />
    </bean>
  </property>
 </bean>
 
 <bean id="transactionManager"
  class="org.springframework.orm.jpa.JpaTransactionManager">
  <property name="entityManagerFactory" ref="entityManagerFactory" />
 </bean>

 <bean id="userDAO" class="com.ssh.rbac.dao.impl.RbacUserDaoImpl"/>
    <bean id="userService" class="com.ssh.rbac.service.impl.RbacUserServiceImpl"/>
   
</beans>

persistence.xml如下:
 
 <?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/persistencehttp://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
 <persistence-unit name="jpaUnit"
 transaction-type="RESOURCE_LOCAL">
  <provider>org.hibernate.ejb.HibernatePersistence</provider>
  <class>com.dreamity.rbac.domain.RbacUser</class>
  <properties/>
 </persistence-unit>
</persistence>
 
 jdbc.properties如下:
jdbc.driver=com.microsoft.sqlserver.jdbc.SQLServerDriver
jdbc.url=jdbc:sqlserver://localhost:1456;databaseName=DreamityFramework
jdbc.username=sa
jdbc.password=nariacc
hibernate.dialect=org.hibernate.dialect.SQLServerDialect
hibernate.show_sql=true
 

相關文章