Spring2.5+Struts2+Hibernate3环境搭建并实现简单查询

【jdbc.properties】

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/student?characterEncoding=utf-8
jdbc.user=root
jdbc.pwd=wxy1012

【hibernate.cfg.xml】

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hibernate.show_sql">false</property>
        <property name="hibernate.format_sql">false</property>
        <property name="current_session_context_class">org.springframework.orm.hibernate3.SpringSessionContext</property>
        <property name="hibernate.hbm2ddl.auto">update</property>
    </session-factory>
</hibernate-configuration>

【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.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!-- 导入资源文件 -->
    <context:property-placeholder location="classpath:jdbc.properties"/>
    
    <!-- 配置数据源 -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="username" value="${jdbc.user}"></property>
        <property name="password" value="${jdbc.pwd}"></property>
        <property name="driverClassName" value="${jdbc.driver}"></property>
        <property name="url" value="${jdbc.url}"></property>
    </bean>
    
    <!-- 配置SessionFactory -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
        <property name="mappingResources">
            <list>
                <value>com/wxy/model/Student.hbm.xml</value>
            </list>
        </property>
        <property name="packagesToScan">
            <list>
                <value>com.wxy.model</value>
            </list>
        </property>
    </bean>
    
    <!-- 配置Spring的声明式事务 -->
    <!-- 1.配置hibernate的事务管理器 -->
    <bean id="transationManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    
    <!-- 2.配置事务属性 -->
    <tx:advice id="txAdvice" transaction-manager="transationManager">
        <tx:attributes>
            <tx:method name="get*" read-only="true"></tx:method>
            <tx:method name="lastNameIsValid" read-only="true"></tx:method>
            <tx:method name="*"></tx:method>
        </tx:attributes>
    </tx:advice>
    
    <!-- 3.配置事务切入点,再把事务属性和事务切入点关联起来 -->
    <aop:config>
        <aop:pointcut expression="execution(* com.wxy.service.*.*(..))" id="txPointcut"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
    </aop:config>
    
    <!-- 开启自动扫描指定包上类的注解 -->
    <context:component-scan base-package="com.wxy"/>

</beans>

【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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name></display-name>
  
  <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:applicationContext*.xml</param-value>
  </context-param>
  
  <listener>  
    <!-- spring 监听  在启动Web容器时,自动装配spring applicationContext.xml的配置信息。 -->  
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  </listener>
  
  <filter>  
    <filter-name>struts</filter-name>  
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>  
  </filter>  
  <filter-mapping>  
    <filter-name>struts</filter-name>  
    <url-pattern>/*</url-pattern>  
  </filter-mapping>
</web-app>

【struts.xml】

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">
    
<struts>
    <constant name="struts.i18n.encoding" value="utf-8" />
    
    <package name="student" namespace="/" extends="struts-default">
        <action name="studentList" class="studentAction" method="list">
            <result name="success">/index.jsp</result>
        </action>
    </package>
</struts>

【xx.java】

package com.wxy.model;

public class Student {
    private int sno;
    private String name;
    private String gender;
    private String birthday;
    private String address;
    public int getSno() {
        return sno;
    }
    public void setSno(int sno) {
        this.sno = sno;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getGender() {
        return gender;
    }
    public void setGender(String gender) {
        this.gender = gender;
    }
    public String getBirthday() {
        return birthday;
    }
    public void setBirthday(String birthday) {
        this.birthday = birthday;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    
}

【xx.hbm.xml】

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
    
    <hibernate-mapping package="com.wxy.model">
        <class name="Student">
            <id name="sno">
                   <generator class="native"></generator>
               </id>
               <property name="name" />
               <property name="gender" />
               <property name="birthday" />
               <property name="address" />
        </class>
    </hibernate-mapping>

【xxDao.java】

package com.wxy.dao;

import java.util.List;

import com.wxy.model.Student;

public interface StudentDao {
    public List<Student> list();
}

【xxDaoImpl.java】

package com.wxy.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.SessionFactory;
import org.springframework.stereotype.Component;

import com.wxy.dao.StudentDao;
import com.wxy.model.Student;

@Component("studentDao")
public class StudentDaoImpl implements StudentDao {
    private SessionFactory sessionFactory;
    
    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    @Resource
    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    public List<Student> list()
    {
        return (List<Student>) sessionFactory.getCurrentSession().find("from Student");
    }
}

【xxService.java

package com.wxy.service;

import java.util.List;

import com.wxy.model.Student;

public interface StudentService {
    public List<Student> list();
}

【xxServiceImpl.java】

package com.wxy.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.wxy.dao.StudentDao;
import com.wxy.model.Student;
import com.wxy.service.StudentService;

@Component("studentService")
public class StudentServiceImpl implements StudentService {
    private StudentDao studentDao;
    
    public StudentDao getStudentDao() {
        return studentDao;
    }

    @Resource
    public void setStudentDao(StudentDao studentDao) {
        this.studentDao = studentDao;
    }

    public List<Student> list()
    {
        return studentDao.list();
    }
}

【xxAction.java】

package com.wxy.action;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Component;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.wxy.model.Student;
import com.wxy.service.StudentService;

@Component("studentAction")
public class StudentAction extends ActionSupport implements ModelDriven<Student>{
    private Student student = new Student();
    private StudentService studentService;
    public Student getModel()
    {
        return student;
    }
    public StudentService getStudentService() {
        return studentService;
    }
    @Resource
    public void setStudentService(StudentService studentService) {
        this.studentService = studentService;
    }
    
    public String list()
    {
        HttpServletRequest request = ServletActionContext.getRequest();
        request.setAttribute("list", studentService.list());
        
        return SUCCESS;
    }
}

【index.jsp】

<%@ page language="java" import="com.wxy.model.*,java.util.*" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
List<Student> ss = (List<Student>) request.getAttribute("list");
%>
<!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=UTF-8">
<title></title>
</head>
<body>
    <center>
    <a href="studentList.action">查询</a>
    <br>
    <table>
        <tr>
            <th>学号</th>
            <th>姓名</th>
            <th>性别</th>
            <th>生日</th>
            <th>住址</th>
        </tr>
        
        <%
        try{
        for(int i=0;i<ss.size();i++)
        {
        %>
        <tr>
            <td><%=ss.get(i).getSno() %></td>
            <td><%=ss.get(i).getName() %></td>
            <td><%=ss.get(i).getGender() %></td>
            <td><%=ss.get(i).getBirthday() %></td>
            <td><%=ss.get(i).getAddress() %></td>
        </tr>
        <%
        }}catch(Exception e){}
        %>
    </table>
    </center>
</body>
</html>
原文地址:https://www.cnblogs.com/wxyxxx/p/6964140.html