SSH框架整合的基本步骤

SSH框架整合的基本步骤
1.加入Spring
1).加入jar包
这里写图片描述
2).配置Web.xml文件

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

3 ).加入Spring配置文件

2.加入Hibernate
1).同时建立持久化类,和其对应的.hbm.xml文件,生成对应的数据表
2).Spring 整合Hibernate
3).步骤:
①.加入jar包
这里写图片描述
②.在类路径下加入hibernate.cfg.xml文件,在其中配置hibernate的基本属性(方言、是否显示及格式化SQL、生成数据的策略、二级缓存)
③.建立持久化类,和对应的.hbm.xml文件
④.和Spring进行整合

Ⅰ.加入c3p0的驱动
erro:can’t find com.mchange.v2.c3p0.ComboPooledDataSource!
Q:版本0.9.2.1的C3P0jar包需要培mchang-commons-java 的jar包。
Ⅱ.在Spring的配置文件中配置:
1)、数据源(写好资源文件db.properties)
mysql的账户、密码、url、driverclass、初始化连接池

    <!-- 导入资源文件 -->
    <context:property-placeholder location="classpath:db.properties" />

    <!-- 配置C3P0数据源 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="user" value="${jdbc.user}"></property>
        <property name="password" value="${jdbc.password}"></property>
        <property name="driverClass" value="${jdbc.driverClass}"></property>
        <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>


        <property name="initialPoolSize" value="${jdbc.initialPoolSize}"></property>
        <property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
    </bean>

2)、SessionFactory
加载数据源、加载配置文件、加载映射对象

    <!-- 配置SessionFactory -->
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <!-- 数据库连接 -->
        <property name="dataSource" ref="dataSource"></property>
        <!-- 加载资源文件 -->
        <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
        <!-- 加载映射对象 -->
<property name="mappingLocations" value="classpath:com/silver/ssh/entities/*.hbm.xml"></property>
</bean>

3)、声明式事务
1配置hibernate的事务管理器
2配置事务属性
3 配置书屋切入点,事务属性关联事务切入点

<!-- 配置Spring的声明式事务 -->
    <!-- 1配置hibernate的事务管理器 -->
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>

    <!-- 2配置事务属性 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="get*" read-only="true" />
            <tx:method name="lastNameIsValid"  read-only="true" />
            <tx:method name="*" />
        </tx:attributes>
    </tx:advice>

    <!--3 配置书屋切入点,事务属性关联事务切入点 -->
    <aop:config>
        <aop:pointcut expression="execution(* com.silver.ssh.service.*.*(..))"
            id="txPointcut" />
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut" />
    </aop:config>

⑤.启动项目,生成对应的数据表

3.加入Struts2
1).加入jar包 :若有重复jar包删除版本较低。
这里写图片描述
2).在web.xml 文件中配置Filter

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

3).加入Struts2的配置文件

<constant name="struts.enabl.DynamicMethodInvocation" value="true"></constant>
    <constant name="struts.devMode" value="ture" />
    <package name="default" namespace="/" extends="struts-default">

4).整合Spring
①.加入Struts2的Spring插件的jar包
struts2-spring-plugin.jar
②.在spring中正常配置Action ,注意Action的scope为prototype (多例模式)
③.在struts2的配置文件中配置Action时,class属性指向该 Action 在 IOC 中的id(注意匹配)

4.完成功能

1).获取所有员工信息:若在Dao 中只查询Employee 的信息,而且Employee 的Department
还是使用的懒加载,页面上还需要显示员工信息,此时会出现懒加载异常;
.org.hibernate.LazyInitializationException: could not initia

解决:
①.打开懒加载:不推荐(效率低)
②.获取Employee 时使用 迫切左外连接同时初始化其关联的Employee对象
③.使用OpenSessionInViewFilter:

2).删除员工信息
①。正常删除,返回值要是redirect类型,重定向到emp-list
②。使用jquery完成删除确认
PS:jquery的JS文件不能放在web-inf目录下
③。Ajax使用参照struts-2.3.29/docs/docs/ajax.html

3).添加员工:
① 、使用Struts2 的ModelDriven 和Preparable
② 、在前台页面传入输入值到值栈
③ 、时间是一个字符串,转化为Date。

4).修改员工:
一、在界面中添加隐藏域,用以储存未更改的值
二、在后台创建Model时从数据库读取,然后仅更改变更部分

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值