ssh整合(下)

来源:互联网 发布:opencv lbp算法 编辑:程序博客网 时间:2024/06/06 12:38



9.com.hsp.test:测试专用。在开发中,特别是在写service层的方法时,难以确定自己写的方法是否正确。确认service没问题了,可以把精力集中在Action中,即使报错,也能很快找出bug

testService.java:

package com.hsp.test;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.hsp.domain.User;import com.hsp.service.Inter.UserService;public class testService {private ApplicationContext ctx=null;private UserService userService=null;{ctx=new ClassPathXmlApplicationContext("applicationContext.xml");userService=(UserService)ctx.getBean("userService");}@Testpublic void testCheckUserService(){User user=new User();user.setUsername("wayne");user.setPassword("123");user=userService.checkUser(user);System.out.println("username="+user.getUsername()+" id="+user.getId());}}
10.配置文件,这没什么好说的。

applicationContext.xml:

<?xml version="1.0" encoding="UTF-8"?>    <beans xmlns="http://www.springframework.org/schema/beans"        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       xmlns:p="http://www.springframework.org/schema/p"      xmlns:aop="http://www.springframework.org/schema/aop"       xmlns:context="http://www.springframework.org/schema/context"      xmlns:jee="http://www.springframework.org/schema/jee"      xmlns:tx="http://www.springframework.org/schema/tx"      xsi:schemaLocation="            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd          http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd          http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd          http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd          http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">        <!-- 配置自动扫描的包 -->    <context:component-scan base-package="com.hsp" />    <!-- 导入资源文件 -->      <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>       <!-- 配置Hibernate的SessionFactory实例:通过Spring提供的LocalSessionFactoryBean 进行配置-->    <bean id="sessionFactory"          class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">          <property name="dataSource">              <ref bean="dataSource" />          </property>          <!-- 配置hibernate映射文件的位置名称 -->        <property name="configLocation" value="classpath:hibernate.cfg.xml"/>           <!-- 使用hibernateProperties属性来配置Hibernate的原生属性 -->        <!-- <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>            <prop key="hibernate.hbm2ddl.auto">update</prop>        </props>        </property> -->        <!-- 配置hibernate影射文件位置及名称 -->        <property name="mappingLocations"         value="classpath:com/hsp/domain/*.hbm.xml"></property>    </bean>        <!-- 配置Spring的声明式事务 -->    <!-- 1.配置事务管理器 -->    <bean id="transactionManager"          class="org.springframework.orm.hibernate4.HibernateTransactionManager">          <property name="sessionFactory" ref="sessionFactory" />      </bean>         <!-- 2.配置事务属性,需要事务管理器 -->     <tx:advice id="txAdvice" transaction-manager="transactionManager">          <!-- 定义事务传播属性 -->          <tx:attributes>              <tx:method name="insert*" propagation="REQUIRED" />              <tx:method name="update*" propagation="REQUIRED" />              <tx:method name="edit*" propagation="REQUIRED" />              <tx:method name="save*" propagation="REQUIRED" />              <tx:method name="add*" propagation="REQUIRED" />              <tx:method name="new*" propagation="REQUIRED" />              <tx:method name="set*" propagation="REQUIRED" />              <tx:method name="remove*" propagation="REQUIRED" />              <tx:method name="delete*" propagation="REQUIRED" />              <tx:method name="change*" propagation="REQUIRED" />              <tx:method name="get*" propagation="REQUIRED" read-only="true" />              <tx:method name="find*" propagation="REQUIRED" read-only="true" />              <tx:method name="load*" propagation="REQUIRED" read-only="true" />              <tx:method name="*" propagation="REQUIRED" read-only="true" />          </tx:attributes>      </tx:advice>            <!-- 3.配置事务切点,并把切点和事务属性关联起来 -->    <aop:config>          <aop:pointcut id="txPointcut" expression="execution(* com.hsp.service..*.*(..))" />          <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut" />      </aop:config>  </beans>  

11.db.properties:

jdbc.user=rootjdbc.password=rootjdbc.driverClass=com.mysql.jdbc.Driverjdbc.jdbcUrl=jdbc:mysql:///hongliangjdbc.initialPoolSize=5jdbc.maxPoolSize=10jdbc.minPoolSize=2


12.struts.xml:

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"    "http://struts.apache.org/dtds/struts-2.0.dtd"><struts>      <!-- 指定Spring接管Struts2 -->      <constant name="struts.objectFactory" value="spring"/>       <constant name="struts.enable.DynamicMethodInvocation" value="false"/>      <!-- 指定Struts2Action的后缀名 -->      <constant name="struts.action.extension" value="do,action"/>      <constant name="struts.devMode" value="true"/>      <package name="default" namespace="/" extends="struts-default">         <!-- Spring整合Struts2时,在Struts2中配置Spring的Action的class需要指向IOC容器中该bean的id -->           <action name="user_*" class="loginAction" method="{1}">                <result name="loginSuccess">/WEB-INF/success.jsp</result>                <result name="loginFail">/WEB-INF/login.jsp</result>           </action>      </package></struts>13.hibernate.cfg.xml:该文件在整合中其实可以不写,但是为了applicationContext.xml不要太过冗余,一些配置还是在.hibernate.cfg.xml中合适.hibernate.cfg.xml:<?xml version='1.0' encoding='gb2312'?>   <!--表明解析本XML文件的DTD文档位置,DTD是Document Type Definition 的缩写,即文档类型的定义,XML解析器使用DTD文档来检查XML文件的合法性。hibernate.sourceforge.net/hibernate-configuration-3.0dtd可以在Hibernate3.1.3软件包中的src\org\hibernate目录中找到此文件-->   <!DOCTYPE hibernate-configuration PUBLIC             "-//Hibernate/Hibernate Configuration DTD 3.0//EN"             "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><hibernate-configuration><session-factory><!-- 配置hibernate基本属性 --><!-- 1.数据源需配置到IOC容器中,此处不再配置数据源 --><!-- 2.关联的hbm.xml也在IOC容器配置SessionFactory实例时在进行配置--><!-- 3.配置hibernate基本属性:方言,SQL显示及格式化,生成数据表的策略以及二级缓存等。 --><property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property><property name="hibernate.show_sql">true</property><property name="hibernate.format_sql">true</property><property name="hibernate.hbm2ddl.auto">update</property><!-- 配置hibernate的二级缓存 --></session-factory></hibernate-configuration>   

14.web.xml:

<!--?xml version="1.0" encoding="UTF-8"?-->  <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation="http://java.sun.com/xml/ns/javaee       http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">    <display-name></display-name>          <!-- 添加对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>           <!-- 添加对struts2的支持 -->    <filter>      <filter-name>struts2</filter-name>      <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>    </filter>    <!-- 该过滤器作用类似于取消懒加载,会在需要关联时向数据库发送请求 -->   <filter>      <filter-name>openSessionInViewFilter</filter-name>      <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>      <init-param>        <param-name>singleSession</param-name>        <param-value>true</param-value>      </init-param>    </filter>         <filter-mapping>      <filter-name>struts2</filter-name>      <url-pattern>/*</url-pattern>    </filter-mapping>     <filter-mapping>      <filter-name>openSessionInViewFilter</filter-name>      <url-pattern>*.do,*.action</url-pattern>    </filter-mapping>         <welcome-file-list>      <welcome-file>index.jsp</welcome-file>    </welcome-file-list>  </web-app>  

15.note.txt:整合ssh的流程:

ssh(spring4.2+hibernate4.2+struts2.3.29)整合步骤1).加入jar包:在WEB-INF的lib目录下:2).ssh整合中Spring与Hibernate的整合较为复杂,两者的整合包括:--Spring的IOC容器来管理Hibernate的SessionFactory--让Hibernate使用上Spring的声明式事务3).引进Hibernate--添加Hibrnate的配置文件:hibernate.cfg.xml--编写持久化类对应的hbm.xml:   在生成hbm.xml时,不提倡使用注解,而是采用传统的生成hbm.xml文件,因为hbm.xml通俗易懂,   展示各张表的关联依赖关系,方便修改对应的数据库的表的设计,hbm.xml代码可以通过Myeclipse   生成。   --在使用Myeclipse生成xhbm.xml文件,可以参考百度的教程,有个细节注意下:不建议直接在原     项目中生成hbm.xml文件,可以额外创建一个项目,这个项目专门用来存放Myeclipse生成的hbm.xml     ,待生成这些hbm.xml文件后,可将hbm.xml复制粘贴到原项目中。4).引进Spring--添加Spring的配置文件:applicationContext.xml5).在测试包中测试所写的service方法,测试Spring与Hibernate是否整合好6).引进Struts2--添加Struts2的配置文件:struts.xml--配置web.xml文件,增加Spring监听器和Struts2的过滤器7).写相应的Action与jsp页面测试Spring与Struts2是否连通8).整合成功,可在此架构上编码

16.一些前端的页面就不展示了,根据struts2,Action和数据库的信息自己模拟。
给个form表单:

<form action="${pageContext.request.contextPath}/user_login.action" method="post">                <input type="text" name="username" class="username" placeholder="用户名">                <input type="password" name="password" class="password" placeholder="密码">                <button type="submit"> 登 录 </button>                <div class="username" style="margin-top:20px;color:red;"><span>${message}</span></div>            </form>


17.最后罗嗦一句,关于ssh和ssm:struts2虽然被很多人喷,但是springMVC能做的,struts2基本都可以,而且两者的思路差不多,都相当于一个路由器。mybatis虽然也挺火,但是感觉做小项目还行,轻便容易操作,没hibernate那么笨重。但是如果一个项目有几十张表,用mybatis,光写sql语句就能写断手。所以以需求为准,根据实际情况选择。


1 0