多语言展示
当前在线:286今日阅读:26今日分享:39

SSH框架之浅析探讨(一)

本内容详细介绍了SSH框架的搭建步骤,并且做了一个简单的“增、删、改、查”小项目。首先需要搭建开发环境,准备开发工具:Eclipse,Tomcat,MySql安装并配置好,版本分别为:E->Version: Kepler Release,T->8.5.43,M->8.0.16接下来文就一步一步详细的介绍SSH框架的搭建及小项目的开发。
工具/原料

Eclipse,Tomcat,MySql SSH框架的各种jar包

方法/步骤
1

创建一个动态WEB项目步骤:1、File --> New --> Dynamic Web Project 如下图所示:图中红线部分最好一致,可以在Target runtime 后面New runtime配置与图中一致就行

2

项目创建好了,开始搭建SSH框架一、加入 Spring、Hibernate、Strurs2所有所需jar包1  把所有jar包复制到步骤一图3所示的lib目录下,如下图所示(共有37个,包 含C3P0,Mysql Connection的jar包),难得自个儿去下载,需要我的可以问我要。2  配置 web.xml 文件: contextConfigLocation classpath:applicationContext*.xml   org.springframework.web.context.ContextLoaderListener struts2 org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter struts2 /*  3 加入Spring,Hibernate,Struts2的配置文件,创建一个Source Folder名为config的文件夹存放我们所有的配置文件3.1 加入Spring配置文件步骤:在config目录下 New—>Spring Bean Configuration File名为applicationContext.xml的文件,按图所示创建即可(注意:如找不到,可能是eclipse缺少Spring插件,安装插件即可)

3

加入Hibernate配置文件步骤:在config目录下 New—>Hibernate Configuration File名为hibernate.cfg.xml的文件,按图所示创建即可在hibernate.cfg.xml文件中配置基本属性:依次是配置(方言、显示sql、格式化sql、生成数据表策略)此次配置好这4个就行了org.hibernate.dialect.MySQL5InnoDBDialecttruetrueupdate然后添加链接数据库的数据源:其中user和password写你自己的数据库链接用户名和密码jdbc.user=rootjdbc.password=123qwejdbc.driverClass=com.mysql.jdbc.Driverjdbc.jdbcUrl=jdbc:mysql:///ssh?useUncode=true&characterEncoding=utf8&serverTimezone=UTC

4

建立持久化类, 生成其对应的Xxx.hbm.xml 文件 1、建2个类,分别为学生Student类和学院College类Student类:package com.ssh.entities;import java.util.Date; public class Student { private Integer id; private String stuName; private String stuSex; private int stuAge; private Date stuBirth; //单向n-1的关联关系 private College collegeName;  public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getStuName() { return stuName; } public void setStuName(String stuName) { this.stuName = stuName; } public String getStuSex() { return stuSex; } public void setStuSex(String stuSex) { this.stuSex = stuSex; } public int getStuAge() { return stuAge; } public void setStuAge(int stuAge) { this.stuAge = stuAge; } public Date getStuBirth() { return stuBirth; } public void setStuBirth(Date stuBirth) { this.stuBirth = stuBirth; } public College getCollegeName() { return collegeName; } public void setCollegeName(College collegeName) { this.collegeName = collegeName; } @Override public String toString() { return 'Student [id=' + id + ', stuName=' + stuName + ', stuSex=' + stuSex + ', stuAge=' + stuAge + ', stuBirth=' + stuBirth + ', collegeName=' + collegeName + ']'; }}College类:package com.ssh.entities; public class College { private Integer id; private String collegeName;  public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getCollegeName() { return collegeName; } public void setCollegeName(String collegeName) { this.collegeName = collegeName; }}生成对应的Xxx.hbm.xml 文件,其中需要把generator 的class属性改为'native'如图所示:

5

Spring整合Hibernate在applicationContext.xml文件中配置:数据源,sessionFactory,声明式事物即可。在此说一下C3P0,Mysql Conn...的驱动版本c3p0-0.9.1.2.jar,mysql-connector-java-8.0.16.jar数据源:  sessionFactory: classpath:hibernate.cfg.xml classpath:com/ssh/entities/*.hbm.xml 到此先不急配下去,运行项目,看是否在mysql的ssh中生成数据表college和student,如下图查看:若运行成功,生成对应的数据表,再接着配声明式事物。声明式事物: 至此Spring整合Hibernate完毕

6

加入Struts21、加入 Struts2 的配置文件如图所示创建一个struts.xml文件:   至此,整个SSH框架已搭建完毕,有的就只是在完成功能时再在相应的配置文件中配置。功能的实现,请查看SSH框架之浅析探讨(二)

注意事项
1

初学者请重视我多次提到的版本问题,请保持一致,否则可能会出现一些问题

2

因为WEB-INF下没有index.jsp,所以是看不到页面的,运行成功只需去看数据库有没有生成对应的表就行

推荐信息