SSH的简单入门体验(Struts2.1+Spring3.1+Hibernate4.1)- 查询系统(上)
所谓SSH,指的是struts+spring+hibernate的一个集成框架,它是目前较流行的一种Web应用程序的开源框架。
集成SSH框架的系统从职责上分为四层:表示层、业务逻辑层、数据持久层和域模块层,以帮助开发人员在短期内搭建结构清晰、可复用性好、维护方便的Web应用程序。其中使用Struts作为系统的整体基础架构,负责MVC的分离,在Struts框架的模型部分,控制业务跳转,利用Hibernate框架对持久层提供支持,Spring做管理,管理struts和hibernate。(摘自百度百科)毕业实习布置了一个利用SSH框架做一个查询系统的作业,结果跟驾校科目三的考试冲突了,很悲剧的没有赶上第一次的检查作业,只能等国庆后再去补交了。我从SSH框架的布置到系统的简单实现一共花了三天的时间,里面的概念还不是很明白,只是知道怎么做而已。写下这篇文章也是为了以后重新入门SSH的时候有个入门介绍,防止以后还要从头开始学起(虽然觉得以后如果要写SSH的项目的话肯定要重新系统的学习一遍,无视无视~)
一、准备工具
- MyEclipse2014GA版本
- MySql5.6 和 其驱动包
- Tomcat 8
我这里提供一下MyEclipse的下载链接吧,正好网盘里有(附带破解),其他的工具请大家自行斟酌,因为MyEclipse已经自带数据库和Tomcat了,大家可以使用自带的工具。我这里是使用了上述的工具。
MyEclipse下载链接:百度网盘
MyEclipse破解文件下载链接:百度网盘
二、新建Web项目,导入SSH
- 首先第一步是新建一个web project的项目

点击next,下一步
点击finish,完成web project的创建。 - 接下来我们导入SSH框架所需的文件
首先在CnDemo项目根文件夹上右键
我没记错的话myeclipse早几期的版本菜单是add xxxx的样式,大家对号入座就行了。 - 首先是安装spring框架,选择Install Spring Facet.


点击finish,这样spring3.1框架就导入进去了。 - 添加Hibernate,不过在此之前你要先建立数据库的链接

这是我的数据库连接,使用了mysql。在Connection Url这里要填写你要连接的域名和数据库名以及用户名和密码。要导入对应的数据库驱动jar文件才行。最后不要忘记测试一下数据库是否能用。 - 导入Hibernate框架,选择Install Hibernate Facet.


点击完成,创建完毕。 - 最后是导入struts框架,选择Install Apache Struts(2.x) Facet.

点击完成就好了。
以上这样配置,就算基本上把SSH最简单的框架给搭建好了。
三、关于SSH的配置文件
其中最重要的是这两个配置文件
spring配置文件:applicationContext.xml。里面存放实体来控制整个系统。
struts配置文件:struts.xml。里面存放action的映射。
- 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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd"
xmlns:tx="http://www.springframework.org/schema/tx"> <!-- 数据库实体 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver">
</property>
<property name="url" value="jdbc:mysql://localhost/querydb"></property>
<property name="username" value="root"></property>
<property name="password" value="shen"></property>
</bean> <!-- hibernate实体 -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<!-- hibernate 数据的更新方式 -->
<prop key="hibernate.hbm2ddl.auto">update</prop>
<!-- 解决no session found -->
<!--使用getCurrentSession()需要加入如下配置 -->
<prop key="hibernate.current_session_context_class">thread</prop>
<!-- hibernate分页出现 ResultSet may only be accessed in a forward direction需要设置hibernate结果集滚动 -->
<!-- <prop key="jdbc.use_scrollable_resultset">false</prop> -->
</props> </property> <!-- 配置实体Hibernate的描述文件 -->
<property name="mappingResources">
<list>
<!-- 实体类列表 -->
<value>cpacm/pojo/Archive.hbm.xml</value>
</list>
</property>
</bean> <!-- 控制层 struts实体(Action实体) -->
<bean id="ArchiveAction" class="cpacm.action.ArchiveAction" scope="prototype">
<property name="archiveService" ref="ArchiveService"></property>
</bean> <!-- 逻辑层 service -->
<bean id="ArchiveService" class="cpacm.service.ArchiveService">
<property name="archiveDao" ref="ArchiveDao"></property>
</bean> <!-- 数据层,用于数据库的操作 -->
<bean id="ArchiveDao" class="cpacm.dao.ArchiveDao">
<property name="sessionFactory" ref="sessionFactory" />
</bean> <!-- spring自带的事物管理 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" /> <bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="create*" propagation="REQUIRED" />
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="findByQuery*" propagation="REQUIRED" />
<tx:method name="*" read-only="true" />
</tx:attributes>
</tx:advice> <aop:config proxy-target-class="true">
<aop:advisor advice-ref="txAdvice"
pointcut="execution(* net.noday..service..*.*(..))" /> </aop:config> </beans>其中,bean的注入基本方法为

- 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>
<!-- action由spring进行实例化 -->
<constant name="struts.objectFactory" value="spring" /> <constant name="struts.devMode" value="true"></constant> <package name="MySSH" extends="struts-default">
<!-- <action name="test" class="cpacm.struts2.demoAction" method="execute">
<result name="success">/Strut2Test.jsp</result> </action> --> <!-- 全局的通用的action -->
<global-results>
<result name="error">/error.jsp</result>
<!-- <result name="success">/success.jsp</result> -->
</global-results> <!-- Action列表,其中class=xxx为spring中的id -->
<action name="query" class="ArchiveAction" method="Query">
<result name="Query">/frame/ArcDataGrid.jsp</result>
</action>
<action name="tag" class="ArchiveAction" method="toUpdate">
<result name="toUpdate">/frame/ArcUpdate.jsp</result>
</action>
<action name="update" class="ArchiveAction" method="Update">
<result name="success">/frame/ArcDataGrid.jsp</result>
</action>
<action name="delete" class="ArchiveAction" method="Delete">
<result name="success">/frame/ArcDataGrid.jsp</result>
</action>
<action name="queryByclassId" class="ArchiveAction" method="QueryByclassID">
<result name="success">/frame/ArcDataGrid.jsp</result>
</action>
<action name="ArcAdd" class="ArchiveAction" method="Add">
<result name="success">/frame/ArcAddData.jsp</result>
</action>
</package> </struts>
以上基本就是配置的全部过程了,可能很简单,但也是五脏俱全了。篇幅有限,接下来会在下一篇讲述怎么建一个完整的系统。
SSH的简单入门体验(Struts2.1+Spring3.1+Hibernate4.1)- 查询系统(上)的更多相关文章
- SSH的简单入门体验(Struts2.1+Spring3.1+Hibernate4.1)- 查询系统(下)
我们继续吧,SSH最大的优点就是实现的系统的松耦合,能够将后台和前台有机的分离开来. 一.目录结构 一个好的程序要有一个好的开始.我们先来看看整个目录结构吧 主要的是三层架构概念,或者说是mvc的概念 ...
- struts2.3+spring3.2+hibernate4.2例子
有些教程比较老,可是版本更新不等人,基于马士兵老师小例子,自己重新引用了新的包,调试确实有点烦人,但是通过英文文档和google解决问题.官网的更新超快,struts2.3+spring3.2+hib ...
- Struts2.3+Spring3.2+Hibernate4.2框架搭建
一.环境 SSH使用的版本:struts2.3.14.spring3.2.2.hibernate4.2.0 数据库:MYSQL tomcat版本:apache-tomcat-7.0.42 二.所需要导 ...
- 基于Struts2,Spring4,Hibernate4框架的系统架构设计与示例系统实现
笔者在大学中迷迷糊糊地度过了四年的光景,心中有那么一点目标,但总感觉找不到发力的方向. 在四年间,尝试写过代码结构糟糕,没有意义的课程设计,尝试捣鼓过Android开发,尝试探索过软件工程在实际开发中 ...
- Struts2.0+Spring3+Hibernate3(SSH~Demo)
Struts2.0+Spring3+Hibernate3(SSH~Demo) 前言:整理一些集成框架,发现网上都是一些半成品,都是共享一部分出来(确实让人很纠结),这是整理了一份SSH的测试案例,完全 ...
- 【java开发系列】—— spring简单入门示例
1 JDK安装 2 Struts2简单入门示例 前言 作为入门级的记录帖,没有过多的技术含量,简单的搭建配置框架而已.这次讲到spring,这个应该是SSH中的重量级框架,它主要包含两个内容:控制反转 ...
- 一篇文章带你了解服务器操作系统——Linux简单入门
一篇文章带你了解服务器操作系统--Linux简单入门 Linux作为服务器的常用操作系统,身为工作人员自然是要有所了解的 在本篇中我们会简单介绍Linux的特点,安装,相关指令使用以及内部程序的安装等 ...
- Struts2入门2 Struts2深入
Struts2入门2 Struts2深入 链接: http://pan.baidu.com/s/1rdCDh 密码: sm5h 前言: 前面学习那一节,搞得我是在是太痛苦了.因为在Web项目中确实不知 ...
- Struts2入门1 Struts2基础知识
Struts2入门1 Struts2基础知识 20131130 代码下载: 链接: http://pan.baidu.com/s/11mYG1 密码: aua5 前言: 之前学习了Spring和Hib ...
随机推荐
- 201621044079 week07-JAVA GUI类
作业07-Java GUI编程 1. 本周学习总结 1.1 思维导图:Java图形界面总结 1.2 可选:使用常规方法总结其他上课内容. 2.书面作业 1. GUI中的事件处理 1.1 写出事件处理模 ...
- 【Linux】——实用命令
[前言] Linux的命令可以分为文件存取.目录操作.进程管理.权限管理.磁盘操作等内容,大量的命令方便了用户进行更快捷更高效的工作.但有一点需要说明的是,如果不采用linux的命令,也可以完成相应的 ...
- about !dbgprint to analyze BSOD dump file.
基本规则: 只有debug mode enable的机器,产生的dump file才会保存dbgprint的buffer. 默认!dbgprint的buffer size是4k. 增加buffer s ...
- BZOJ4563 HAOI2016放棋子(高精度)
没看清题还以为是要求数最大匹配数量……注意到任意障碍不在同一行同一列,且恰好有n个障碍,不妨通过交换列使得第i行第i列均有障碍.那么就是个错排了.居然wa了一发简直没救. #include<io ...
- P2563 [AHOI2001]质数和分解
题目描述 任何大于 1 的自然数 n 都可以写成若干个大于等于 2 且小于等于 n 的质数之和表达式(包括只有一个数构成的和表达式的情况),并且可能有不止一种质数和的形式.例如,9 的质数和表达式就有 ...
- SPOJ Repeats(后缀数组+RMQ-ST)
REPEATS - Repeats no tags A string s is called an (k,l)-repeat if s is obtained by concatenating k& ...
- ARC074 E RGB Sequence DP
---题面--- 题解: 首先,有一个不太直观的状态,f[i][j][k][l]表示DP到i位,三种颜色最后出现的位置分别是j, k, l的方案数.因为知道了三种颜色最后出现的位置,因此也可以得知以当 ...
- How to turn off the binary log for mysqld_multi instances?
Q: MySQL supports running multiple mysqld on the same server. One of the ways is to use mysqld_multi ...
- angular的一些问题
引入第三方类库 1.安装依赖 npm install jquey --save 2.引入项目 在angular-cli.json "scripts": [ "../nod ...
- D. Equalize the Remainders (set的基本操作)
D. Equalize the Remainders time limit per test 3 seconds memory limit per test 256 megabytes input s ...