Eclipse搭建SSH(Struts2+Spring+Hibernate)框架教程
ccwgpt 2024-09-14 00:18 103 浏览 0 评论
SSH框架简介:①SSH框架是由struts2、spring、hibernate三大框架组合起来的一套总框架,一般来说这三个东西我们不会单独使用。
②在学习SSH框架之前建议读者先学mvc,因为SSH是在mvc基础上根据mvc的缺点而产生的一套比较成熟的框架,也比较稳定。
③SSH框架的流程:浏 览器(或客户端)发送请求到服务器,先经过项目中web.xml中过滤器(<filter>和<filter- mapping>)审核,通过了再发送给action包中的IndexAction类,struts.xml根据IndexAction类中 return的值再进行跳转,跳转的页面是struts.xml中<result>配置的页面名,然后页面响应回客户端(至于怎么响应的就是 当客户敲回车之后就有一个页面显示)。
④struts的核心思想:实现mvc
⑤spring的核心思想:解耦,也就是代码中不出现new实现类的代码,我们创建了接口不用关心实现类是谁,实现类由spring帮我们注入,我们只需要在定义接口的时候给它一个set方法并且在配置文件里改<property>中的id和ref就行
⑥hibernate的核心思想:(ORM-对象关系映射)连接 数据库,我们不用在数据库写创建表的语句,数据库表的字段根据实体类中属性的名字然后我们在BookCard.hbm.xml文件里配 置<property>以及<property>的相关属性。
搭建前需要注意事项(搭建前应准备):
1.需要用到的技术(必须了解):Struts2/spring(新版spring官网不能直接下载,可以百度下载别人打包好的)/hibernate(S-S-H)
2.需要用到的工具:eclipse
3.需要用到的包:①structs2.3.30 ②spring-framework-4.2.2.RELEASE-dist ③hibernate-release-5.2.2.Final
4.建议运行环境:①Windows 7-64位 ②Tomcat 8.0 ③jdk1.8.0_91 ④SQL server2008 ⑤Eclipse JavaEE环境下
博主建议:1.读者在看本博文之前读者必须了解mvc
2.读者必须按照博文的步骤来阅读和操作
3.博主不会写没有作用的注释代码,读者要好好体会每一句注释代码的意义(绿色的字体代表的是注释)
4.本博文Struts、spring、hibernate中所有导的包都放在WEB-INF-->lib目录下
5.软件工程思想(灵魂):高内聚、低耦合
6.必须熟知每个包的作用:action 包(跳转),service包(服务/业务逻辑),dao包(访问数据库),entity包(实体类),util包(工具包)。在创建包的同时我们要新建 的接口以及实现类有: ①在service包创建接口IndexService(名字随意但最好有意义)以及实现类IndexServiceImpl(名字随意但最好有意义) ②在dao包新建接口IndexDao以及实现类IndexDaoImpl ③在util包新建接口MyConnection以及实现类MyConnectionImpl
7.本来util包有两个作用:工具包、连接数据库包,但加入了hibernate之后取代了连接数据库的功能,现在的util包只做工具包。
正式开始:
第一步:新建一个web项目,并勾上Generate web.xml deployment descriptor。如下图:
-----------------------------------------------------------------------
第二步:导入struts的所有jar包放在lib目录下(structs2.3.30\struts-2.3.30-apps\struts-2.3.30\apps\struts2-showcase\WEB-INF\lib)
第三步: 打开web.xml文件。
目录:【WebContent-->WEB-INF目录-->web.xml】 如下图:
打开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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>TestSSH</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> </web-app>
改成如下(直接把以下代码拷贝覆盖原来的代码)
<?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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>TestSSH</display-name> //配置首页 <welcome-file-list> <welcome-file>index.action</welcome-file> </welcome-file-list> //配置过滤器 <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> </web-app>
第四步:在src目录下新建五个包:action(跳转)包、service(服务/业务逻辑)包、dao(访问数据库)包、entity(实体类)包、util(工具)包。如下图:
第五步:在action包下新建IndexAction类并继承Actionsupport,目的是为了让本类拥有ActionSupport类的方法
package action; import com.opensymphony.xwork2.ActionSupport; public class IndexAction extends ActionSupport{ //定义Struts默认的方法 public String execute(){ //返回字符串类型给struts.xml文件接收 return "success"; } //定义错误时应调用方法 public String error(){ //返回字符串类型给struts.xml文件接收 return "error"; } }
第六步:在src目录下创建struts.xml文件
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" "http://struts.apache.org/dtds/struts-2.5.dtd"> <!-- 上面的头,注意版本,从样例里复制过来 showcase.war\WEB-INF\src\java\struts.xml --> <struts> <!-- 第1步:先定义一个包,名字任意取 并继承struts-default--> <package name="mypck" extends="struts-default"> <!-- 第2步:①定义一个action,配置跳转信息 name 类似于Servlet,这个name是浏览器要访问的name,很重要,②class的值:包名.类名 http://xxxx/xxx/Index.action http://xxxx/xxx/Index class 对应于自己写的Action类 当不写method属性时,默认调用的是execute --> <action name="Index" class="ssh.IndexAction" method="exe2"> <!-- 跳转是forward /WEB-INF/是防止jsp不经过action就可以访问 --> <!-- success和error是从action类中return回的值,要与action类return值一样 --> <!-- index2.jsp和error.jsp表示要跳转的页面 --> <result name="success">/WEB-INF/index2.jsp</result> <result name="error">/WEB-INF/error.jsp</result> </action> </package> </struts>
经过以上步骤我们SSH中的struts2已经配置好了,下面是配置Spring:
第七步:导jar包(\spring-framework-4.2.2.RELEASE\libs全部拷贝 以Javadoc和source为后缀的不要
如下图:
第八步:在web.xml文件里配置监听器
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>ssh_001</display-name> <welcome-file-list> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <!-- 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> <!-- spring的监听器配置开始 --> <!-- 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> <!-- spring的监听器配置结束 --> </web-app>
第九步:在struts.xml文件里配置<constant name="struts.objectFactory" value="spring" />
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" "http://struts.apache.org/dtds/struts-2.5.dtd"> <!-- 上面的头,注意版本,从样例里复制过来 showcase.war\WEB-INF\src\java\struts.xml --> <struts> <!-- 这里是spring配置 --> <!-- 告知Struts2运行时使用Spring来创建对象 --> <constant name="struts.objectFactory" value="spring" /> <!-- 第1步:先定义一个包,名字任意取 --> <package name="mypck" extends="struts-default"> <!-- 第2步:定义一个action,配置跳转信息 name 类似于Servlet,这个name是浏览器要访问的name,很重要,class的值:包名.类名 http://xxxx/xxx/Index.action http://xxxx/xxx/Index class 对应于自己写的Action类 当不写method属性时,默认调用的是execute --> <action name="Index" class="ssh.IndexAction" method="exe2"> <!-- 跳转是forward /WEB-INF/是防止jsp不经过action就可以访问 --> <!-- success和error是从action类中return回的值,要与action类return值一样 --> <!-- index2.jsp和error.jsp表示要跳转的页面 --> <result name="success">/WEB-INF/index2.jsp</result> <result name="error">/WEB-INF/error.jsp</result> </action> </package> </struts>
第十步:在src目录下新建application.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.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd"> <!-- ①一个bean标签对应一个类,id为myIndexAction的bean就对应项目中的IndexAction类 ②id的值是随便起,但最好有意义 ③class的值是包名.类名 ④scope="prototype"是非单例,不用理解,但一定要写这句代码,记住有这回事就行 --> <bean id="myIndexAction" class="ssh.action.IndexAction" scope="prototype"> <!-- ①name的值是要注入的变量名 ②ref是引用类的类名,name为“is”的变量引用的是myIndexService的值 --> <property name="is" ref="myIndexService"/> </bean> <!-- myIndexService = new ssh.service.IndexServiceImpl() id为myIndexService的bean对应项目中的IndexService类--> <bean id="myIndexService" class="ssh.service.IndexServiceImpl" scope="prototype"> <!-- name为id的变量引用的是myIndexDao的值 --> <property name="id" ref="myIndexDao"/> </bean> <bean id="myIndexDao" class="ssh.dao.IndexDaoImpl" scope="prototype"> <property name="c" ref="myConnection"></property> </bean> <!-- 下面这个bean是对应项目中的connection类,class的值是包名.类名 --> <bean id="myConnection" class="ssh.util.MyConnectionImpl_SQLSERVER" scope="prototype"> <!-- 这里没有<property>是因为connection这个类已经是连接数据库的类,我们已经不需要通过new实现类了 --> </bean> </beans>
以上就是struts和spring的使用,接下来是SSH中的最后一个,也是最重要的一个:hibernate
第十一步:导jar包(hibernate-release-5.2.2.Final\lib\required所有包)
第十二步:把(hibernate-release-5.2.2.Final\project\hibernate-core\src\test\resources)目录下的hibernate.cfg.xml文件放在src目录下。
第十三步:在hibernate.cfg.xml文件里最顶部加上<?xml version="1.0" encoding="utf-8"?>
如图:
第十四步:配置hibernate.cfg.xml文件,并配置映射文件
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- hibernate配置文件 --> <!-- 配置数据库名,以及用户名,密码 --> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost:3306/CardDB</property> <property name="connection.username">root</property> <property name="connection.password">123456</property> <!-- 每个数据库都有1个 --> <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property> <property name="connection.pool_size">5</property> <property name="show_sql">true</property> <property name="format_sql">true</property> <property name="hbm2ddl.auto">update</property> <!-- 配置映射文件 --> <mapping resource="ssh/entity/BookCard.hbm.xml"/> </session-factory> </hibernate-configuration>
第十五步:配置BookCard.hbm.xml(实体类配置)文件
<?xml version="1.0" encoding="UTF-8"?> <hibernate-mapping xmlns="http://www.hibernate.org/xsd/hibernate-mapping"> <!-- ①class的值是包名.实体类名 ②table的值是数据库表名 --> <class name="ssh.entity.BookCard" table="BookCard"> <!-- ①<id>标签是要作为主键的属性或字段才能用 ②column是数据库的字段名--> <id name="cid" column="cid"> <generator class="native"></generator> </id> <!-- <property>标签对应于属性(数据库字段)在<property>标签中设置数据库相关的属性,比如长度、类型、是否为空、列名...等等 --> <property name="name" type="string" length="50" column="name" not-null="true"></property> <property name="sex" type="string" length="2" column="sex"></property> <property name="cardDate" type="date" column="cardDate"></property> <property name="deposit" type="double" column="deposit"></property> </class> </hibernate-mapping>
第十六步:在IndexDaoImpl实现类中构造SessionFactory
package ssh.dao; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import org.hibernate.query.Query; public class IndexDaoImpl implements IndexDao { <!-- SessionFactory是hibernate的内置对象 --> private SessionFactory sessionFactory; <!-- 给SessionFactory一个set方法,便于spring注入 --> public void setSessionFactory(SessionFactory sf) { this.sessionFactory = sf; } @Override public List<BookCard> getAllBookCard() { <!-- sessionFactory这个实例可以自己按常规的hibernate传统写法创建也可以交给spring去托管sessionFactory = new Configuration().configure().buildSessionFactory(); --> Session session = sessionFactory.openSession(); } }
以上就是eclipse搭建SSH框架的全过程,接下来就可以在service相关类以及dao相关类编写我们的代码!
有不懂的或者要提意见的可以关注博主,我们来互相探讨。
相关推荐
- 一个基于.Net Core遵循Clean Architecture原则开源架构
-
今天给大家推荐一个遵循CleanArchitecture原则开源架构。项目简介这是基于Asp.netCore6开发的,遵循CleanArchitecture原则,可以高效、快速地构建基于Ra...
- AI写代码翻车无数次,我发现只要提前做好这3步,bug立减80%
-
写十万行全是bug之后终于找到方法了开发"提示词管理助手"新版本那会儿,我差点被bug整崩溃。刚开始两周,全靠AI改代码架构,结果十万行程序漏洞百出。本来以为AI说没问题就稳了,结果...
- OneCode低代码平台的事件驱动设计:架构解析与实践
-
引言:低代码平台的事件驱动范式在现代软件开发中,事件驱动架构(EDA)已成为构建灵活、松耦合系统的核心范式。OneCode低代码平台通过创新性的注解驱动设计,将事件驱动理念深度融入平台架构,实现了业务...
- 国内大厂AI插件评测:根据UI图生成Vue前端代码
-
在IDEA中安装大厂的AI插件,打开ruoyi增强项目:yudao-ui-admin-vue31.CodeBuddy插件登录腾讯的CodeBuddy后,大模型选择deepseek-v3,输入提示语:...
- AI+低代码技术揭秘(二):核心架构
-
本文档介绍了为VTJ低代码平台提供支持的基本架构组件,包括Engine编排层、Provider服务系统、数据模型和代码生成管道。有关UI组件库和widget系统的信息,请参阅UI...
- GitDiagram用AI把代码库变成可视化架构图
-
这是一个名为gitdiagram的开源工具,可将GitHub仓库实时转换为交互式架构图,帮助开发者快速理解代码结构。核心功能一键可视化:替换GitHubURL中的"hub...
- 30天自制操作系统:第六天:代码架构整理与中断处理
-
1.拆开bootpack.c文件。根据设计模式将对应的功能封装成独立的文件。2.初始化pic:pic(可编程中断控制器):在设计上,cpu单独只能处理一个中断。而pic是将8个中断信号集合成一个中断...
- AI写代码越帮越忙?2025年研究揭露惊人真相
-
近年来,AI工具如雨后春笋般涌现,许多人开始幻想程序员的未来就是“对着AI说几句话”,就能轻松写出完美的代码。然而,2025年的一项最新研究却颠覆了这一期待,揭示了一个令人意外的结果。研究邀请了16位...
- 一键理解开源项目:两个自动生成GitHub代码架构图与说明书工具
-
一、GitDiagram可以一键生成github代码仓库的架构图如果想要可视化github开源项目:https://github.com/luler/reflex_ai_fast,也可以直接把域名替换...
- 5分钟掌握 c# 网络通讯架构及代码示例
-
以下是C#网络通讯架构的核心要点及代码示例,按协议类型分类整理:一、TCP协议(可靠连接)1.同步通信//服务器端usingSystem.Net.Sockets;usingTcpListene...
- 从复杂到优雅:用建造者和责任链重塑代码架构
-
引用设计模式是软件开发中的重要工具,它为解决常见问题提供了标准化的解决方案,提高了代码的可维护性和可扩展性,提升了开发效率,促进了团队协作,提高了软件质量,并帮助开发者更好地适应需求变化。通过学习和应...
- 低代码开发当道,我还需要学习LangChain这些框架吗?| IT杂谈
-
专注LLM深度应用,关注我不迷路前两天有位兄弟问了个问题:当然我很能理解这位朋友的担忧:期望效率最大化,时间用在刀刃上,“不要重新发明轮子”嘛。铺天盖地的AI信息轰炸与概念炒作,很容易让人浮躁与迷茫。...
- 框架设计并不是简单粗暴地写代码,而是要先弄清逻辑
-
3.框架设计3.框架设计本节我们要开发一个UI框架,底层以白鹭引擎为例。框架设计的第一步并不是直接撸代码,而是先想清楚设计思想,抽象。一个一个的UI窗口是独立的吗?不是的,...
- 大佬用 Avalonia 框架开发的 C# 代码 IDE
-
AvalonStudioAvalonStudio是一个开源的跨平台的开发编辑器(IDE),AvalonStudio的目标是成为一个功能齐全,并且可以让开发者快速使用的IDE,提高开发的生产力。A...
- 轻量级框架Lagent 仅需20行代码即可构建自己的智能代理
-
站长之家(ChinaZ.com)8月30日消息:Lagent是一个专注于基于LLM模型的代理开发的轻量级框架。它的设计旨在简化和提高这种模型下代理的开发效率。LLM模型是一种强大的工具,可以...
你 发表评论:
欢迎- 一周热门
- 最近发表
- 标签列表
-
- 框架图 (58)
- flask框架 (53)
- quartz框架 (51)
- abp框架 (47)
- springmvc框架 (49)
- 分布式事务框架 (65)
- scrapy框架 (56)
- shiro框架 (61)
- 定时任务框架 (56)
- java日志框架 (61)
- mfc框架 (52)
- abb框架断路器 (48)
- beego框架 (52)
- java框架spring (58)
- grpc框架 (65)
- tornado框架 (48)
- 前端框架bootstrap (54)
- orm框架有哪些 (51)
- 知识框架图 (52)
- ppt框架 (55)
- 框架图模板 (59)
- 内联框架 (52)
- cad怎么画框架 (58)
- ssm框架实现登录注册 (49)
- oracle字符串长度 (48)