搭建ssh框架的步骤
1.創(chuàng)建web項(xiàng)目
2.導(dǎo)入ssh 所需要的多有jar包,到web-inf下面的lib里面
3.將導(dǎo)入過(guò)來(lái)的jar包都build–path一下
4.切換到myeclipse database視圖中,添加鏈接數(shù)據(jù)庫(kù)的鏈接
5.新建一個(gè)數(shù)據(jù)庫(kù)連接:
常用數(shù)據(jù)庫(kù)連接字符串:https://blog.csdn.net/qq_34137397/article/details/55548094
6.切換視圖,在src下面新建一個(gè)名為org.entity的包:
7.添加hibernate,右擊項(xiàng)目名,選擇myeclipse?add HIbernaete ……
在自動(dòng)創(chuàng)建的hibernate.cfg.xml文件中,新加兩行代碼,實(shí)現(xiàn)打印輸出sql語(yǔ)句和格式化sql語(yǔ)句的功能。
true
true
8.右擊項(xiàng)目,添加struts
9.添加spring的內(nèi)容:
10.web.xml里面的內(nèi)容:
<?xml version="1.0" encoding="UTF-8"?> org.springframework.web.context.ContextLoaderListener contextConfigLocation classpath:applicationContext.xml openSessionInViewFilter org.springframework.orm.hibernate3.support.OpenSessionInViewFilter <init-param> <param-name>flushMode</param-name> <param-value>AUTO</param-value> </init-param> openSessionInViewFilter /* struts2 org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter struts2 /* 404 /errorPage.jsp index.jsp11.配置spring的內(nèi)容,打開(kāi)applicationContext.xml文件:
<?xml version="1.0" encoding="UTF-8"?> <!-- sessionFactory --> <bean id="sessionFactory"class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"><property name="configLocation"value="classpath:hibernate.cfg.xml"></property> </bean> <!-- 配置事務(wù) --> <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><!-- 切入點(diǎn) --> <aop:config><aop:pointcut expression="execution(* org.service..*.*(..))" id="mycut"/><aop:advisor advice-ref="txAdvice" pointcut-ref="mycut"/> </aop:config></beans>12.切換到myeclipse database視圖:(反向生成實(shí)體類(lèi))
13.右擊表:
點(diǎn)擊finish完成即可。
14.切換視圖至myeclipsep perspective
15.將項(xiàng)目發(fā)布到tomcat中。
16.啟動(dòng)tomcat服務(wù),檢查控制臺(tái)是否有錯(cuò)誤(一般只要控制臺(tái)中沒(méi)有超鏈接錯(cuò)誤,正常顯示毫秒數(shù)即可)。
17.如果沒(méi)有錯(cuò)誤,將服務(wù)關(guān)掉。
18.開(kāi)始根據(jù)實(shí)體類(lèi)寫(xiě)接口,一般一個(gè)實(shí)體類(lèi)對(duì)應(yīng)一個(gè)Dao接口
19.在IStudentDao接口中寫(xiě)增刪改查的抽象方法。
20.開(kāi)始寫(xiě)Dao層的實(shí)現(xiàn)類(lèi),新建一個(gè)StudentDaoImpl的實(shí)現(xiàn)類(lèi)。需要繼承HibernateDaoSupport類(lèi),實(shí)現(xiàn)IStudentDao接口。
實(shí)現(xiàn)類(lèi)中的代碼:
public class StudentDaoImpl extends HibernateDaoSupport implements IStudentDao {
}
21.創(chuàng)建Service接口,IStudentService:
IStudentService中的代碼:
22.創(chuàng)建Service的實(shí)現(xiàn)類(lèi),StudentServiceImpl。
在類(lèi)中先創(chuàng)建dao層的對(duì)象,并且需要getters和setters
StudentServiceImpl中的代碼:
public class StudentServiceImpl implements IStudentService {
//創(chuàng)建dao層的對(duì)象,需要getter和setter
private IStudentDao studentDao;
}
23.創(chuàng)建applicationContext-dao.xml文件(可以復(fù)制一份applicationContext.xml一份,對(duì)應(yīng)的在改一下),代碼如下:
<?xml version="1.0" encoding="UTF-8"?>
24.創(chuàng)建applicationContext-service.xml文件(可以復(fù)制一份applicationContext-dao.xml一份,對(duì)應(yīng)的在改一下),代碼如下:
<?xml version="1.0" encoding="UTF-8"?>
25.創(chuàng)建StudentAction類(lèi),繼承ActionSupport.
StudentAction里面的代碼,省略展示getters和setters的方法:
26.配置Struts.xml文件:
<?xml version="1.0" encoding="UTF-8" ?> index.jsp27.index.jsp頁(yè)面,需要將學(xué)生信息用table的形式展示出來(lái)
首先在最上面添加jstl的標(biāo)簽庫(kù):
<%@ taglib uri=“http://java.sun.com/jsp/jstl/core” prefix=“c” %>
寫(xiě)一個(gè)table表格遍歷信息:
<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>| 學(xué)號(hào) | 姓名 | 密碼 | 電話(huà) | 年級(jí) | 操作 |
29.在applicationContext.xml中引入applicationContext-dao.xml, applicationContext-service.xml, applicationContext-action.xml文件,引入方式:
運(yùn)行結(jié)果:
總結(jié)
以上是生活随笔為你收集整理的搭建ssh框架的步骤的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 手机sd卡无法读取原因是什么
- 下一篇: 《金色梦乡》金句摘抄(八)