- Spring OXM 教程
- Spring OXM - 主页
- Spring OXM - 概述
- Spring OXM - 环境设置
- 弹簧 OXM 和 JAXB
- Spring OXM - 创建项目
- Spring OXM - 更新项目 JAXB2
- Spring OXM - 测试 JAXB2
- Spring OXM 和 XStream
- Spring OXM - 更新项目
- Spring OXM - 测试 XStream
- 弹簧 OXM 和脚轮
- Spring OXM - 更新项目
- Spring OXM - 测试脚轮
- Spring OXM 有用资源
- Spring OXM - 快速指南
- Spring OXM - 有用的资源
- Spring OXM - 讨论
Spring OXM - 更新项目 Castor
更新 pom.xml 的内容以具有 Castor 依赖项,如下所示 -
pom.xml
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.tutorialspoint</groupId> <artifactId>springoxm</artifactId> <version>0.0.1-SNAPSHOT</version> <name>Spring OXM</name> <description>Spring OXM Project</description> <properties> <org.springframework.version>4.3.7.RELEASE</org.springframework.version> <org.hibernate.version>5.2.9.Final</org.hibernate.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${org.springframework.version}</version> <scope>compile</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-oxm</artifactId> <version>${org.springframework.version}</version> <scope>compile</scope> </dependency> <dependency> <groupId>org.codehaus.castor</groupId> <artifactId>castor-core</artifactId> <version>1.4.1</version> </dependency> <dependency> <groupId>org.codehaus.castor</groupId> <artifactId>castor-xml</artifactId> <version>1.4.1</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>${java.version}</source> <target>${java.version}</target> </configuration> </plugin> </plugins> </build> </project>
添加castor映射所需的映射xml,将Student类映射为src→main→resources文件夹下的mappings.xml,如下所示。
映射.xml
<?xml version="1.0"?> <!DOCTYPE mapping PUBLIC "-//EXOLAB/Castor Mapping DTD Version 1.0//EN" "http://castor.org/mapping.dtd"> <mapping> <class name="com.tutorialspoint.oxm.model.Student" auto-complete="true" > <map-to xml="Student"/> <field name="id" type="integer"> <bind-xml name="id" node="attribute"></bind-xml> </field> <field name="name"> <bind-xml name="name"></bind-xml> </field> <field name="age"> <bind-xml name="age" type="int"></bind-xml> </field> </class> </mapping>
使用以下内容更新src → main → resources中的 applicationcontext.xml以使用 CastorMarshaller。CastorMarshaller 对象可用于编组和解组。
应用程序上下文.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:oxm="http://www.springframework.org/schema/oxm" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.0.xsd"> <bean id="castorMarshaller" class="org.springframework.oxm.castor.CastorMarshaller"> <property name="targetClass" value="com.tutorialspoint.oxm.model.Student"></property> <property name="mappingLocation" value="mappings.xml"></property> </bean> </beans>