package com.zl.entity; import java.util.Date; /** * Emp entity. * * @author MyEclipse Persistence Tools */ public class Emp implements java.io.Serializable { // Fields private Long empno; private String ename; private String job; private Long mgr; private Date hiredate; private Double sal; private Double comm; // Constructors /** default constructor */ public Emp() { } // Property accessors public Long getEmpno() { return this.empno; } public void setEmpno(Long empno) { this.empno = empno; } public String getEname() { return this.ename; } public void setEname(String ename) { this.ename = ename; } public String getJob() { return this.job; } public void setJob(String job) { this.job = job; } public Long getMgr() { return this.mgr; } public void setMgr(Long mgr) { this.mgr = mgr; } public Date getHiredate() { return this.hiredate; } public void setHiredate(Date hiredate) { this.hiredate = hiredate; } public Double getSal() { return this.sal; } public void setSal(Double sal) { this.sal = sal; } public Double getComm() { return this.comm; } public void setComm(Double comm) { this.comm = comm; } }
注:applicationContext-action.xml,applicationContext-biz.xml,applicationContext-common.xml,applicationContext-dao.xml,hibernate.cfg.xml都放在src目录;
package com.zl.entity; /** * Usertab entity. * * @author MyEclipse Persistence Tools */ public class Usertab implements java.io.Serializable { // Fields private UsertabId id; // Constructors /** default constructor */ public Usertab() { } /** full constructor */ public Usertab(UsertabId id) { this.id = id; } // Property accessors public UsertabId getId() { return this.id; } public void setId(UsertabId id) { this.id = id; } }
package com.zl.dao; import java.util.List; import com.zl.entity.Emp; public interface EmpDao { public void addEmp(Emp e); public void updateEmpByEmpNo(int empNo); public void deleteEmpByEmpNo(int empNo); public List<Emp> selectAll(); }
package com.zl.dao.impl; import java.util.List; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import com.zl.dao.EmpDao; import com.zl.entity.Emp; public class EmpDaoImpl extends HibernateDaoSupport implements EmpDao { public void addEmp(Emp e) { // TODO Auto-generated method stub } public void deleteEmpByEmpNo(int empNo) { // TODO Auto-generated method stub } public List<Emp> selectAll() { return this.getSession().createQuery("from Emp").list(); } public void updateEmpByEmpNo(int empNo) { // TODO Auto-generated method stub } }
package com.zl.biz; import java.util.List; import com.zl.entity.Emp; public interface EmpBiz { public void addEmp(Emp e); public void updateEmpByEmpNo(int empNo); public void deleteEmpByEmpNo(int empNo); public List<Emp> selectAll(); }
package com.zl.biz.impl; import java.util.List; import com.zl.biz.EmpBiz; import com.zl.dao.EmpDao; import com.zl.entity.Emp; public class EmpBizImpl implements EmpBiz { private EmpDao empdao; public void setEmpdao(EmpDao empdao) { this.empdao = empdao; } public void addEmp(Emp e) { // TODO Auto-generated method stub } public void deleteEmpByEmpNo(int empNo) { // TODO Auto-generated method stub } public List<Emp> selectAll() { return empdao.selectAll(); } public void updateEmpByEmpNo(int empNo) { // TODO Auto-generated method stub } }
package com.zl.action; import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.apache.struts.actions.DispatchAction; import com.zl.biz.EmpBiz; import com.zl.entity.Emp; public class EmpAction extends DispatchAction { private EmpBiz empbiz; public void setEmpbiz(EmpBiz empbiz) { this.empbiz = empbiz; } public ActionForward show(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { List<Emp> listemp=empbiz.selectAll(); request.setAttribute("listemp", listemp); return null; } }
package com.zl.form; import org.apache.struts.action.ActionForm; import com.zl.entity.Emp; public class EmpForm extends ActionForm { private Emp emp=new Emp(); public Emp getEmp() { return emp; } public void setEmp(Emp emp) { this.emp = emp; } }
package com.zl.util; import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.cfg.Configuration; /** * Configures and provides access to Hibernate sessions, tied to the * current thread of execution. Follows the Thread Local Session * pattern, see {@link http://hibernate.org/42.html }. */ public class HibernateSessionFactory { /** * Location of hibernate.cfg.xml file. * Location should be on the classpath as Hibernate uses * #resourceAsStream style lookup for its configuration file. * The default classpath location of the hibernate config file is * in the default package. Use #setConfigFile() to update * the location of the configuration file for the current session. */ private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml"; private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>(); private static Configuration configuration = new Configuration(); private static org.hibernate.SessionFactory sessionFactory; private static String configFile = CONFIG_FILE_LOCATION; static { try { configuration.configure(configFile); sessionFactory = configuration.buildSessionFactory(); } catch (Exception e) { System.err .println("%%%% Error Creating SessionFactory %%%%"); e.printStackTrace(); } } private HibernateSessionFactory() { } /** * Returns the ThreadLocal Session instance. Lazy initialize * the <code>SessionFactory</code> if needed. * * @return Session * @throws HibernateException */ public static Session getSession() throws HibernateException { Session session = (Session) threadLocal.get(); if (session == null || !session.isOpen()) { if (sessionFactory == null) { rebuildSessionFactory(); } session = (sessionFactory != null) ? sessionFactory.openSession() : null; threadLocal.set(session); } return session; } /** * Rebuild hibernate session factory * */ public static void rebuildSessionFactory() { try { configuration.configure(configFile); sessionFactory = configuration.buildSessionFactory(); } catch (Exception e) { System.err .println("%%%% Error Creating SessionFactory %%%%"); e.printStackTrace(); } } /** * Close the single hibernate session instance. * * @throws HibernateException */ public static void closeSession() throws HibernateException { Session session = (Session) threadLocal.get(); threadLocal.set(null); if (session != null) { session.close(); } } /** * return session factory * */ public static org.hibernate.SessionFactory getSessionFactory() { return sessionFactory; } /** * return session factory * * session factory will be rebuilded in the next call */ public static void setConfigFile(String configFile) { HibernateSessionFactory.configFile = configFile; sessionFactory = null; } /** * return hibernate configuration * */ public static Configuration getConfiguration() { return configuration; } }
hibernate.cfg.xml: <?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <!-- Generated by MyEclipse Hibernate Tools. --> <hibernate-configuration> <session-factory> <property name="connection.username">hello</property> <property name="connection.url"> jdbc:oracle:thin:@localhost:1521:orcl </property> <property name="dialect"> org.hibernate.dialect.Oracle9Dialect </property> <property name="myeclipse.connection.profile">oracle</property> <property name="connection.password">123</property> <property name="connection.driver_class"> oracle.jdbc.driver.OracleDriver </property> <property name="show_sql">true</property> <property name="format_sql">true</property> <mapping resource="com/zl/entity/Emp.hbm.xml" /> <mapping resource="com/zl/entity/Usertab.hbm.xml"></mapping> </session-factory> </hibernate-configuration>
struts-config.xml: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd"> <struts-config> <data-sources /> <form-beans> <form-bean name="empForm" type="com.zl.form.EmpForm"></form-bean> </form-beans> <global-exceptions /> <global-forwards /> <action-mappings> <action path="/empdo" name="empForm" scope="request" type="org.springframework.web.struts.DelegatingActionProxy" parameter="method" > <forward name="index" path="/index.jsp"></forward> </action> </action-mappings> <message-resources parameter="com.yourcompany.struts.ApplicationResources" /> </struts-config>
web.xml: <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext-*.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>action</servlet-name> <servlet-class>org.apache.struts.action.ActionServlet</servlet-class> <init-param> <param-name>config</param-name> <param-value>/WEB-INF/struts-config.xml</param-value> </init-param> <init-param> <param-name>debug</param-name> <param-value>3</param-value> </init-param> <init-param> <param-name>detail</param-name> <param-value>3</param-value> </init-param> <load-on-startup>0</load-on-startup> </servlet> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
index.jsp: <%@ page language="java" isELIgnored="false" import="java.util.*" pageEncoding="UTF-8"%> <%@taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %> <% 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"> <!-- contentType="application/vnd.ms-excel" --> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> <jsp:include page="/empdo.do?method=show" /> </head> <body> <center><h1>员工管理</h1> <table border="1"> <tr> <td>员工编号</td> <td>员工名称</td> <td>员工工作</td> <td>员工上级</td> <td>入职时间</td> <td>员工薪水</td> <td>额外薪水</td> <td>操作</td> </tr> <logic:iterate id="e" name="listemp"> <tr> <td>${e.empno}</td> <td>${e.ename }</td> <td>${e.job }</td> <td>${e.mgr }</td> <td>${e.hiredate} </td> <td>${e.sal} </td> <td>${e.comm} </td> <td><a href="add.jsp">添加</a>/<a href="#">删除</a>/<a href="#">修改</a></td> </tr> </logic:iterate> </table> </center> </body> </html>
com.zl.entity/Emp.hbm.xml <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Mapping file autogenerated by MyEclipse Persistence Tools --> <hibernate-mapping> <class name="com.zl.entity.Emp" table="EMP" schema="SCOTT"> <id name="empno" type="java.lang.Long"> <column name="EMPNO" precision="4" scale="0" /> <generator class="sequence" /> </id> <property name="ename" type="java.lang.String"> <column name="ENAME" length="10" /> </property> <property name="job" type="java.lang.String"> <column name="JOB" length="9" /> </property> <property name="mgr" type="java.lang.Long"> <column name="MGR" precision="4" scale="0" /> </property> <property name="hiredate" type="java.util.Date"> <column name="HIREDATE" length="7" /> </property> <property name="sal" type="java.lang.Double"> <column name="SAL" precision="7" /> </property> <property name="comm" type="java.lang.Double"> <column name="COMM" precision="7" /> </property> </class> </hibernate-mapping>
com.zl.entity/Usertab.hbm.xml: <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Mapping file autogenerated by MyEclipse Persistence Tools --> <hibernate-mapping> <class name="com.zl.entity.Usertab" table="USERTAB" schema="SCOTT"> <composite-id name="id" class="com.zl.entity.UsertabId"> <key-property name="userid" type="java.lang.Long"> <column name="USERID" precision="22" scale="0" /> </key-property> <key-property name="username" type="java.lang.String"> <column name="USERNAME" length="30" /> </key-property> <key-property name="userpwd" type="java.lang.String"> <column name="USERPWD" length="30" /> </key-property> </composite-id> </class> </hibernate-mapping>
applicationContext-dao.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="configLocation" value="classpath:hibernate.cfg.xml"> </property> </bean> <bean id="empdao" class="com.zl.dao.impl.EmpDaoImpl"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> </beans>
applicationContext-biz.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> <bean id="empbiz" class="com.zl.biz.impl.EmpBizImpl"> <property name="empdao" ref="empdao"></property> </bean> </beans>
applicationContext-action.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> <bean name="/empdo" class="com.zl.action.EmpAction"> <property name="empbiz" ref="empbiz" ></property> </bean> </beans>
applicationContext-common.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: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-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd "> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="configLocation" value="classpath:hibernate.cfg.xml"> </property> </bean> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="add*" propagation="REQUIRED"/> <tx:method name="update*" propagation="REQUIRED"/> <tx:method name="delete*" propagation="REQUIRED"/> <tx:method name="*" read-only="true"/> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut id="pointCut" expression="execution(* com.zl.biz.impl.*.*(..))"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="pointCut"/> </aop:config> </beans>
SSH框架
最新推荐文章于 2024-09-30 10:21:58 发布