javaEE开发中遇到的常见配置解析,XML和注解

  • 1.名词解释
  • 1.1XML(能力有限,加上初学,复制粘贴时内容纷乱,重复,大家可以直接跳转到文末总结部分)

一种用于存储和传输数据的可扩展标记语言。就是用一系列标记来描述数据,然后在利用对应的XML解析器来解析数据。

网上找的简单示例:

<?xml version="1.0" encoding="UTF-8"?>

<note>

<to>Tove</to>

<from>Jani</from>

<heading>Reminder</heading>

<body>Don't forget me this weekend!</body>

</note>

使用树结构,且必须有一个根节点(root node),更多关于XML的学习资料可自行网上查找。

本文主要讲的是javaEE开发中用到的一些XML配置文件的使用

先来一个大家最常见到的web.xml

<?xml version="1.0" encoding="UTF-8"?> <!--每个xml文件必须有的,且独立放在文档的开头 -->
<web-app
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                      http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1"
         metadata-complete="true">

</web-app>

<web-app> <web-app>是整个文档的根节点

xmlns  XML Namespaces的缩写)是一个属性,是XML命名空间。作用是赋予命名空间一个唯一的名称,避免命名冲突(标记同名而意义不同产生的歧义)。 

可以在文档中定义一个或多个可供选择的命名空间。

该属性可以放置在文档内任何元素的开始标签中。该属性的值类似于 URL,它定义了一个命名空间,浏览器会将此命名空间用于该属性所在元素内的所有内容。

xsi  (xml schema instance)  具体的xml schema实例

xsi:schemaLocation    定义了XML Namespace和对应的XSD(Xml Schema Definition)文档的位置的关系。它的值由一个或多个URI引用对组成,两个URI之间以空白符分隔(空格和换行均可)。第一个URI是定义的XML Namespace的值,第二个URI给出Schema文档的位置,Schema处理器将从这个位置读取Schema文档,该文档的targetNamespace必须与第一个URI相匹配

以上是使用XSD文档限定

还可以使用DTD文档限定,文档类型定义(Document Type Definition)是一套为了进行程序间的数据交换而建立的关于标记符的语法规则。是HTML和XML规格的一部分,可通过比较文档和文档类型定义文件来检查文档是否符合规范,元素和标签使用是否正确。

例如strus.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
        "http://struts.apache.org/dtds/struts-2.5.dtd">

<struts>

</struts>

<!DOCTYPE 文档类型

struts 文档的根节点

PUBLIC  公共标识,唯一性,类似与xsd中的namespace的作用(只是一个比喻)

 

  • 1.2注解

元数据,即一种描述数据的数据。所以,可以说注解就是源代码的元数据。Annotation是一种应用于类、方法、参数、变量、构造器及包声明中的特殊修饰符。

为什么要引入注解?

在使用Annotation之前,XML被广泛的应用于描述元数据,XML配置是为了分离代码和配置而引入的。

但一些工程师在工作中又希望使用一些和代码紧耦合的东西,而不是像XML那样和代码是松耦合的(在某些情况下甚至是完全分离的)代码描述。

两者看起来似乎背道而驰,不过在不同的应用场景,二者各有利弊。目前,许多框架将XML和Annotation两种方式结合使用,相互补充不足。

假如你想为应用设置很多的常量或参数,这种情况下,XML是一个很好的选择,因为它不会与特定的代码相连。如果你想把某个方法声明为服务,那么使用Annotation会更好一些,因为这种情况下需要注解和方法紧密耦合起来。

  • 2.XML

SSH框架常用到的XML配置

struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
        "http://struts.apache.org/dtds/struts-2.5.dtd">

<struts>
    <!--告知struts2运行时,使用spring创建对象-->
    <constant name="struts.objectFactory" value="org.apache.struts2.spring.StrutsSpringObjectFactory"/>
    <!--第一步,先定义一个包-->
    <package name="mypack001" extends="struts-default">
    <!--第二步,定义一个action,配置跳转信息,此处action的class不是全限定名,对应于spring中的配置,路径为/WEB-INF/jsp/index.jsp是为了防止jsp不经过action直接访问-->
    <action name="Index" class="myIndexAction">
        <result name="success">/WEB-INF/jsp/index.jsp</result>
    </action>
    </package>
</struts>

applicationContext.xml

配置spring容器,并整合hibernate配置文件(最后一句黑体部分)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--所有需要类的实例都由spring管理-->
    <bean id="myIndexAction" class="main.ssh.action.IndexAction" scope="prototype">
        <property name="is" ref="myIndexService"/>
    </bean>

    <bean id="myIndexService" class="main.ssh.service.IndexServiceImpl" scope="prototype"><property name="id" ref="myIndexDao"/>
    </bean>

    <bean id="myIndexDao" class="main.ssh.dao.IndexDaoImpl" scope="prototype">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>

    <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean" scope="prototype">
        <property name="configLocation" value="classpath:hibernate.cfg.xml"/>
    </bean>
</beans>

hibernate.cfg.xml

hibernate主要配置文件

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD//EN"
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
  <session-factory>
    <!--连接数据库的驱动-->
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <!--连接数据库用户名-->
      <property name="hibernate.connection.username">root</property>
      <!--连接数据库的密码-->
      <property name="hibernate.connection.password">12345677</property>
      <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/sshhello</property>
    <!--操作数据库时,会向控制台打印sql-->
    <property name="show_sql">true</property>
    <!--打印语句前会先格式化sql语句-->
    <property name="format_sql">true</property>
    <!--hbm2dd1.auto生成表结构的策略配置,update(如果当前数据库不存在表结构会自动创建,如果存在表结构但与实体不一致,会修改表结构,保留原有列)-->
    <property name="hbm2ddl.auto">update</property>
    <!--数据库方言配置,选择最短的-->
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    <!--事务自动提交-->
    <property name="hibernate.connection.autocommit">true</property>
    <!--将Session与线程绑定,只有进行了该配置才能使用getCurrentSession-->
    <property name="hibernate.current_session_context_class">thread</property>
      <mapping resource="main/ssh/entity/BookCard.hbm.xml"/>
      <!--引入ORM映射文件-->
      <mapping class="main.ssh.entity.BookCard"/>
  </session-factory>
</hibernate-configuration>

实体类映射配置文件  实体类名.hbm.xml

本例BookCard.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>

    <class name="main.ssh.entity.BookCard" table="book_card" schema="sshhello">
        <id name="cid" column="cid">
            <generator class="native"/>
        </id>
        <property name="name" column="name"/>
        <property name="sex" column="sex"/>
        <property name="cardDate" column="cardDate"/>
        <property name="deposit" column="deposit"/>
    </class>
</hibernate-mapping>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                      http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1"
         metadata-complete="true">

  <display-name>Welcome to Tomcat</display-name>
  <welcome-file-list>
      <welcome-file>index.action</welcome-file>
  </welcome-file-list>
    <!--struts2过滤器-->
<filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <!--spring监听器-->
    <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>
</web-app>
 

SSM框架配置

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app 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"
         version="2.5">
    <display-name>SSM</display-name>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    <!-- spring监听器 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!-- 指定spring核心配置文件 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring-mvc.xml</param-value>
    </context-param>
    <!-- 配置过滤器处理POST提交乱码问题 -->
    <filter>
        <filter-name>encoding</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>encoding</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <!-- 配置前端控制器 -->
    <!-- 配置DispatcherServlet -->
    <servlet>
        <servlet-name>springDispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- 配置Spring mvc下的配置文件的位置和名称,如果不设置,默认找/WEB-INF/<servlet-name>-servlet.xml -->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring/springmvc-config.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springDispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app> 

springMVC配置

spring-mvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
      http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd">
    <!--spring和springMVC的配置合并-->
    <!-- 配置扫描注解 @Controller @Service -->
    <context:component-scan base-package="com."/>
    <!-- SpringMVC使用<mvc:annotation-driven>自动加载RequestMappingHandlerMapping和RequestMappingHandlerAdapter -->
    <mvc:annotation-driven/>
    <!-- 配置静态资源映射 -->
    <!--
 <mvc:resources location="/js/" mapping="/js/**"/>
 <mvc:resources location="/css/" mapping="/css/**"/>
 <mvc:resources location="/imgs/" mapping="/imgs/**"/>
 <mvc:resources location="/font/" mapping="/font/**"/>
 -->
    <!-- 配置视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- 配置逻辑视图的前缀 -->
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <!-- 配置逻辑视图的后缀 -->
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

<?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"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
                        http://www.springframework.org/schema/context
                        http://www.springframework.org/schema/context/spring-context-3.1.xsd
                        http://www.springframework.org/schema/mvc
                        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
    <!-- 自动扫描 -->
    <context:component-scan base-package="cn.code2038" />
    <!-- 引入配置文件 -->
    <bean id="propertyConfigurer"
          class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="classpath:jdbc.properties" />
    </bean>

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
          destroy-method="close">
        <property name="driverClassName" value="${driver}" />
        <property name="url" value="${url}" />
        <property name="username" value="${username}" />
        <property name="password" value="${password}" />
        <!-- 初始化连接大小 -->
        <property name="initialSize" value="${initialSize}"></property>
        <!-- 连接池最大数量 -->
        <property name="maxActive" value="${maxActive}"></property>
        <!-- 连接池最大空闲 -->
        <property name="maxIdle" value="${maxIdle}"></property>
        <!-- 连接池最小空闲 -->
        <property name="minIdle" value="${minIdle}"></property>
        <!-- 获取连接最大等待时间 -->
        <property name="maxWait" value="${maxWait}"></property>
    </bean>

    <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <!-- 自动扫描mapping.xml文件 -->
        <property name="mapperLocations" value="classpath:cn/code2038/mapping/*.xml"/>
    </bean>

    <!-- DAO接口所在包名,Spring会自动查找其下的类 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="cn.code2038.dao" />
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
    </bean>

    <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
    <bean id="transactionManager"
          class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>

</beans>
mybatis sql语句映射配置

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <!--给实体类取别名-->
    <typeAliases>

    </typeAliases>
</configuration>

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="space.DAO.UserDao">

    <select id="getUserById" resultType="space.xxhui.POJO.User">
        SELECT * FROM USER WHERE id = #{id};
    </select>

    <!--auto generated Code-->
    <resultMap id="AllColumnMap" type="space.xxhui.POJO.User">
        <result column="name" property="name"/>
        <result column="birth" property="birth"/>
        <result column="sex" property="sex"/>
        <result column="age" property="age"/>
        <result column="phone" property="phone"/>
        <result column="email" property="email"/>
        <result column="pwd" property="pwd"/>
    </resultMap>

    <!--auto generated Code-->
    <sql id="all_column">
        `name`,
        `birth`,
        `sex`,
        `age`,
        `phone`,
        `email`,
        `pwd`
    </sql>

    <!--auto generated Code-->
    <insert id="insert" useGeneratedKeys="true" keyProperty="pojo.name">
        INSERT INTO user (
            `name`,
            `birth`,
            `sex`,
            `age`,
            `phone`,
            `email`,
            `pwd`
        ) VALUES (
            #{pojo.name},
            #{pojo.birth},
            #{pojo.sex},
            #{pojo.age},
            #{pojo.phone},
            #{pojo.email},
            #{pojo.pwd}
        )
    </insert>

    <!--auto generated Code-->
    <insert id="insertSelective" useGeneratedKeys="true" keyProperty="pojo.name">
        INSERT INTO user
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="pojo.name!=null"> `name`,</if>
            <if test="pojo.birth!=null"> `birth`,</if>
            <if test="pojo.sex!=null"> `sex`,</if>
            <if test="pojo.age!=null"> `age`,</if>
            <if test="pojo.phone!=null"> `phone`,</if>
            <if test="pojo.email!=null"> `email`,</if>
            <if test="pojo.pwd!=null"> `pwd`,</if>
        </trim>
        VALUES
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="pojo.name!=null">#{pojo.name},</if>
            <if test="pojo.birth!=null">#{pojo.birth},</if>
            <if test="pojo.sex!=null">#{pojo.sex},</if>
            <if test="pojo.age!=null">#{pojo.age},</if>
            <if test="pojo.phone!=null">#{pojo.phone},</if>
            <if test="pojo.email!=null">#{pojo.email},</if>
            <if test="pojo.pwd!=null">#{pojo.pwd},</if>
        </trim>
    </insert>

    <!--auto generated Code-->
    <insert id="insertList">
        INSERT INTO user (
        <include refid="all_column"/>
        )VALUES
        <foreach collection="pojos" item="pojo" index="index" separator=",">
            (
            #{pojo.name},
            #{pojo.birth},
            #{pojo.sex},
            #{pojo.age},
            #{pojo.phone},
            #{pojo.email},
            #{pojo.pwd}
            )
        </foreach>
    </insert>

    <!--auto generated Code-->
    <update id="update">
        UPDATE user
        <set>
            <if test="pojo.name != null"> `name` = #{pojo.name}, </if>
            <if test="pojo.birth != null"> `birth` = #{pojo.birth}, </if>
            <if test="pojo.sex != null"> `sex` = #{pojo.sex}, </if>
            <if test="pojo.age != null"> `age` = #{pojo.age}, </if>
            <if test="pojo.phone != null"> `phone` = #{pojo.phone}, </if>
            <if test="pojo.email != null"> `email` = #{pojo.email}, </if>
            <if test="pojo.pwd != null"> `pwd` = #{pojo.pwd} </if>
        </set>
        WHERE name = #{pojo.name}
    </update>
</mapper>

jdbc.properties

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/ssmhello?useSSL="false"
username=root
password=code2038@1970
#定义初始连接数
initialSize=0
#定义最大连接数
maxActive=20
#定义最大空闲
maxIdle=20
#定义最小空闲
minIdle=1
#定义最长等待时间
maxWait=60000
log4j.properties

#定义LOG输出级别
log4j.rootLogger=INFO,Console,File
#定义日志输出目的地为控制台
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.Target=System.out
#可以灵活地指定日志输出格式,下面一行是指定具体的格式
log4j.appender.Console.layout = org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=[%c] - %m%n

#文件大小到达指定尺寸的时候产生一个新的文件
log4j.appender.File = org.apache.log4j.RollingFileAppender
#指定输出目录
log4j.appender.File.File = logs/ssm.log
#定义文件最大大小
log4j.appender.File.MaxFileSize = 10MB
# 输出所以日志,如果换成DEBUG表示输出DEBUG以上级别日志
log4j.appender.File.Threshold = ALL
log4j.appender.File.layout = org.apache.log4j.PatternLayout
log4j.appender.File.layout.ConversionPattern =[%p] [%d{yyyy-MM-dd HH\:mm\:ss}][%c]%m%n
  依赖配置文件pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>springboot.example</groupId>
    <artifactId>spring-boot-hello</artifactId>
    <version>1.0-SNAPSHOT</version>

    <packaging>jar</packaging>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.2.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>1.3.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>4.1.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

 

总结:

如果你能看到这里,我都佩服

实在是太乱了,由于初学,脑子里一团麻,没有整体结构。

不过学习就在于折腾,爬过一个又一个坑,会发现柳暗花明又一村。

查找资料学习时候,由于每个人配置时命名不同,有的人将两个甚至更多配置弄到了一个xml中。

这就会造成好多配置缺失,或重复,结构之混乱不可言说。

在某个瞬间,脑子灵光一闪,xml的名字并不是那么重要的,关键在于标记(毕竟是标记语言)

但标记也不是随便用的,关键还是在于DTD和XSD

这样就清晰了

注意以下只是为了分析的简化表示,具体使用需完整描述

配置web.xml,用到web-app根节点

<?xml version="1.0" encoding="UTF-8"?>
<web-app web-app_2_5.xsd>         <web-app>

配置用到struts根节点

<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
        "http://struts.apache.org/dtds/struts-2.5.dtd">

<struts>         </struts>

用到spring-bean,spring框架的配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<bean></bean>

</beans>

hibernate的配置,需要用到hibernate-configuratin根节点

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

hibernate映射配置,用到<hibernate-mapping>节点

<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>  <hibernate-mapping>

需要在spring-bean中用到 <context:    /> 和<mvc:   />

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
      http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd">

mybatis的<configuration>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
</configuration>

mybatis的映射配置<mapper>配置
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="space.DAO.UserDao">

    <select id="getUserById" resultType="space.xxhui.POJO.User">
        SELECT * FROM USER WHERE id = #{id};
    </select>

</mapper>

基础不牢,地动山摇。

深刻理解的自己的不足,继续补基础去。

正所谓,不识庐山真面目,只因俺在此山中。

 

文中配置多为借鉴各博文学习SSH,SSM框架时,复制别人的,多有重复,请见谅。大家可以采用文末的方法,DTD,XSD加以甄别剔除。

关于注解在开发框架中的应用以及XML配置中具体标签和属性的使用方法,本人理解还不够,继续学习,后续在更吧.........................................................

本人能力有限,有什么错误,希望大家不吝指教

欢迎留言补充交流

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值