总结一些,配置struts2+spring+ibatis项目开发环境需要的几个jar: struts2-core-2.0.11.jar(struts2框架) commons-lang.jar (struts2基础设施1,提供java常用操作API,比如字符串处理,XML解析等) commons-logging.jar(struts2基础设施2,提供java日志操作API,抽象出日志接口,方便操作) freemarker-2.3.8.jar(struts2基础设施3,freemarker模板视图支持,struts2标签主题支持) xwork-2.0.4.jar(struts2基础设施4,命令模式框架支持) ognl-2.6.11.jar(struts2基础设施5,OGNL表达式支持) struts2-spring-plugin-2.0.11.jar (struts2基础设施6,struts集成spring插件) struts2-sitemesh-plugin-2.0.11.jar(struts2基础设施7,struts集成sitemesh插件) spring.jar(spring框架) ibatis-2.3.0.677.jar (ibatis框架) sitemesh-2.3.jar(sitemesh框架)
package com.zl.entity;import java.util.Date;/** * Emp entity. * * @author MyEclipse Persistence Tools */public class Emp implements java.io.Serializable {// Fieldsprivate 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 accessorspublic 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;}}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"> <param name="" ></param> </generator> </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>
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) { this.getSession().save(e); } public void deleteEmpByEmpNo(int empNo) { this.getSession().createQuery("delete from Emp where empno=?").setParameter(0,empNo).executeUpdate(); } public List<Emp> selectAll() { return this.getSession().createQuery("from Emp").list(); } public void updateEmpByEmpNo(int empNo) { Emp emp=(Emp)this.getSession().get(Emp.class, empNo); this.getSession().update(emp); } }
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) { empdao.addEmp(e); } public void deleteEmpByEmpNo(int empNo) { empdao.deleteEmpByEmpNo(empNo); } public List<Emp> selectAll() { return empdao.selectAll(); } public void updateEmpByEmpNo(int empNo) { empdao.updateEmpByEmpNo(empNo); } }
package com.zl.action; import java.util.ArrayList; import java.util.List; import com.opensymphony.xwork2.ActionSupport; import com.zl.biz.EmpBiz; import com.zl.entity.Emp; public class EmpAction extends ActionSupport{ private EmpBiz empbiz; public void setEmpbiz(EmpBiz empbiz) { this.empbiz = empbiz; } List<Emp> listemp=new ArrayList<Emp>(); private Emp emp=new Emp(); public Emp getEmp() { return emp; } public void setEmp(Emp emp) { this.emp = emp; } /** * 查询 * @return */ public String selectAll(){ listemp=empbiz.selectAll(); return "index"; } /** * 添加 * @return */ public String add(){ empbiz.addEmp(emp); return "index"; } /** * 删除 * @return */ public String delete(){ return "index"; } /** * 修改 * @return */ public String update(){ return "index"; } public List<Emp> getListemp() { return listemp; } public void setListemp(List<Emp> listemp) { this.listemp = listemp; } }
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">aa</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" /> </session-factory> </hibernate-configuration>
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="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="EmpAction" 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="delete*" propagation="REQUIRED"/> <tx:method name="update" 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>
struts.xml: <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <!-- 默认的 --> <constant name="" value=""></constant> <package name="mypackage" namespace="/test" extends="struts-default"> <action name="EmpAction" class="EmpAction"> <result name="index">/index.jsp</result> </action> </package> </struts>
test/com.zl.biz.implTest.EmpBizImplTest.java: /** * */ package com.zl.biz.implTest; import static org.junit.Assert.*; import java.util.List; import org.hibernate.Session; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; import com.zl.biz.impl.EmpBizImpl; import com.zl.entity.Emp; import com.zl.util.HibernateSessionFactory; /** * @author zhang * */ public class EmpBizImplTest { static Session session = null; @BeforeClass public static void befreClss(){ session=HibernateSessionFactory.getSession(); session.beginTransaction(); } @Test public void selecttest(){ String hql="from Emp"; List<Emp> list=session.createQuery(hql).list(); for (Emp emp : list) { System.out.println(emp.getEname()); } } @AfterClass public static void afterClass(){ session.getTransaction().commit(); HibernateSessionFactory.closeSession(); } }
web.xml: <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <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> <filter> <filter-name>filterDispacher</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> </filter> <filter-mapping> <filter-name>filterDispacher</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
add.jsp: <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@taglib uri="/struts-tags" prefix="s"%> <% 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> <center><h1>添加员工</h1> <form action="" method="post"> 员工名称:<input type="text" name=""><br> 员工工作:<input type="text" name=""><br> 员工上级:<input type="text" name=""><br> 入职时间:<input type="text" name=""><br> 员工薪水:<input type="text" name=""><br> 额外薪水:<input type="text" name=""><br> <input type="submit" value="提交"><br> </form> </center> </body> </html>
index.jsp: <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@taglib uri="/struts-tags" prefix="s"%> <% 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"> --> <!-- contentType="application/vnd.ms-excel" --> </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> <s:iterator value="listemp" id="e" status="status"> <tr> <td><s:property value="#request.e.empno"/></td> <td><s:property value="#request.e.ename"/></td> <td><s:property value="#request.e.job"/></td> <td><s:property value="#request.e.mgr"/></td> <td><s:property value="#request.e.hiredate"/></td> <td><s:property value="#request.e.sal"/></td> <td><s:property value="#request.e.comm"/></td> <td><a href="add.jsp">添加</a>/<a href="#">删除</a>/<a href="#">修改</a></td> </tr> </s:iterator> </table> </center> </body> </html>
SSH2框架
最新推荐文章于 2019-02-21 21:12:25 发布