关闭

spring struts hibernate 集成

851人阅读 评论(0) 收藏 举报
前几天做了一个小项目,用到了这三个东西,现在我把它们的集成过程写下来,只是一个样本,备查!
 struts 的action 和hibernate和DAO都由spring进行装配的。

1.写三个spring的配置文件,分别用于连接数据库,装配DAO,装配action
------------------------------------------
spring-base.xml(这个是基本配置,里面存放数据库的连接等信息)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>

    <bean id="dataSource"
        class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass">
         <value>com.mysql.jdbc.Driver</value>
        </property>
        <property name="jdbcUrl">
         <value>jdbc:mysql://localhost:3306/yitai</value>
        </property>
            <property name="properties">
                  <props>
                          <prop key="c3p0.minPoolSize">10</prop>
                          <prop key="hc3p0.maxPoolSize">20</prop>
                          <prop key="hc3p0.timeout">60</prop>
                          <prop key="c3p0.max_statement">50</prop>
                          <prop key="c3p0.testConnectionOnCheckout">true</prop> 
                          <prop key="user">root</prop>
                          <prop key="password">1234</prop>
                  </props>
        </property>
    </bean>
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource">
            <ref bean="dataSource" />
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">
                    org.hibernate.dialect.MySQLDialect
                </prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>
        <property name="mappingResources">
            <list>
                <value>com/gosure/model/dealer/Sales.hbm.xml</value>
                </list>
        </property></bean>
   

     </beans>
--------------------------------------
spring-dao.mxl
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>


    <bean id="SalesDAO" class="com.gosure.dao.dealer.SalesDAO">
        <property name="sessionFactory">
            <ref bean="sessionFactory" />
        </property>
    </bean>
</beans>
-------------------------------------
spring-action.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>
   
     <!-- list goods -->
     <bean name="/dealer/goodsList" class="com.gosure.struts.action.dealer.GoodslistAction">
<!--以下可以用 autowire="byType"-->
        <property name="goodsDao">
            <ref bean="GoodsDAO" />
        </property>
     </bean>
     </beans>
============================================================
struts-config.xml
........  
<action path="/guide" type="org.springframework.web.struts.DelegatingActionProxy" >
      <forward name="orderWay" path="/../WEB-INF/dealer/orderWay.jsp" />
      <forward name="afterSevice" path="/../WEB-INF/dealer/afterSevice.jsp" />
    </action>
.......
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
    <set-property property="contextConfigLocation" value="/WEB-INF/classes/spring-base.xml,/WEB-INF/classes/spring-dealerDAO.xml,/WEB-INF/classes/spring-dealer.xml"/>
 </plug-in>





//TODO 先写到这,以后做详细说明!!!
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:13045次
    • 积分:235
    • 等级:
    • 排名:千里之外
    • 原创:9篇
    • 转载:3篇
    • 译文:0篇
    • 评论:1条
    最新评论