SSH整合

实现简单的添加股票功能

 

导入jar包:

 

 

创建实体类:

数据访问层接口及其实现

 

创建service

配置ApplicationContext.xml

 

复制代码
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    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
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        ">
   <!-- 01.C3p0 数据源 -->
   <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <!-- 设值注入,本质上使用的是类的set方法 -->
     <property name="driverClass" value="${jdbc.driver}"></property>
     <property name="jdbcUrl" value="${jdbc.url}"></property>
     <property name="user" value="${jdbc.username}"></property>
     <property name="password" value="${jdbc.password}"></property>
  </bean>

   <!-- 方式二:找到jdbc.peroperties文件 -->
  <context:property-placeholder location="classpath:jdbc.properties"/>
  
  <!-- 1.1 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.MySQL5InnoDBDialect</prop>
              <prop key="hibernate.show_sql">true</prop>
              <prop key="hibernate.format_sql">true</prop>
              <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate3.SpringSessionContext</prop>
      </props>
    </property>  
    
    <property name="mappingLocations" value="classpath:cn/happy/beans/Stock.hbm.xml"></property>
  </bean>
  
  <!-- dao -->
  <bean id="stockDao" class="cn.happy.dao.impl.StockDAOImpl">
    <property name="sessionFactory" ref="sessionFactory"></property>
  </bean>
  
   <!-- service -->
  <bean id="service" class="cn.happy.service.impl.StockServiceImpl">
    <property name="dao" ref="stockDao"></property>
  </bean>
  
   <!-- action -->
  <bean id="stockAction" class="cn.happy.action.StockAction">
    <property name="service" ref="service"></property>
  </bean>
 

   <!-- 事务;能让多个操作做为一个整体,同生共死 荣辱与共  DataSourceTransactionManager-->
   <!-- 01.事务管理器 -->
   <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
       <!-- 必须配置数据源 -->
       <property name="sessionFactory" ref="sessionFactory"></property>
   </bean>
   
   <!-- 第三种:AspectJ AOP 配置事务 -->
   <tx:advice id="txAdvice" transaction-manager="transactionManager">
       <tx:attributes>
         <tx:method name="save*" isolation="DEFAULT" propagation="REQUIRED"/>
       </tx:attributes>
   </tx:advice>
   
   <!-- 具体的AOP -->
   <aop:config>
      <aop:pointcut expression="execution(* *..service.*.*(..))" id="pointCut"/>
      <aop:advisor advice-ref="txAdvice" pointcut-ref="pointCut"/>
      
   </aop:config>
  
</beans>
复制代码

配置数据库连接信息

创建struts.xml

 

 因为是web端:所以要配置web.xml来加载资源文件

复制代码
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
  <display-name></display-name>
  
  <!--指定路径 applicationContext.xml  -->
  <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  
  <!-- 监听器:在应用程序加载启动的时候,spring容器自动构建 -->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  
  <!-- 核心过滤器 -->
  <filter>
    <filter-name>struts</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  
  <filter-mapping>
    <filter-name>struts</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  
  
  
  
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>
复制代码

 

 最后创建页面进行访问:

add.jsp

 

成功页面:

index.jsp

 

posted @ 2016-11-07 11:54  方圆i  阅读(171)  评论(0编辑  收藏  举报