SSH整合

第一步:引入所需jar
第二步:配置web.xml文件

<!-- 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>

    <!-- Spring配置 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:bean*.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

classpath:bean*.xml指定路径下的文件
第三步:写指定路径下的文件 bean-action.xml bean-service.xml bean-dao.xml
首先配置bean-dao.xml:
Dao配置
<bean id="studao" class="dao.stuDao">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
连接池
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="net.sourceforge.jtds.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:jtds:sqlserver://127.0.0.1:49593/mybysj;charset=gbk;SelectMethod=CURSOR"></property>
<property name="user" value="sa"></property>
<property name="password" value="sa123"></property>
<property name="initialPoolSize" value="3"></property>
<property name="maxPoolSize" value="15"></property>
</bean>
配置sessionFactory
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm1ddl.auto">update</prop>
</props>
</property>
<property name="mappingLocations">
<list>
<value>classpath:hbm/*.hbm.xml</value>
</list>
</property>
</bean>
配置事物
<bean id="txMannger" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
配置事物增强
<tx:advice id="txadvice" transaction-manager="txMannger">
<tx:attributes>
<tx:method name="*" read-only="false"/>
</tx:attributes>
</tx:advice>
配置aop
<aop:config>
<aop:pointcut expression="execution(* service.*.*(..))" id="pt"/>
<aop:advisor advice-ref="txadvice" pointcut-ref="pt"/>
</aop:config>

classpath:hbm/*.hbm.xml 指定这个路径下的映射文件
这里配置连接池,sessionFactory,事物及增强,aop切点表达式

第五步:写pojo和*.hbm.xml

public class Student {
    private String sid;
    private String  name;
    private Integer age;
    private String address;
    public Student(){}
    public String getSid() {
        return sid;
    }
    public void setSid(String sid) {
        this.sid = sid;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }

}

student.hbm.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="entity.Student" table="student">
        <id name="sid" column="s_id">

        </id>
        <property name="name" column="s_name" type="java.lang.String"/>
        <property name="age" column="s_age" />
        <property name="address" column="s_address" type="java.lang.String"/>
    </class>
</hibernate-mapping>

第六步:写bean-action.xml和bean-service.xml
bean-action.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" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">

    <bean id="stuAction" class="action.stuAction">
        <property name="stuservice" ref="stuservice"></property>
    </bean>

</beans>

bean-service.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" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
    http://www.springframework.Forg/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">

    <bean id="stuservice" class="service.stuService">
        <property name="studao" ref="studao"></property>
    </bean>

</beans>

第七步:写struts.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="ture" />
    <constant name="struts.ui.theme" value="simple" />
    <constant name="struts.objectFactory.spring.autoWire" value="no" />     
    <package name="default" namespace="/" extends="struts-default">
        <action name="stu" class="stuAction" method="inertStu">
            <result name="success">text.jsp</result>
        </action>
    </package>
</struts>

整合完毕,在整合期间遇到各种问题,记录一下:
Session session = sessionFactory.getCurrentSession();
用这个方法取session,不能用sessionFactory.getSeeion();因为连接池支持线程
还有就是一定要细心,测试的代码要注释掉,我忘了把
sessionFactory = new Configuration().configure().buildSessionFactory();注释掉,老是报找不到hibernate.cf.xml文件,可是整合的时候没有这个文件啊,所以以后一定要细心;
最后谢谢大家观看。让我们一起进步!

项目工程资源经过严格测试可直接运行成功且功能正常的情况才上传,可轻松复刻,拿到资料包后可轻松复现出一样的项目,本人系统开发经验充足(全领域),有任何使用问题欢迎随时与我联系,我会及时为您解惑,提供帮助。 【资源内容】:包含完整源码+工程文件+说明(如有)等。答辩评审平均分达到96分,放心下载使用!可轻松复现,设计报告也可借鉴此项目,该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的。 【提供帮助】:有任何使用问题欢迎随时与我联系,我会及时解答解惑,提供帮助 【附带帮助】:若还需要相关开发工具、学习资料等,我会提供帮助,提供资料,鼓励学习进步 【项目价值】:可用在相关项目设计中,皆可应用在项目、毕业设计、课程设计、期末/期中/大作业、工程实训、大创等学科竞赛比赛、初期项目立项、学习/练手等方面,可借鉴此优质项目实现复刻,设计报告也可借鉴此项目,也可基于此项目来扩展开发出更多功能 下载后请首先打开README文件(如有),项目工程可直接复现复刻,如果基础还[基于Python]自己写的一个微信跳一跳自动游戏程序(针对安卓手机)。 全自动运行 自动适应不同分辨率 自动调整各个参数误差.zip行,也可在此程序基础上进行修改,以实现其它功能。供开源学习/技术交流/学习参考,勿用于商业用途。质量优质,放心下载使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值