zoukankan      html  css  js  c++  java
  • spring 之 ssh整合

    struts2,spring,hibernate整合步骤如下:

    1. 新建web项目
    2. 导入jar包
      antlr-2.7.7.jar
      aopalliance.jar
      asm-3.3.jar
      asm-commons-3.3.jar
      asm-tree-3.3.jar
      aspectjweaver.jar
      c3p0-0.9.2.1.jar
      commons-fileupload-1.2.2.jar
      commons-io-2.0.1.jar
      commons-lang3-3.1.jar
      commons-logging.jar
      dom4j-1.6.1.jar
      freemarker-2.3.19.jar
      hibernate-c3p0-4.3.10.Final.jar
      hibernate-commons-annotations-4.0.5.Final.jar
      hibernate-core-4.3.10.Final.jar
      hibernate-jpa-2.1-api-1.0.0.Final.jar
      jandex-1.1.0.Final.jar
      javassist-3.18.1-GA.jar
      jboss-logging-3.1.3.GA.jar
      jboss-logging-annotations-1.2.0.Beta1.jar
      jboss-transaction-api_1.2_spec-1.0.0.Final.jar
      mchange-commons-java-0.2.3.4.jar
      mysql-connector-java-5.1.20-bin.jar
      ognl-3.0.5.jar
      spring-aop-4.1.6.RELEASE.jar
      spring-aspects-4.1.6.RELEASE.jar
      spring-beans-4.1.6.RELEASE.jar
      spring-context-4.1.6.RELEASE.jar
      spring-core-4.1.6.RELEASE.jar
      spring-expression-4.1.6.RELEASE.jar
      spring-jdbc-4.1.6.RELEASE.jar
      spring-orm-4.1.6.RELEASE.jar
      spring-tx-4.1.6.RELEASE.jar
      spring-web-4.1.6.RELEASE.jar
      spring-webmvc-4.1.6.RELEASE.jar
      struts2-core-2.3.4.jar
      struts2-spring-plugin-2.3.4.jar
      xwork-core-2.3.4.jar
    3. 新建vo类:
      @Entity
      @Table(name="t_user")
      public class User implements Serializable{
          @Id
          @GeneratedValue(strategy=GenerationType.AUTO)
          private int id;
          private String name;
          private int age;
          public int getId() {
              return id;
          }
          public void setId(int id) {
              this.id = id;
          }
          public String getName() {
              return name;
          }
          public void setName(String name) {
              this.name = name;
          }
          public int getAge() {
              return age;
          }
          public void setAge(int age) {
              this.age = age;
          }
      }
    4. 新建dao类:
      public class UserDaoImpl implements UserDao{
          private SessionFactory sessionFactory;
          public List<User> findAll() {
              return sessionFactory.getCurrentSession().createQuery("from User").list();
          }
          public int save(User user) {
              try {
                  sessionFactory.getCurrentSession().save(user);
                  return 1;
              } catch (HibernateException e) {
                  e.printStackTrace();
              }
              return -1;
          }
          public int delete(User user) {
              try {
                  sessionFactory.getCurrentSession().delete(user);
                  return 1;
              } catch (HibernateException e) {
                  e.printStackTrace();
              }
              return -1;
          }
          public void setSessionFactory(SessionFactory sessionFactory) {
              this.sessionFactory = sessionFactory;
          }
      }
    5. 新建service类:
      public class UserServiceImpl implements UserService {
          private UserDao userDao;
          public List<User> findAll() {
              return userDao.findAll();
          }
          public int save(User user) {
               return userDao.save(user);
          }
          public void setUserDao(UserDao userDao) {
              this.userDao = userDao;
          }
      }
    6. 新建action类:
      public class UserAction {
          private List<User> list;
          private UserService userService;
          public String list(){
              list = userService.findAll();
              return Action.SUCCESS;
          }
          public List<User> getList() {
              return list;
          }
          public void setList(List<User> list) {
              this.list = list;
          }
          public UserService getUserService() {
              return userService;
          }
          public void setUserService(UserService userService) {
              this.userService = userService;
          }
      }
    7. 配置spring的配置文件:ApplicationContext-core.xml
      <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: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.xsd 
              http://www.springframework.org/schema/tx
              http://www.springframework.org/schema/tx/spring-tx.xsd">
          <!-- 整合hibernate第一种及配置方式 
          <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
              <property name="configLocation" value="classpath:hibernate.cfg.xml"/>
          </bean>
          -->
          <!-- 整合hibernate第二种方式 -->
          <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
              <property name="location" value="classpath:db.properties"/>
          </bean>
          <!-- 配置数据源 
          <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
              <property name="driverClassName" value="${driver}"/>
              <property name="url" value="${url}"/>
              <property name="username" value="${username}"/>
              <property name="password" value="${password}"/>
          </bean>-->
          <!-- 配置c3p0连接池 -->
          <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
              <property name="driverClass" value="${driver}"></property>
              <property name="jdbcUrl" value="${url}"></property>
              <!--用户名。Default: null-->
              <property name="user" value="${username}"></property>
              <!--密码。Default: null-->
              <property name="password" value="${password}"></property>
              <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
              <property name="acquireIncrement" value="3"></property>
              <!--定义在从数据库获取新连接失败后重复尝试的次数。Default: 30 -->
              <property name="acquireRetryAttempts" value="30"></property>
              <!--两次连接中间隔时间,单位毫秒。Default: 1000 -->
              <property name="acquireRetryDelay" value="1000"></property>
              <!--连接关闭时默认将所有未提交的操作回滚。Default: false -->
              <property name="autoCommitOnClose" value="false"></property>
              <!--获取连接失败将会引起所有等待连接池来获取连接的线程抛出异常。但是数据源仍有效
              保留,并在下次调用getConnection()的时候继续尝试获取连接。如果设为true,那么在尝试
              获取连接失败后该数据源将申明已断开并永久关闭。Default: false-->
              <property name="breakAfterAcquireFailure" value="false"></property>
              
              <!--当连接池用完时客户端调用getConnection()后等待获取新连接的时间,超时后将抛出
              SQLException,如设为0则无限期等待。单位毫秒。Default: 0 -->
              <property name="checkoutTimeout" value="100"></property>
              <!--每60秒检查所有连接池中的空闲连接。Default: 0 -->
              <property name="idleConnectionTestPeriod" value="60"></property>
              <!--初始化时获取三个连接,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
              <property name="initialPoolSize" value="3"></property>
              <!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
              <property name="maxIdleTime" value="60"></property>
              <!--连接池中保留的最大连接数。Default: 15 -->
              <property name="maxPoolSize" value="15"></property>
              
              <!--c3p0是异步操作的,缓慢的JDBC操作通过帮助进程完成。扩展这些操作可以有效的提升性能
              通过多线程实现多个操作同时被执行。Default: 3-->
              <property name="numHelperThreads" value="3"></property>
          </bean>
          <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
              <property name="dataSource" ref="dataSource"/>
              <!-- 映射文件映射 
              <property name="mappingLocations">
                  <list>
                      <value>classpath:cn/wh/vo/User.hbm.xml</value>
                  </list>
              </property>
              -->
              <property name="annotatedClasses">
                  <list>
                      <value>cn.wh.vo.User</value>
                  </list>
              </property>
              <property name="hibernateProperties">
                  <props>
                      <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                      <prop key="hibernate.show_sql">true</prop>
                      <prop key="hibernate.format_sql">true</prop>
                  </props>
              </property>
          </bean>
          <!-- 声明式事务的配置 -->
          <!-- 事务管理器 
          <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
              <property name="dataSource" ref="dataSource"/>
          </bean>-->
          <bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
              <property name="sessionFactory" ref="sessionFactory"/>
          </bean>
          <!-- 配置事务通知 -->
          <tx:advice id="txAdvice" transaction-manager="txManager">
              <tx:attributes>
                  <!--propagation 事务的传播方式
                      PROPAGATION_REQUIRED 支持当前事务,如果当前没有事务,就新建一个事务。这是最常见的选择。
                      PROPAGATION_SUPPORTS 支持当前事务,如果当前没有事务,就以非事务方式执行。
                      PROPAGATION_MANDATORY 支持当前事务,如果当前没有事务,就抛出异常。
                      PROPAGATION_REQUIRES_NEW 新建事务,如果当前存在事务,把当前事务挂起。
                      PROPAGATION_NOT_SUPPORTED 以非事务方式执行操作,如果当前存在事务,就把当前事务挂起。
                      PROPAGATION_NEVER 以非事务方式执行,如果当前存在事务,则抛出异常。
                      PROPAGATION_NESTED 如果当前存在事务,则在嵌套事务内执行。如果当前没有事务,则进行与PROPAGATION_REQUIRED类似的操作。
                    -->
                  <tx:method name="save*" propagation="REQUIRED"/>
                  <tx:method name="update*" propagation="REQUIRED"/>
                  <tx:method name="find*" read-only="true"/>
                  <tx:method name="*" propagation="REQUIRED"/>
              </tx:attributes>
          </tx:advice>
          <aop:config>
              <aop:pointcut expression="execution(* cn.wh.service.impl.*.*(..))" id="pointcut"/>
              <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut"/>
          </aop:config>
      </beans>
    8. 配置applicationContext-asd.xml
      <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: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.xsd 
              http://www.springframework.org/schema/tx
              http://www.springframework.org/schema/tx/spring-tx.xsd">
          <bean id="userDao" class="cn.wh.dao.impl.UserDaoImpl">
              <property name="sessionFactory" ref="sessionFactory"></property>
          </bean>
          <bean id="userService" class="cn.wh.service.impl.UserServiceImpl">
              <property name="userDao" ref="userDao"></property>
          </bean>
          <bean id="userAction" class="cn.wh.action.UserAction" scope="prototype">
              <property name="userService" ref="userService"></property>
          </bean>
      </beans>
    9. 配置struts配置文件
      <package name="default" namespace="/" extends="struts-default">
              <action name="list" class="userAction" method="list">
                  <result>/list.jsp</result>
              </action>
          </package>
    10. 配置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>
        <!-- open session in view过滤器 -->
        <filter>
            <filter-name>osiv</filter-name>
            <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
        </filter>
        <filter-mapping>
          <filter-name>osiv</filter-name>
          <url-pattern>/*</url-pattern>
        </filter-mapping>
        <!-- 乱码过滤器 -->
        <filter>
            <filter-name>CharacterEncoding</filter-name>
            <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
            <init-param>
                <param-name>encoding</param-name>
                <param-value>utf-8</param-value>
            </init-param>
        </filter>
        <filter-mapping>
            <filter-name>CharacterEncoding</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
        <!-- 配置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>
    11. 编辑list.jsp页面:
      <table width="80%" align="center">
              <tr>
                  <td>编号</td>
                  <td>姓名</td>
                  <td>年龄</td>
              </tr>
              <c:forEach items="${list }" var="bean">
                  <tr>
                  <td>${bean.id }</td>
                  <td>${bean.name }</td>
                  <td>${bean.age }</td>
              </tr>
              </c:forEach>
          </table>
  • 相关阅读:
    游戏开发中——垂直同步、绘制效率、显示器刷新频率与帧率
    python 异常
    python 多文件知识
    python if,for,while
    python 算术运算
    1.英语单词笔记
    Java import的作用
    java基础点
    Eclipse Java注释模板设置详解
    Java文档注释详解
  • 原文地址:https://www.cnblogs.com/forever2h/p/6757248.html
Copyright © 2011-2022 走看看