spring 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:aop="http://www.springframework.org/schema/aop" 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-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-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"> <!--从hibernate.cfg.xml中拿到session工厂--> <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="get*" read-only="true"/> <tx:method name="*" /> </tx:attributes> </tx:advice> <!--配置切入点和适配器--> <aop:config> <aop:pointcut expression="execution(* org.pxw.service.impl.*.*(..))" id="txpointcut"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="txpointcut"/> </aop:config>
<!--annotation注入--> <context:annotation-config></context:annotation-config> <!--dao--> <bean id="personDao" class="org.pxw.dao.imple.PersonDaoImple"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <bean id="personService" class="org.pxw.dao.service.imple.PersonServiceImple"> <property name="personDao" ref="personDao"></property> </bean> <bean id="personAction" class="org.pxw.web.action.PersonAction"></bean> </beans>
Person.hbm.xml的配置Person实体的代码:
package org.pxw.entity; public class Person { private Integer pid; private String pname; private Integer page; public Integer getPid() { return pid; } public void setPid(Integer pid) { this.pid = pid; } public String getPname() { return pname; } public void setPname(String pname) { this.pname = pname; } public Integer getPage() { return page; } public void setPage(Integer page) { this.page = page; } }
person.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>
<!--对应的person实体的映射--> <class name="org.pxw.entity.Person" table="PERSON" schema="SCOTT"> <id name="pid" type="java.lang.Integer"> <column name="pid" precision="5" scale="0" /> <generator class="increment" /> </id> <property name="pname" type="java.lang.String"> <column name="pname" length="12" /> </property> <property name="page" type="java.lang.Integer"> <column name="page" length="3" /> </property> </class> </hibernate-mapping>
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="dialect"> org.hibernate.dialect.Oracle9Dialect </property> <property name="connection.url"> jdbc:oracle:thin:@localhost:1521:orcl </property> <property name="connection.username">scott</property> <property name="connection.password">tiger</property> <property name="connection.driver_class"> oracle.jdbc.driver.OracleDriver </property> <property name="myeclipse.connection.profile">oracle</property> <property name="show_sql">true</property> <!--对应的实体类的映射文件XX.cfg.xml--> <mapping resource="org/pxw/entity/Person.hbm.xml" /> </session-factory> </hibernate-configuration>
PersonDaoImple.java
import java.util.List; import org.pxw.dao.PersonDao; import org.pxw.entity.Person; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; public class PersonDaoImple extends HibernateDaoSupport implements PersonDao{ @SuppressWarnings("unchecked") @Override public List<Person> findall() { // TODO Auto-generated method stub String sql = "select p from Person p"; return this.getHibernateTemplate().find(sql); } }
PersonServiceImple.java
import java.util.List; import org.pxw.dao.PersonDao; import org.pxw.dao.service.PersonService; import org.pxw.entity.Person; public class PersonServiceImple implements PersonService { private PersonDao personDao; //set注入 public void setPersonDao(PersonDao personDao) { this.personDao = personDao; } @Override public List<Person> findall() { // TODO Auto-generated method stub return personDao.findall(); } }
PersonAction.java
package org.pxw.web.action; import java.util.List; import javax.annotation.Resource; import org.pxw.dao.PersonDao; import org.pxw.dao.service.PersonService; import org.pxw.entity.Person; import com.opensymphony.xwork2.ActionSupport; public class PersonAction extends ActionSupport { @Resource(name="personService") private PersonService personService; private List<Person> persons; public List<Person> getPersons() { return persons; } public void setPersons(List<Person> persons) { this.persons = persons; } @Override public String execute() throws Exception { System.out.println(1000); setPersons(personService.findall()) ; 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="struts" namespace="/" extends="struts-default"> <action name="person" class="personAction"> <result name="success">login.jsp</result> </action> </package> </struts>
web.xml配置
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>s2sh</display-name> <filter> <filter-name>OpenSessionInViewFilter</filter-name> <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class> </filter> <filter-mapping> <filter-name>OpenSessionInViewFilter</filter-name> <url-pattern>*.action</url-pattern> </filter-mapping> <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> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <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>*.action</url-pattern> </filter-mapping> </web-app>
jsp页面:
index.jsp页面: <a href="person.action">查询所有</a> login.jsp: <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" import="java.util.*"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> <table> <tr> <td>编号</td> <td>姓名</td> <td>年龄</td> </tr> <c:forEach items="${persons}" var="p"> <tr> <td>${p.pid}</td> <td>${p.pname}</td> <td>${p.page}</td> </tr> </c:forEach> </table> </body> </html>
所需要的包不是最少的包: