SSH实例(2)

在WebContentWEB-INF下新建两个文件:applicationContext.xml和web.xml。

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  
 <filter>
  <filter-name>struts2</filter-name>
  <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
 </filter>
 
 <filter-mapping>
  <filter-name>struts2</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>
 
 <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener> 
 
 <welcome-file-list> 
     <welcome-file>index.jsp</welcome-file>
 </welcome-file-list> 
  
</web-app>

web.xml指定了filter和listener。

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:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    <!-- 配置SessionFactory -->
     <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" >
            <ref local="dataSource"/>
        </property>
        <!-- 配置Hibernate的属性 -->
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>
        <!-- 指定Hibernate映射文件的路径 -->
        <property name="mappingResources">
            <list>
                <value>com/school/entity/Clas.hbm.xml</value>
            </list>
        </property>
     </bean>
     <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName">
            <value>com.mysql.jdbc.Driver</value>
        </property>
        <property name="url">
            <value>jdbc:mysql://localhost:3306/myssh
            </value>
        </property>
        <property name="username">
            <value>root</value>
        </property>
        <property name="password">
            <value>root</value>
        </property>
    </bean>
    <bean id="clasDAO"
         class="com.school.dao.ClasDAOImpl"
         abstract="false" lazy-init="default" autowire="default">
         <property name="sessionFactory">
             <ref bean="sessionFactory" />
         </property>
     </bean>    
    <bean id="clasService" class="com.school.service.ClasServiceImpl">
         <property name="clasDAO" ref="clasDAO"></property>
    </bean> 
    <bean id="clasQueryAction" class="com.school.action.ClasQueryAction">
         <property name="clasService" ref="clasService"></property>
     </bean>
    <bean id="clasAction" class="com.school.action.ClasAction">
         <property name="clasService" ref="clasService"></property>
     </bean>
 </beans>

applicationContext.xml定义了多个bean,其中dataSource定义了连接数据库的url、用户名、密码等属性。sessionFactory配置了Hibernate的属性以及映射文件的路径,映射的com/school/entity/Clas.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="com.school.entity.Clas" table="clas">
        <id name="id" type="java.lang.Integer">
            <column name="ID" precision="22" scale="0" />
            <generator class="identity" />
        </id>
        <property name="name" type="java.lang.String">
            <column name="NAME"  length="50" not-null="true">
                <comment>课程名称</comment>
            </column>
        </property>
        <property name="comment" type="java.lang.String">
            <column name="COMMENT" length="500" not-null="false">
                <comment>课程介绍</comment>
            </column>
        </property>
    </class>
</hibernate-mapping>

该文件对应MySQL数据库中的clas表,表的结构如下:

对应的Clas文件如下:

package com.school.entity;

public class Clas {    

    // 课程id
    private int id;
    // 课程名称
    private String name;
    // 课程介绍
    private String comment;
    
    // 默认构造方法
    public Clas() {
    }
    
    // 包含全部属性的构造方法
    public Clas(int id, String name, String comment) {
        super();
        this.id = id;
        this.name = name;
        this.comment = comment;
    }
    
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getComment() {
        return comment;
    }
    public void setComment(String comment) {
        this.comment = comment;
    }

}

在applicationContext.xml中,各个bean之间存在引用关系:

clasDAO对应的类为com.school.dao.ClasDAOImpl,以sessionFactory作为参数;

clasService对应的类为com.school.service.ClasServiceImpl,以clasDAO作为参数;

clasQueryAction对应的类为com.school.action.ClasQueryAction,以clasService作为参数;

clasAction对应的类为com.school.action.ClasAction,以clasService作为参数。

原文地址:https://www.cnblogs.com/mstk/p/4539402.html