SSH实例(2)

在WebContent\WEB-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作为参数。

时间: 2025-01-14 06:45:14

SSH实例(2)的相关文章

python之socket-ssh实例

本文转载自大王http://www.cnblogs.com/alex3714/articles/5830365.html 加有自己的注释,应该会比原文更突出重点些 一. 基本Socket实例 前面讲了这么多,到底咋么用呢? 1 import socket 2 3 server = socket.socket() #获得socket实例 4 5 server.bind(("localhost",9998)) #绑定ip port 6 server.listen() #开始监听 7 pri

SSH实例(3)

src文件夹的结构如下: clasDao文件: package com.school.service; import java.util.List; import com.school.dao.ClasDAO; import com.school.entity.Clas; public class ClasServiceImpl implements ClasService{ private ClasDAO clasDAO; public void setClasDAO(ClasDAO Clas

SSH实例(7)

运行结果. 浏览课程: 添加课程: 还有删除课程,这里就不演示了.

SSH实例(5)

在src中新建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"> <

SSH实例(4)

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"> <

SSH实例(6)

在WebContent文件夹下新建query.jsp和save.jsp文件. query.jsp: <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE HTML PUBLIC "

SSH实例(1)

首先,配置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"> <st

Python 之 SSH实例(可接受大字节返回)

服务器端代码 #Author Kang import os import subprocess import socket server = socket.socket() server.bind(('localhost',9999)) server.listen() print("等待客户端连接:>>>>") while True: conn,addr = server.accept() print("接受了一个新的链接!!") while

SSH搭建完美CURD,含分页算法

今日开始研究使用java平台上的框架解决web服务端的开发. 这是一个完整的SSH实例,在马士兵老师的SSH整合代码基础上,增加用户的增删改查,同时实现structs方式的分页 放出源代码供大家学习参考.关键是网上的学习案例真少 http://pan.baidu.com/s/1ntHswHF