Eclipse,Tomcat,MySql SSH框架的各种jar包
创建一个动态WEB项目步骤:1、File --> New --> Dynamic Web Project 如下图所示:图中红线部分最好一致,可以在Target runtime 后面New runtime配置与图中一致就行
项目创建好了,开始搭建SSH框架一、加入 Spring、Hibernate、Strurs2所有所需jar包1 把所有jar包复制到步骤一图3所示的lib目录下,如下图所示(共有37个,包 含C3P0,Mysql Connection的jar包),难得自个儿去下载,需要我的可以问我要。2 配置 web.xml 文件:
加入Hibernate配置文件步骤:在config目录下 New—>Hibernate Configuration File名为hibernate.cfg.xml的文件,按图所示创建即可在hibernate.cfg.xml文件中配置基本属性:依次是配置(方言、显示sql、格式化sql、生成数据表策略)此次配置好这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'如图所示:
Spring整合Hibernate在applicationContext.xml文件中配置:数据源,sessionFactory,声明式事物即可。在此说一下C3P0,Mysql Conn...的驱动版本c3p0-0.9.1.2.jar,mysql-connector-java-8.0.16.jar数据源:
加入Struts21、加入 Struts2 的配置文件如图所示创建一个struts.xml文件:
初学者请重视我多次提到的版本问题,请保持一致,否则可能会出现一些问题
因为WEB-INF下没有index.jsp,所以是看不到页面的,运行成功只需去看数据库有没有生成对应的表就行