Spring 4.2.5 + Hibernate 4.3.11 + JPA + tomcat 集成

102 篇文章 2 订阅
9 篇文章 0 订阅

Spring 4.2.5 + Hibernate 4.3.11 + JPA + tomcat 集成 。本人在eclipse 4.4 上以run on server (Tomcat) 调试通过。


配置文件为:

applicationContext.xml

<?xml version="1.0" encoding="GBK"?>
<!-- Spring配置文件的根元素,使用spring-beans-4.0.xsd语义约束 -->
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:cache="http://www.springframework.org/schema/cache" xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-4.0.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
       http://www.springframework.org/schema/cache 
       http://www.springframework.org/schema/cache/spring-cache-4.0.xsd">
       
     <!-- 定义数据源 bean, 使用3P0  -->
<bean id="ds" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<!-- 指定连接数据库的驱动 -->
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<!-- 指定连接数据库的URL -->
<property name="jdbcUrl" value="jdbc:mysql://localhost/javaee"/>
<!-- 指定连接数据库的用户名 -->
<property name="user" value="root"/>
<!-- 指定连接数据库的密码 -->
<property name="password" value="root"/>
<!-- 指定连接数据库连接池的最大连接数 -->
<property name="maxPoolSize" value="40"/>
<!-- 指定连接数据库连接池的最小连接数 -->
<property name="minPoolSize" value="1"/>
<!-- 指定连接数据库连接池的初始化连接数 -->
<property name="initialPoolSize" value="1"/>
<!-- 指定连接数据库连接池的连接的最大空闲时间 -->
<property name="maxIdleTime" value="20"/>
</bean>
<!-- 定义DAO Bean-->
<bean id="userDao" class="app.dao.impl.UserDaoHibernate">
<!-- 注入持久化操作所需的SessionFactory,只能手动注入, name="sessionFactory " 在 HibernateDaoSupport中 -->
<property name="emf" ref="entityManagerFactory"/>
</bean>
<!-- 定义一个业务逻辑组件,实现类为app.service.impl.MyAuthImplBean -->
<bean id="myService" class="app.service.impl.MyAuthImplBean">
<property name="userDao" ref="userDao"/>
</bean>
<!-- 让Spring管理的Action实例 -->
<bean id="springloginAction" class="app.action.LoginAuthAction"
scope="prototype">
<!-- 依赖注入业务逻辑组件 -->
<property name="ms" ref="myService"/>
</bean>
<bean id="jpaVendorAdapter" 
        class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
         <property name="database" value="MYSQL"></property>
         <property name="showSql" value="true" />
         <property name="databasePlatform" value="org.hibernate.dialect.MySQL5Dialect" />
         <!--  相当于  <prop key="hibernate.hbm2ddl.auto">update</prop> -->
         <property name="generateDdl" value="true" /> 
     </bean>
<bean id="entityManagerFactory" 
     class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" >
     <property name="dataSource" ref="ds"></property>
          <property name="persistenceXmlLocation" value="classpath*:/persistence.xml" />
          <property name="persistenceUnitName" value="user_unit"/> 
          <property name="jpaVendorAdapter" ref="jpaVendorAdapter"></property>
      </bean>
       
     <bean id="jpaDialect"
        class="org.springframework.orm.jpa.vendor.HibernateJpaDialect">
    </bean>
     <bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager"> 
           <property name="entityManagerFactory" ref="entityManagerFactory"/> 
           <property name="jpaDialect" ref="jpaDialect"></property>
     </bean> 
     
      <!-- Bean's use annotation (@Transactional) to demark transaction  -->
      <tx:annotation-driven transaction-manager="txManager"/>
</beans>





persistence.xml


<?xml version="1.0" encoding="GBK"?>
<persistence version="1.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_1_0.xsd">
<!-- 为持久化单元指定名称,并通过transaction-type指定事务类型
transaction-type属性合法的属性值有JTA、RESOURCE_LOCAL两个-->
<persistence-unit name="user_unit" transaction-type="RESOURCE_LOCAL">
<!-- 列出该应用需要访问的所有的Entity类, 
也可以用 <mapping-file> 或<jar-file>元素来定义 -->
<class>app.domain.User</class>
</persistence-unit>
</persistence>


struts.xml


<?xml version="1.0" encoding="GBK"?>
<!-- 指定Struts 2配置文件的DTD信息 -->
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
"http://struts.apache.org/dtds/struts-2.1.7.dtd">
<!-- Struts 2配置文件的根元素 -->
<struts>
<!-- 配置了系列常量 -->
<constant name="struts.i18n.encoding" value="GBK"/>
<constant name="struts.devMode" value="true"/>
<package name="MySpringAndStrutsAndHibernate" extends="struts-default">
<!-- 定义处理用户请求的Action,该Action的class属性不是实际处理类
, 而是Spring容器中的Bean实例-->
<action name="fakeloginAuth" class="springloginAction">
<!-- 为两个逻辑视图配置视图页面 -->
<result name="error">/WEB-INF/content/error.jsp</result>
<result name="success">/WEB-INF/content/welcome.jsp</result>
</action>
<!-- 让用户直接访问该应用时列出所有视图页面 -->
<action name="*">
<result>/WEB-INF/content/{1}.jsp</result>
</action>
</package>
</struts>


web.xml

<?xml version="1.0" encoding="GBK"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener
</listener-class>
  </listener>
  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值