SSH整合

1 篇文章 0 订阅

项目结构

Dept类提供 属性 与 get/set方法

提供 hibernate映射配置文件


<hibernate-mapping package="com.xt.entity">
    <class name="Dept">
        <id name="deptNo">
            <generator class="native"></generator>
        </id>
        <property name="loc"></property>
        <property name="dName"></property>
    </class>
</hibernate-mapping>

dao类

public class DeptDaoImpl implements DeptDaoI {
    private SessionFactory sessionFactory;

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    @Override
    public List<Dept> getAll() {
        Session session = sessionFactory.getCurrentSession();
        List<Dept> list = session.createCriteria(Dept.class).list();
        return list;
    }
}

service类

public class DeptServiceImpl implements DeptServiceI {
    private DeptDaoI deptDaoI;

    public void setDeptDaoI(DeptDaoI deptDaoI) {
        this.deptDaoI = deptDaoI;
    }


    @Override
    public List<Dept> getAll() {
        return deptDaoI.getAll();
    }
}

action类

public class DeptAction extends ActionSupport {

    private DeptServiceI deptServiceI;

    public void setDeptServiceI(DeptServiceI deptServiceI) {
        this.deptServiceI = deptServiceI;
    }

    @Override
    public String execute() throws Exception {
        List<Dept> list = deptServiceI.getAll();
        System.out.println( list );
        ActionContext.getContext().getContextMap().put("list",list);
        return super.execute();
    }
}

spring配置文件

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans.xsd
         http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context.xsd
         http://www.springframework.org/schema/aop 
         http://www.springframework.org/schema/aop/spring-aop.xsd
         http://www.springframework.org/schema/tx 
         http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!-- 配置连接池 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test"></property>
        <property name="user" value="root"></property>
        <property name="password" value="root"></property>
        <property name="maxPoolSize" value="6"></property>
        <property name="initialPoolSize" value="3"></property>
    </bean>

    <!-- 配置sessionFactory -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <!-- 设置dataSource -->
        <property name="dataSource" ref="dataSource"></property>
        <!-- 配置 hibernate -->
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">true</prop>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
            </props>
        </property>
        <!-- 扫描 映射文件-->
        <property name="mappingLocations">
            <array>
                <value>classpath:com/xt/entity/*hbm.xml</value>
            </array>
        </property>
    </bean>

    <!-- 配置 dao -->
    <bean id="deptDao" class="com.xt.dao.impl.DeptDaoImpl">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>

    <!-- 配置service类 -->
    <bean id="deptService" class="com.xt.service.impl.DeptServiceImpl">
        <property name="deptDaoI" ref="deptDao"></property>
    </bean>

    <!-- 配置action类 -->
    <bean id="deptAction" class="com.xt.action.DeptAction">
        <property name="deptServiceI" ref="deptService"></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 id="pt" expression="execution( * com.xt.service.impl.*.*(..))"></aop:pointcut>
        <!-- 应用通知 + 切入点 -->
        <aop:advisor advice-ref="txAdvice" pointcut-ref="pt"></aop:advisor>
    </aop:config>
</beans>

struts配置文件


<struts>
    <package name="dept" extends="struts-default">
        <action name="getAll" class="deptAction">
            <result>/index.jsp</result>
        </action>
    </package>

</struts>

web.xml配置

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

    <!-- ioc 容器交给 tomcat启动-->
    <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>

list.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
  <head>
    <title>$Title$</title>
  </head>
  <body>
    <c:forEach items="${list}" var="list">
        ${list} <br/>
    </c:forEach>
  </body>
</html>

运行效果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值