使用Maven插件jaxb2-maven-plugin,xml生成JavaBean

发布时间 2023-04-25 09:56:38作者: 小胡桐

1、使用 trang.jar生成xsd文件

java -jar trang.jar test.xml  test.xsd

tarng.jar下载链接:https://note.youdao.com/s/7qYsVXtQ

2、使用maven插件生成实体

pom.xml文件中添加以下配置即可:

<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>jaxb2-maven-plugin</artifactId>
            <version>2.5.0</version>
            <executions>
                <execution>
                    <id>xjc</id>
                    <goals>
                        <goal>xjc</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <packageName>com.example.package.name</packageName>
                <schemaDirectory>${project.basedir}/src/main/resources/schema</schemaDirectory>
                <outputDirectory>${project.build.directory}/generated-sources/xjc</outputDirectory>
            </configuration>
        </plugin>
    </plugins>
</build>

 

其中,packageName为生成的Java类的包名,schemaDirectory为XSD文件存放的目录,outputDirectory为生成Java类的输出目录。执行   mvn jaxb2:xjc   即可在指定的输出目录下生成对应的Java类。