==============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">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:application*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
=========================================================
===================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:context="http://www.springframework.org/schema/context"
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.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<!-- 数据源 -->
<bean name="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="org.gjt.mm.mysql.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/zhtt"></property>
<property name="password" value="zhtt"></property>
<property name="username" value="root"/>
<property name="initialSize" value="3"> </property>
<property name="maxActive"> <value>100</value> </property>
<property name="maxIdle"> <value>5</value> </property>
<property name="maxWait"> <value>10</value> </property>
</bean>
<!-- sessionFactory -->
<bean name="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="mappingResources">
<list>
<value>pojo/Dept.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<!-- 配置事务管理器 -->
<bean id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 通过AOP规定哪些类的方法需要用到事务 -->
<aop:config>
<aop:pointcut id="deptDao"
expression="execution(* dao.DeptDaoImpl.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="deptDao" />
</aop:config>
<!-- 通过建言配置具体方法所对应的事务 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="insert" propagation="REQUIRED"/>
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="find*" propagation="REQUIRED"/>
<tx:method name="*" read-only="true"/>
</tx:attributes>
</tx:advice>
<bean name="deptDaoImpl" class="dao.DeptDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean name="deptServiceImpl" class="service.DeptServiceImpl">
<property name="deptDao" ref="deptDaoImpl"></property>
</bean>
<bean name="deptAction" class="action.DeptAction">
<property name="deptService" ref="deptServiceImpl"></property>
</bean>
</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>
<package name="dept" namespace="/" extends="struts-default">
<action name="*" method="{1}" class="deptAction">
<result name="queryAll">/deptList.jsp</result>
</action>
</package>
</struts>
==============================================
==============dao=============================
public interface DeptDao { public List<Dept> queryAll();}
import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import pojo.Dept;
public class DeptDaoImpl extends HibernateDaoSupport implements DeptDao{
public List<Dept> queryAll() {
List<Dept> list=this.getHibernateTemplate().find("from Dept");
return list;
}
}
======================================================
-----------------------------------pojo-----------------------------------------------------
public class Dept {
private int deptno;
private String dname;
private String dloc;}
==============================================
---------------DeptPojo.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">
<hibernate-mapping>
<class name="pojo.Dept" table="dept">
<id name="deptno">
<generator class="increment"></generator>
</id>
<property name="dname"></property>
<property name="dloc"/>
</class>
</hibernate-mapping>
=================================================
--------------------------------service----------------------------------
public interface DeptService {
public List<Dept> queryAll();
}
-----------------------------------
import java.util.List;
import pojo.Dept;
import dao.DeptDao;
public class DeptServiceImpl implements DeptService{
private DeptDao deptDao;
public DeptDao getDeptDao() {
return deptDao;
}
public void setDeptDao(DeptDao deptDao) {
this.deptDao = deptDao;
}
public List<Dept> queryAll(){
return this.deptDao.queryAll();
}
}
=================================================
---------------------------------------------action-------------------------------------
package action;
import java.util.List;
import pojo.Dept;
import service.DeptService;
import com.opensymphony.xwork2.ActionSupport;
public class DeptAction extends ActionSupport{
private DeptService deptService;
private List<Dept> deptList;
public List<Dept> getDeptList() {
return deptList;
}
public void setDeptList(List<Dept> deptList) {
this.deptList = deptList;
}
public DeptService getDeptService() {
return deptService;
}
public void setDeptService(DeptService deptService) {
this.deptService = deptService;
}
public String queryAll(){
deptList=this.deptService.queryAll();
System.out.println(deptList.size());
return "queryAll";
}
}
========================================
-------------.jsp---------------------------------------
<body>
<a href="queryAll">查看</a>
</body>
---------------
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>My JSP 'deptList.jsp' starting page</title>
</head>
<body>
<c:forEach items="${requestScope.deptList}" var="dept">
deptno:${dept.deptno} || dname: ${dept.dname } || dloc:${dept.dloc }<br>
</c:forEach>
</body>
</html>