SSH整合实例

来源:互联网 发布:软件开发阶段划分 编辑:程序博客网 时间:2024/06/10 06:08

        在上篇博客中,SSH已经整合了,整合后的效果如何,现在就看看在SSH框架下,实现的第一个实例。

第一:文件资源目录概览

               

简要说明:

        com.zhudan.base:包下有公共的dao接口和dao层的实现,比如说BaseDao和BaseDaoImpl

        com.zhudan.dao:包下是自己的dao接口比如说RoleDao,此时,dao接口要继承 com.zhudan.base包下的dao接口

        com.zhudan.dao.impl:包下是对com.zhudan.dao中的接口的一个实现,同时继承com.zhudan.base包下的dao实现。比如说RoleDaoImpl extends BaseDaoImpl implements RoleDao

        com.zhudan.domin:包下是实体和xx.hbm.xml

        com.zhudan.service:包下是业务逻辑层的接口,比如:RoleService

        com.zhudan.service.impl:包下是业务逻辑层接口的实现,比如说:RoleServiceImpl  implementsRoleService

        com.zhudan.view.action:包下是action类,调用service,实现转发与重定向


第二:各配置文件

1)web.xml

<?xml version="1.0" encoding="UTF-8"?>  <web-app version="2.5"       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_2_5.xsd">      <!--     配置spring的用于初始化容器对象的监听器 -->    <listener>        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>    </listener>    <context-param>    <param-name>contextConfigLocation</param-name>    <param-value>     /WEB-INF/classes/applicationContext*.xml    </param-value></context-param><!-- ~~~~~~~~~~~struts2的配置  start~~~~~~~~~~~ -->  <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><!-- ~~~~~~~~~~~struts2的配置  end~~~~~~~~~~~ -->     <welcome-file-list>      <welcome-file>index.jsp</welcome-file>    </welcome-file-list>      </web-app>  

2)struts.xml

<?xml version="1.0" encoding="UTF-8" ?>  <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">  <struts>  <!-- 设置为开发模式 --><constant name="struts.devMode" value="true" /><!-- 扩展名配置为action --><constant name="struts.action.extension" value="action"/>   <!-- 主题,将值设置为simple,即不使用UI模板。这将不会生成额外的html标签 --><constant name="struts.ui.theme" value="simple" />  <!-- ~~~~~~~~~~~spring和struts2整合后的配置 start~~~~~~~~~~~~~-->    <package name="default" extends="struts-default" > <!--     包名到bean名  -->        <action name="test" class="testaction">                <result name="success">/index.jsp</result>          </action>                  <!-- ~~~~~~~~~~~~~~~ 岗位管理 start~~~~~~~~~~~~ --><action name="role_*" class="roleAction" method="{1}"><result name="list">/WEB-INF/jsp/list.jsp</result><result name="toList" type="redirectAction">role_list</result><result name="saveUI">/WEB-INF/jsp/saveUI.jsp</result></action>    <!-- ~~~~~~~~~~~~~~~ 岗位管理 end~~~~~~~~~~~~ -->    </package>  <!-- ~~~~~~~~~~~spring和struts2整合后的配置 end~~~~~~~~~~~~~-->   </struts>   


3)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:context="http://www.springframework.org/schema/context"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">  <!-- 利用注解的方式实现 --><!--     <context:component-scan base-package="com.zhudan.test"></context:component-scan> --><!--     <context:component-scan base-package="com.zhudan.service.*"></context:component-scan>  --><!--     <context:component-scan base-package="com.zhudan.dao.*"></context:component-scan>  --><!--     <context:component-scan base-package="com.zhudan.view.action"></context:component-scan>  -->    <!--导入外部properties文件 -->     <context:property-placeholder location="classpath:jdbc.properties"/>             <!--  配置SessionFactory --><bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"><!-- 指定hibernate配置文件的位置 --><property name="configLocation" value="classpath:hibernate.cfg.xml"></property><!-- 连接池 --><property name="dataSource" ><bean class="com.mchange.v2.c3p0.ComboPooledDataSource">  <!--mysql数据库驱动 -->          <property name="driverClass" value="${driverClass}"></property>          <!-- mysql数据库名称 -->          <property name="jdbcUrl" value="${jdbcUrl}"></property>          <!-- 数据库的登陆用户名 -->          <property name="user" value="${user}"></property>          <!-- 数据库的登陆密码 -->          <property name="password" value="${password}"></property>          <!-- 方言:为每一种数据库提供适配器,方便转换 -->  <!-- <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>   -->        <!-- 其他配置 -->        <!--初始化时获取三个连接,取值应在minPoolSize与maxPoolSize之间。Default: 3 --><property name="initialPoolSize" value="3"></property><!--连接池中保留的最小连接数。Default: 3 --><property name="minPoolSize" value="3"></property><!--连接池中保留的最大连接数。Default: 15 --><property name="maxPoolSize" value="5"></property><!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 --><property name="acquireIncrement" value="3"></property><!-- 控制数据源内加载的PreparedStatements数量。如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 0 --><property name="maxStatements" value="8"></property><!--maxStatementsPerConnection定义了连接池内单个连接所拥有的最大缓存statements数。Default: 0 --><property name="maxStatementsPerConnection" value="5"></property><!--最大空闲时间,1800秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 --><property name="maxIdleTime" value="1800"></property></bean></property></bean><!-- <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"> --><!-- <constructor-arg> --><!-- <ref local="sessionFactory" /> --><!-- </constructor-arg> --><!-- </bean> --><!--dao层注入给sessionfactory  --><bean id="roleDao" class="com.zhudan.dao.impl.RoleDaoImpl"><property name="sessionFactory" ref="sessionFactory"/></bean> <!-- service注入给dao --><bean id="roleService" class="com.zhudan.service.impl.RoleServiceImpl"><property name="roleDao"><ref local="roleDao" /></property></bean><!-- action注入给service --><bean name="roleAction" class="com.zhudan.view.action.RoleAction"><property name="roleService"><ref bean="roleService" /></property></bean>     <!-- 声明式事务管理 (注解方式 )-->     <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">     <property name="sessionFactory" ref="sessionFactory"></property>      </bean>      <tx:annotation-driven transaction-manager="txManager"/>  </beans>  

4)hibernate.cfg.xml

<!DOCTYPE hibernate-configuration PUBLIC      "-//Hibernate/Hibernate Configuration DTD 3.0//EN"      "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">    <hibernate-configuration>  <session-factory >                 <!-- 方言:为每一种数据库提供适配器,方便转换 -->          <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>                  <property name="show_sql">true</property>        <property name="hbm2ddl.auto">update</property>                <mapping resource="com/zhudan/domin/User.hbm.xml"/>        <mapping resource="com/zhudan/domin/Role.hbm.xml"/>        <mapping resource="com/zhudan/domin/Department.hbm.xml"/>             </session-factory>  </hibernate-configuration>  

5)jdbc.properties

driverClass=com.mysql.jdbc.DriverjdbcUrl=jdbc:mysql://localhost:3306/oauser=rootpassword=123456

第三:包中的实现

主要是在action中和serviceImpl中通过get、set方法来注入,获取相应的service接口和dao接口


第四:jsp页面显示效果如下:

            


总结:

         一个好的SSH框架,不是说就像原来三层一样,分下UBD就可以了,越往后学习,越要学会抽象。根据自己的实际情况,舍去service也不是不可以;Java中的这些配置,分两套,一套xml一套注解,不管是什么吧,都是一个原理,换汤不换药,不管使用哪个,自己觉得好就好,自己选择就可以了。




0 0
原创粉丝点击