ssh框架配置

1. 首先整合Spring

  1. 先加入jar包
  2. web.xml中配置spring的核心监听器,并配置applicationContext.xml文件
        <!-- 1. 配置spring的核心监听器 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <!-- 配置spring的applicationContext.xml -->
  <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
  </context-param>

2. 整合hibernate

  1. 加入jar包
  2. 创建实体化类和其对应的***hbm.xml文件
    xxx.hbm.xml文件:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="[包名.实体类名]" table="[对应的表名]">
        <id name="[实体类的对应主键]" column="[字段主键]" length="[字段长度]" type="java.lang.Integer">
            <generator class="native"></generator>
        </id>
        <property name="[实体类成员变量]" column="[对应的字段]" length="[字段长度]" type="[字段类型]"/>
        ....
    </class>
</hibernate-mapping>
  1. 配置hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <!-- 配置 hibernate 的基本属性 -->
        <!-- 方言 -->
        <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
        <!-- 是否显示及格式化 SQL -->
        <property name="hibernate.show_sql">true</property>
        <property name="hibernate.format_sql">true</property>
        <!-- 生成数据表的策略 -->
        <property name="hibernate.hbm2ddl.auto">update</property>
        <!-- 二级缓存相关 -->
    </session-factory>
</hibernate-configuration>
  1. 配置db.properties
jdbc.name=root
jdbc.password=123456
jdbc.url=jdbc:mysql://127.0.0.1:3306/ssh2
jdbc.driver=com.mysql.jdbc.Driver

jdbc.initPoolSize=5
jdbc.maxPoolSize=10

4.加入spring的配置文件,并配置spring的事务管理

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.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">

       <!-- 导入db.properties的文件 -->
       <context:property-placeholder location="classpath:db.properties" />
       <!-- 配置c3p0 -->
       <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
            <property name="user" value="${jdbc.name}"></property>
            <property name="password" value="${jdbc.password}"></property>
            <property name="driverClass" value="${jdbc.driver}"></property>
            <property name="jdbcUrl" value="${jdbc.url}"></property>

            <property name="initialPoolSize" value="${jdbc.initPoolSize}"></property>
            <property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
       </bean>
       <!-- 配置sessionFactory -->
       <bean name="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
            <property name="dataSource" ref="dataSource" />
            <property name="configLocation" value="classpath:hibernate.cfg.xml" />
            <property name="mappingLocations" value="classpath:com/bart/ssh/entity/*.hbm.xml"/>
       </bean>
       <!-- 配置spring的声明式事务管理 -->       
       <!--1. 配置 hibernate的事务管理  -->
       <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
            <property name="sessionFactory" ref="sessionFactory"/>
       </bean>
       <!--2. 事务管理 -->
       <tx:advice id="txAdvice" transaction-manager="transactionManager">
            <tx:attributes>
                <tx:method name="get*" read-only="true"/>
                <tx:method name="*" />
            </tx:attributes>
       </tx:advice>
       <!--3. 配置事务切入点,再把事属性和事务切入点关联起来 -->
       <aop:config>
            <aop:pointcut expression="execution(* com.bart.ssh.service.*.*(..))" id="txPoint"/>
            <aop:advisor pointcut-ref="txPoint" advice-ref="txAdvice"/>
       </aop:config>
</beans>

此时spring和hibernate整合完毕,运行tomcat服务器,这个时候对应数据中就会产生相对应的表了

3. 整合struts2

  1. 在web.xml中配置struts2的核心过滤器
<!-- 2. 配置struts2过滤器 -->
  <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>
  1. 加入struts2的核心配置文件
    struts2.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>

    <constant name="struts.enable.DynamicMethodInvocation" value="false" />
    <constant name="struts.devMode" value="true" />

    <package name="default" namespace="/" extends="struts-default">
        <result name="xxx" >xxx.jsp</reault>        
    </package>

</struts>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值