SSH(Spring+Struts2+Hibernate)框架搭建步骤(含配置文件以及运行结果)
1.创建web项目
2.导入ssh 所需要的多有jar包,到web-inf下面的lib里面
3.将导入过来的jar包都build--path一下
4.切换到myeclipse database视图中,添加链接数据库的链接
5.新建一个数据库连接(如果忘记了数据库链接时你可以去下面的网址中查看):
常用数据库连接串与驱动总结
6.切换视图,在src下面新建一个名为org.entity的包:
7.添加hibernate,右击项目名,选择myeclipseadd HIbernaete ……
在自动创建的hibernate.cfg.xml文件中,新加两行代码,实现打印输出sql语句和格式化sql语句的功能。
<property name="show_sql">trueproperty>
<property name="format_sql">trueproperty>
8.右击项目,添加struts
9.添加spring的内容:
10.web.xml里面的内容:
version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
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_3_0.xsd">
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
</listener>
<context-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:applicationContext.xml
</param-value>
</context-param>
<filter>
<filter-name>openSessionInViewFilterfilter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilterfilter-class>
<init-param>
<param-name>flushModeparam-name>
<param-value>AUTOparam-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>openSessionInViewFilterfilter-name>
<url-pattern>/*url-pattern>
</filter-mapping>
<filter>
<filter-name>struts2filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2filter-name>
<url-pattern>/*url-pattern>
</filter-mapping>
<error-page>
<error-code>404error-code>
<location>/errorPage.jsplocation>
</error-page>
<welcome-file-list>
<welcome-file>index.jspwelcome-file>
</welcome-file-list>
</web-app>
如果嫌复制麻烦,可以直接下载源文件(由于附件不支持.xml格式,所以下载完之后需要将后缀名改成.xml即可):
点击可下载web.xml文件
11.配置spring的内容,打开applicationContext.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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
">
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation"
value="classpath:hibernate.cfg.xml">
</property>
</bean>
<bean id="txManage" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<tx:advice id="txAdvice" transaction-manager="txManage">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="del*" propagation="REQUIRED"/>
<tx:method name="get*" read-only="true"/>
<tx:method name="find*" read-only="true"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut expression="execution(* org.service..*.*(..))" id="mycut"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="mycut"/>
</aop:config>
</beans>
如果嫌复制麻烦,可以直接下载源文件(由于附件不支持.xml格式,所以下载完之后需要将后缀名改成.xml即可):
点击下载applicationContext.xml文件
12.切换到myeclipse database视图:(反向生成实体类)
13.右击表:
点击finish完成即可。
14.切换视图至myeclipsep perspective
15.将项目发布到tomcat中。
16.启动tomcat服务,检查控制台是否有错误(一般只要控制台中没有超链接错误,正常显示毫秒数即可)。
17.如果没有错误,将服务关掉。
18.开始根据实体类写接口,一般一个实体类对应一个Dao接口
19.在IStudentDao接口中写增删改查的抽象方法。
20.开始写Dao层的实现类,新建一个StudentDaoImpl的实现类。需要继承HibernateDaoSupport类,实现IStudentDao接口。
实现类中的代码:
publicclassStudentDaoImplextendsHibernateDaoSupportimplements IStudentDao {
//添加
@Override
publicvoidsaveStudent(Studentstudent) {
this.getHibernateTemplate().save(student);
}
//修改
@Override
publicvoidupdateStudent(Studentstudent) {
this.getHibernateTemplate().update(student);
}
//删除
@Override
publicvoiddelStudent(Studentstudent) {
this.getHibernateTemplate().delete(student);
}
//根据编号查询
@Override
publicStudentgetStudentById(intsid) {
returnthis.getHibernateTemplate().get(Student.class, sid);
}
//查询全部
@Override
public List<Student> getStudentAll() {
returnthis.getSession().createQuery("from Student").list();
}
}
21.创建Service接口,IStudentService:
IStudentService中的代码:
22.创建Service的实现类,StudentServiceImpl。
在类中先创建dao层的对象,并且需要getters和setters
StudentServiceImpl中的代码:
public class StudentServiceImpl implements IStudentService {
//创建dao层的对象,需要getter和setter
private IStudentDao studentDao;
@Override
public void saveStudent(Student student) {
studentDao.saveStudent(student);
}
@Override
public void updateStudent(Student student) {
studentDao.updateStudent(student);
}
@Override
public void delStudent(Student student) {
studentDao.delStudent(student);
}
@Override
public Student getStudentById(int sid) {
return studentDao.getStudentById(sid);
}
@Override
public ListgetStudentAll() {
return studentDao.getStudentAll();
}
/**
* @author Mu Xiongxiong
* @created 2020-4-30 下午2:47:37
* @return type
* 个人博客:https://blog.csdn.net/qq_34137397
*/
public IStudentDao getStudentDao() {
return studentDao;
}
/**
* @author Mu Xiongxiong
* @created 2020-4-30 下午2:47:37
* @param studentDao
* 个人博客:https://blog.csdn.net/qq_34137397
*/
public void setStudentDao(IStudentDao studentDao) {
this.studentDao = studentDao;
}
}
23.创建applicationContext-dao.xml文件(可以复制一份applicationContext.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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
">
<bean id="studentDao" class="org.dao.impl.StudentDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
</beans>
24.创建applicationContext-service.xml文件(可以复制一份applicationContext-dao.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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
">
<bean id="studentService" class="org.service.impl.StudentServiceImpl">
<property name="studentDao" ref="studentDao"></property>
</bean>
</beans>
25.创建StudentAction类,继承ActionSupport.
StudentAction里面的代码,省略展示getters和setters的方法:
26.配置Struts.xml文件:
version="1.0" encoding="UTF-8" ?>
DOCTYPEstrutsPUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.1//EN""http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="default" extends="struts-default">
<action name="StudentAll" class="org.web.StudentAction" method="StudentAll">
<result name="success">index.jsp</result>
</action>
</package>
</struts>
27.index.jsp页面,需要将学生信息用table的形式展示出来
首先在最上面添加jstl的标签库:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
写一个table表格遍历信息:
<table border="1">
<tr>
<td>学号</td>
<td>姓名</td>
<td>密码</td>
<td>电话</td>
<td>年级</td>
<td>操作</td>
</tr>
<c:forEach items="${studentList }" var="stu">
<tr>
<td>${stu.sid }</td>
<td>${stu.sname}</td>
<td>${stu.spass }</td>
<td>${stu.sphone }</td>
<td>${stu.grade.gname }</td>
<td>
<a href="getStudentByid?sid=${stu.sid }">修改a>|
<a href="delStudent?sid=${stu.sid }">删除a>
</td>
</tr>
</c:forEach>
</table>
28. 创建applicationContext-action.xml文件(可以复制一份applicationContext-dao.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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
">
<bean id="studentAction" class="org.web.StudentAction">
<property name="studentService" ref="studentService"></property>
</bean>
</beans>
29.在applicationContext.xml中引入applicationContext-dao.xml, applicationContext-service.xml, applicationContext-action.xml文件,引入方式:
运行结果: