快速搭建ssh项目
程序员文章站
2022-06-20 08:48:57
环境:oracle11g、myeclipse2014 首先在web项目中添加spring框架 现在已经添加完spring框架了 然后我们开始添加Hibernate框架 到这一步Hibernate框架就添加完成了 applicationContext.xml配置文件
环境:oracle11g、myeclipse2014
首先在web项目中添加spring框架
现在已经添加完spring框架了
然后我们开始添加hibernate框架
到这一步hibernate框架就添加完成了
applicationcontext.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:context="http://www.springframework.org/schema/context" 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/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"> <bean id="datasource" class="org.apache.commons.dbcp.basicdatasource"> <property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:orcl"> </property> <property name="username" value="system"></property> <property name="password" value="1234"></property> <property name="driverclassname" value="oracle.jdbc.driver.oracledriver"></property> </bean> <bean id="sessionfactory" class="org.springframework.orm.hibernate3.annotation.annotationsessionfactorybean"> <property name="datasource"> <ref bean="datasource" /> </property> <property name="hibernateproperties"> <props> <prop key="hibernate.dialect"> org.hibernate.dialect.oracle9dialect </prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> </props> </property> <!-- <property name="annotatedclasses"> <list> <value>com.bdqn.pojo.dept</value> <value>com.bdqn.pojo.emp</value> </list> </property> --> <property name="packagestoscan" value="com.bdqn.pojo"></property> </bean> <context:component-scan base-package="com.bdqn" /> <bean id="txmanager" class="org.springframework.orm.hibernate3.hibernatetransactionmanager"> <property name="sessionfactory" ref="sessionfactory"></property> </bean> <tx:annotation-driven transaction-manager="txmanager" /> </beans>
然后开始配置struts2框架
到现在struts2框架也整合进来了
然后使用hibernate的反向工程创建实体类
点击finish,然后实体类就创建好了,接下来就可以开始写代码了
dao层接口
package com.bdqn.dao; import java.util.list; import com.bdqn.pojo.emp; public interface empdao { public list<emp> findall(); }
dao层实现
package com.bdqn.dao.impl; import java.util.list; import org.hibernate.sessionfactory; import org.springframework.beans.factory.annotation.autowired; import org.springframework.beans.factory.annotation.qualifier; import org.springframework.orm.hibernate3.support.hibernatedaosupport; import org.springframework.stereotype.repository; import com.bdqn.dao.empdao; import com.bdqn.pojo.emp; @repository("empdao") public class empdaoimpl extends hibernatedaosupport implements empdao { @autowired public empdaoimpl(@qualifier("sessionfactory") sessionfactory sessionfactory) { this.setsessionfactory(sessionfactory); } public empdaoimpl() { } @override public list<emp> findall() { // todo auto-generated method stub return this.gethibernatetemplate().find("from emp"); } }
service层接口
package com.bdqn.service; import java.util.list; import com.bdqn.pojo.emp; public interface empservice { public list<emp> findall(); }
service层实现
package com.bdqn.service.impl; import java.util.list; import org.springframework.beans.factory.annotation.autowired; import org.springframework.stereotype.service; import org.springframework.transaction.annotation.transactional; import com.bdqn.dao.empdao; import com.bdqn.pojo.emp; import com.bdqn.service.empservice; @service("empservice") @transactional public class empserviceimpl implements empservice { @autowired private empdao empdao; public empdao getempdao() { return empdao; } public void setempdao(empdao empdao) { this.empdao = empdao; } @override @transactional(readonly = true) public list<emp> findall() { // todo auto-generated method stub return empdao.findall(); } }
action(web):
package com.bdqn.web; import java.util.list; import java.util.map; import org.springframework.beans.factory.annotation.autowired; import org.springframework.stereotype.controller; import com.bdqn.pojo.emp; import com.bdqn.service.empservice; import com.opensymphony.xwork2.actioncontext; import com.opensymphony.xwork2.actionsupport; @controller public class empaction extends actionsupport { @autowired private empservice empservice; public empservice getempservice() { return empservice; } public void setempservice(empservice empservice) { this.empservice = empservice; } public string execute(){ map<string, object> request = (map<string, object>) actioncontext.getcontext().get("request"); list<emp> emps = empservice.findall(); request.put("emps", emps); return success; } }
struts.xml配置文件
<?xml version="1.0" encoding="utf-8" ?> <!doctype struts public "-//apache software foundation//dtd struts configuration 2.1//en" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <package name="def" namespace="/" extends="struts-default"> <action name="empaction" class="com.bdqn.web.empaction"> <result>/index.jsp</result> </action> </package> </struts>
jsp页面:
<%@ page language="java" import="java.util.*" pageencoding="utf-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <% string path = request.getcontextpath(); string basepath = request.getscheme()+"://"+request.getservername()+":"+request.getserverport()+path+"/"; %> <!doctype html public "-//w3c//dtd html 4.01 transitional//en"> <html> <head> <base href="<%=basepath%>"> <title>my jsp 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="this is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <c:foreach items="${emps}" var="emp"> ${emp.ename }<br/> </c:foreach> </body> </html>
到这一步恭喜,你的ssh项目搭建完了
然后部署项目发布
在浏览器上输入http://localhost:8080/你的项目命/empaction.action
上一篇: 中学生计算机程序设计【入门例1.1】
下一篇: Java自学-集合框架 二叉树