美文网首页
【Spring】SSH三大框架整合

【Spring】SSH三大框架整合

作者: 日落perfe | 来源:发表于2017-07-21 17:23 被阅读0次

数据库信息配置

*在src目录下新建db.properties文件

jdbc.jdbcUrl=jdbc:mysql:///mycrm
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.user=root
jdbc.password=123

Spring集成Hibernate和C3P0

*在src目录下新建applicationContext.xml文件

<!-- 读取db.properties文件 -->
    <context:property-placeholder location="classpath:db.properties" />
    <!-- 配置c3p0连接池 -->
    <bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
        <property name="driverClass" value="${jdbc.driverClass}"></property>
        <property name="user" value="${jdbc.user}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>
<!-- 核心事务管理器 -->
    <bean name="transactionManager"
        class="org.springframework.orm.hibernate5.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    <!-- 开启注解事务 -->
    <tx:annotation-driven transaction-manager="transactionManager" />

    <!-- 将SessionFactory配置到spring容器中 -->
    <bean name="sessionFactory"
        class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <!-- 将连接池注入到sessionFactory, hibernate会通过连接池获得连接 -->
        <property name="dataSource" ref="dataSource"></property>
        <!-- 配置hibernate基本信息 -->
        <property name="hibernateProperties">
            <props>
                <!-- 必选配置 -->
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect
                </prop>

                <!-- 可选配置 -->
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
            </props>
        </property>
        <!-- 引入orm元数据,指定orm元数据所在的包路径,spring会自动读取包中的所有配置 -->
        <property name="mappingDirectoryLocations" value="classpath:cn/itcast/domain"></property>
    </bean>

Spring与Struts2集成

*在src目录下新建struts.xml文件

<struts>
    <!-- #  struts.objectFactory = spring   将action的创建交给spring容器    
            struts.objectFactory.spring.autoWire = name spring负责装配Action依赖属性
            -->
    <constant name="struts.objectFactory" value="spring"></constant>

    <package name="crm" namespace="/" extends="struts-default" >
        <global-exception-mappings>
            <exception-mapping result="error" exception="java.lang.RuntimeException"></exception-mapping>
        </global-exception-mappings>
         
         <!-- 
            整合方案2:class属性上填写spring中action对象的BeanName
                完全由spring管理action生命周期,包括Action的创建
                注意:需要手动组装依赖属性
          -->
        <action name="UserAction_*" class="userAction" method="{1}" >
            <result name="toHome" type="redirect" >/index.html</result>
            <result name="error" >/login.jsp</result>
        </action>
    </package>
</struts>

Spring中注册相关bean

<!-- action -->
    <bean name="userAction" class="cn.itcast.web.action.UserAction"
        scope="prototype">
        <property name="userService" ref="userService"></property>
    </bean>

    <!-- service -->
    <bean name="userService" class="cn.itcast.service.impl.UserServiceImpl">
        <property name="ud" ref="userDao"></property>
    </bean>

    <!-- dao -->
    <bean name="userDao" class="cn.itcast.dao.impl.UserDaoImpl">
        <!-- 注入sessionFactory -->
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>

相关文章

网友评论

      本文标题:【Spring】SSH三大框架整合

      本文链接:https://www.haomeiwen.com/subject/gcvekxtx.html