SSH整合练习

SSH整合:
在日常开发中常常需要将几个框架进行整合,而最近因为学习三大框架的原因,学习了怎么使用三大框架,而今天晚上希望自己能够将Struts2与hibernate还有spring整合起来

  1. 引入jar包:
    需要注意的是在SSH整合的项目中需要用的的包主要有以下的的:
    Hibernate的jar包
    Struts2的jar包
    Spring所需要的jar包

  2. 创建实体类,这里的话使用一个部门与员工之间的关系作为一个例子:

/**部门**/

public class Dept implements Serializable {
    private static final long serialVersionUID = 2597890209529538828L;
    private Integer id;
    private String deptName;
    private String desc;
    private Set<Employee> employees = new HashSet<>();
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getDeptName() {
        return deptName;
    }
    public void setDeptName(String deptName) {
        this.deptName = deptName;
    }
    public String getDesc() {
        return desc;
    }
    public void setDesc(String desc) {
        this.desc = desc;
    }
    public Set<Employee> getEmployees() {
        return employees;
    }
    public void setEmployees(Set<Employee> employees) {
        this.employees = employees;
    }
}
/**员工**/
public class Employee implements Serializable {
    private static final long serialVersionUID = -8657205156539906361L;
    private Integer id;
    private String empName;
    private double sarary;
    private Dept dept;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getEmpName() {
        return empName;
    }

    public void setEmpName(String empName) {
        this.empName = empName;
    }

    public double getSarary() {
        return sarary;
    }

    public void setSarary(double sarary) {
        this.sarary = sarary;
    }

    public Dept getDept() {
        return dept;
    }

    public void setDept(Dept dept) {
        this.dept = dept;
    }

}

3 创建实体类的映射文件:

/**Dept**/
<class name="com.yzy.entity.Dept" table="dept">
        <id name="id" type="java.lang.Integer">
            <column name="id" />
            <generator class="native" />
        </id>
        <property name="deptName" type="string"></property>        <property name="desc"type="string" column="_desc">/property>
        <set name="employees" table="employee">
            <key column="deptid"></key>
            <one-to-many class="com.yzy.entity.Employee"/>
        </set>
    </class>
    /**emp**/
    <class name="com.yzy.entity.Employee" table="employee">
        <id name="id" type="java.lang.Integer">
            <column name="id" />
            <generator class="native" />
        </id>
        <property name="empName" type="string"></property>
        <property name="sarary"></property>
        <many-to-one name="dept" class="com.yzy.entity.Dept" column="deptid"></many-to-one>
    </class>

4.创建bean文件

  • bean-base.xml
<!-- c3p0连接池 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
        <property name="jdbcUrl" value="jdbc:mysql:///ceshi"></property>
        <property name="user" value="root"></property>
        <property name="password" value="root"></property>
        <property name="initialPoolSize" value="3"></property>
        <property name="maxPoolSize" value="10"></property>
        <property name="maxStatements" value="100"></property>
        <property name="acquireIncrement" value="2"></property>
    </bean>
    <!-- 由于hibernate的交由spring管理 从而不需要再创建hibernate.cfg.xml -->
    <!-- 配置sesessionfactory -->
   <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
            </props>
        </property>
        <!-- 映射配置 -->
        <property name="mappingLocations">
            <list>
                <value>classpath:com/yzy/entity/*.hbm.xml</value>
            </list>         
        </property>
   </bean>
    <!-- 配置事务管理器 -->
    <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    <!-- 配置事務增强(事务方法) -->
    <tx:advice id="txAdvice" transaction-manager="txManager">
        <tx:attributes>
            <tx:method name="*" read-only="false"/>
        </tx:attributes>
    </tx:advice>
    <!-- Aop配置 -->

    <aop:config>
        <aop:pointcut expression="execution(* com.yzy.service.*.*(..))" id="pt"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="pt"/>
    </aop:config>
  • bean-dao.xml
<bean id="employeeDao" class="com.yzy.dao.EmployeeDao">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
  • bean-service
<bean id="employeeService" class="com.yzy.service.EmployeeService">
        <property name="employeeDao" ref="employeeDao"></property>
    </bean>
  • bean-action.xml
<bean id="empAction" class="com.yzy.action.EmployeeAction" scope="prototype">
        <property name="employeeService" ref="employeeService"></property>
    </bean>

5.配置web.xml

<!--这个地方主要是用来配置jsp上也可以进行懒加载-->
<filter>
        <filter-name>OpenSessionInViewFilter</filter-name>
        <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>OpenSessionInViewFilter</filter-name>
        <url-pattern>*.action</url-pattern>
    </filter-mapping>
    <!-- 配置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>

6.配置Struts.xml

<constant name="struts.action.extension" value="action,do"></constant>
    <package name="default" namespace="/" extends="struts-default">
        <action name="e_*" class="empAction" method="{1}">
            <result name="success">/index.jsp</result>
        </action>
    </package>

这样的话大致就配置好了一个SSH的整合项目,但是在这里的话需要注意的地方有好多:

  1. 需要注意的是Hibernater.cfg.xml是没有的,而是直接有Spring来进行的管理,Spring使用C3P0的连接池获取到dataSource,再将dataSource交给SessionFactory进行管理的。
  2. 由于页面进行不能够进行懒加载,所以在web.xml中进行了设置,从而使得页面可以访问懒加载的数据.
  3. 需要注意事务
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值