zoukankan      html  css  js  c++  java
  • 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作为参数。

  • 相关阅读:
    [51nod] 1088 最长回文子串 #Hash+二分
    [51nod] 1378 夹克老爷的愤怒 #树形DP
    [BZOJ] 2456: mode #众数计数法
    [51nod] 1199 Money out of Thin Air #线段树+DFS序
    [51nod] 1494 选举拉票 #算法设计策略
    [51nod] 1463 找朋友 #离线+扫描线
    [BZOJ] 2733: [HNOI2012]永无乡 #线段树合并+并查集
    [BZOJ] 1012: [JSOI2008]最大数maxnumber
    [Codeforces] E. Lomsat gelral #DSU on Tree
    [BZOJ] 4756: [Usaco2017 Jan]Promotion Counting #线段树合并+权值线段树
  • 原文地址:https://www.cnblogs.com/mstk/p/4539402.html
Copyright © 2011-2022 走看看