1.约束的概念:
规定xml文档的书写规则
作为框架的使用者(程序员):
1.能够在xml文件中引入约束文档
2.能够简单的读懂约束文档
分类:
1.DTD:一中简单的约束技术
2.Schema:一种复杂的约束技术
2.DTD约束:
引入dtd文档到xml文档中
内部dtd:将约束规则定义在xml文档中
外部dtd:将约束的规则定义在外部的dtd文件中
本地:<! DOCTYPE 根标签名 SYSTEM "dtd文件的位置">
网络:<! DOCTYPE 根标签名 PUBLIC "dtd文件名字" "dtd文件的位置URL">
导入dtd的方式:
内部导入
<!DOCTYPE note [
<!ELEMENT note (to,from,heading,body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
]>
外部导入
本地文件系统:
<!DOCTYPE note SYSTEM "note.dtd">
公共的外部导入:
<!DOCTYPE 根元素 PUBLIC "http://www.abyg.cn/abyg.dtd">
3.DTD语法:
约束标签:<! ELEMENT 元素名称 类别> 或 <! ELEMENT 元素名称 (元素名称)>
类别:
空标签:EMPTY。表示元素一定是空元素
普通字符串:(#PCDATA)。表示元素的内容一定是普通字符串(不能含有子标签)。
任何内容:ANY。表示元素的内容可以是任意内容(包括子标签)
元素内容:
顺序问题:<! ELEMENT 元素名称(子元素名称 1,子元素名称 2, .....)>:按顺序出现子标签
次数问题:
标签 : 必须且出现1次。
标签+ : 至少出现1次
标签* : 0或n次
标签? : 0 或 1次
约束属性:<! ATTLIST 元素名称 属性名称 属性类型 默认值>
默认值:
#REQUIRED 属性是必须的
#IMPLIED 属性不是必须的
#FIXED value 属性不是必须的,但属性值是固定的
属性类型:控制属性值的
CDATA:表示字符串
(en1|en2|....):表示一定是任选其中的一个值
ID:表示在一个xml文档中该属性值必须唯一。值不能以数字开头
4.Schema约束:
引入:
- 填写xml文档的根元素
- 引入xsi前缀:xmlns:xsi="http://www.w3.org/2001/xmlSchema-instance"
- 引入xsd文件命名空间:xsi:schemaLocation="http://www.company.com student.xsd"
- 为每一个xsd约束声明一个前缀,作为标识:xmlns="http://www.company.com"
<students xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http:/www.company.com"
xsi:schemaLocation="http://www.company.com student.xsd">
名称空间:告诉xml文档的哪个元素被哪个schema文档约束
在一个xml文档中,不同的标签可以受到不同的schema文档的约束。
5.案例:
1.dtd:
note.dtd
<!ELEMENT note (to,from,heading,body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
note.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- <!DOCTYPE note SYSTEM "note.dtd"> -->
<!DOCTYPE note [
<!ELEMENT note (to+,from*,heading?,body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from EMPTY>
<!ELEMENT heading ANY>
<!ELEMENT body (#PCDATA)>
<!ATTLIST to name (a|b) #REQUIRED>
]>
<note>
<to name="a"></to>
<from></from>
<heading></heading>
<body></body>
</note>
2.Schema:
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
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-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<!-- 配置 读取properties文件 jdbc.properties -->
<context:property-placeholder location="classpath:jdbc.properties" />
<!-- 配置 数据源 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<!-- 配置SqlSessionFactory -->
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 设置MyBatis核心配置文件 -->
<property name="configLocation" value="classpath:sqlMapConfig.xml" />
<!-- 设置数据源 -->
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 配置Mapper扫描 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 设置Mapper扫描包 -->
<property name="basePackage" value="cn.xclink.mapper" />
</bean>
<!-- 事务管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 数据源 -->
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- 传播行为 -->
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="create*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="find*" propagation="SUPPORTS" read-only="true" />
<tx:method name="select*" propagation="SUPPORTS" read-only="true" />
<tx:method name="get*" propagation="SUPPORTS" read-only="true" />
<tx:method name="query*" propagation="SUPPORTS" read-only="true" />
</tx:attributes>
</tx:advice>
<!-- 切面 -->
<aop:config>
<aop:advisor advice-ref="txAdvice"
pointcut="execution(* cn.xclink.service.*.*(..))" />
</aop:config>
<!-- 开启注解配置事务 -->
<!-- <tx:annotation-driven transaction-manager="transactionManager" /> -->
</beans>
student.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!--
1.填写xml文档的根元素
2.引入xsi前缀. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3.引入xsd文件命名空间. xsi:schemaLocation="http://www.company.com student.xsd"
4.为每一个xsd约束声明一个前缀,作为标识 xmlns="http://www.company.com"
-->
<students xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.company.com"
xsi:schemaLocation="http://www.company.com student.xsd"
>
<student id="1001">
<name>tom</name>
<age>18</age>
<sex>male</sex>
</student>
</students>
student.xsd
<?xml version="1.0"?>
<xsd:schema xmlns="http://www.company.com"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.company.com" elementFormDefault="qualified">
<xsd:element name="students" type="studentsType"/>
<xsd:complexType name="studentsType">
<xsd:sequence>
<xsd:element name="student" type="studentType" minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="studentType">
<xsd:sequence>
<xsd:element name="name" type="xsd:string"/>
<xsd:element name="age" type="ageType" />
<xsd:element name="sex" type="sexType" />
</xsd:sequence>
<xsd:attribute name="id" type="numberType" use="required"/>
</xsd:complexType>
<xsd:simpleType name="sexType">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="male"/>
<xsd:enumeration value="female"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="ageType">
<xsd:restriction base="xsd:integer">
<xsd:minInclusive value="0"/>
<xsd:maxInclusive value="256"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="numberType">
<xsd:restriction base="xsd:string">
<xsd:pattern value="\d{4}"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:schema>