如何使用jaxb2 maven插件生成@TABLE标签

时间:2013-09-10 07:59:54

标签: jaxb maven-jaxb2-plugin jaxb2-basics

您好我已经按照一些教程编写了一个模式,如下所示:

<xs:element name="User" type="kmcs:User"/>
    <xs:element name="UserList" type="kmcs:UserList"/>

    <xs:complexType name="User">
        <xs:sequence>
                <xs:element name="id" type="xs:long" minOccurs="0" />
                <xs:element name="name" type="xs:string" />
                <xs:element name="registrationDate" type="xs:dateTime" />
            </xs:sequence>
            <xs:attribute name = ""/>

    </xs:complexType>

    <xs:complexType name="UserList">
        <xs:complexContent>
            <xs:extension base="kmcs:User">
                <xs:sequence>
                   <xs:element name="user"  minOccurs="0"
                maxOccurs="unbounded" />
                </xs:sequence>
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>


</xs:schema>

它生成以下内容:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "User", propOrder = {
    "id",
    "name",
    "registrationDate"
})
@XmlSeeAlso({
    UserList.class
})
@Entity
@XmlRootElement(name = "{@id}")
@Table(schema = "schemaname", uniqueConstraints = {

}, name = "BillingAccountNumberDetailsInfo")
public class User
    implements Serializable, ToString
{

我想要这样的东西:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "User", propOrder = {
    "id",
    "name",
    "registrationDate"
})
@XmlSeeAlso({
    UserList.class
})
@Entity
@XmlRootElement(name ="user")
@Table(schema = "schema_name", uniqueConstraints = {

}, name = "User")
public class User
    implements Serializable, ToString
{

所以我想要的基本上是表格,它选择我提供的任何硬编码,但我希望它显示类名或类对象名称。

1 个答案:

答案 0 :(得分:0)

使用此配置。

    <bindings version="2.0" xmlns="http://java.sun.com/xml/ns/jaxb"
        xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
        xmlns:annox="http://annox.dev.java.net"
        xmlns:namespace="http://jaxb2-commons.dev.java.net/namespace-prefix">
        <bindings schemaLocation="../yourSchema.xsd">

            <bindings node="//xs:complexType[@name='User']">
                <annox:annotate>
                    <annox:annotate annox:class="javax.persistence.Entity">
                    </annox:annotate>
                    <annox:annotate annox:class="javax.persistence.Table" schema = "schema_name" name = "User">
                    </annox:annotate>
                    <annox:annotate annox:class="javax.xml.bind.annotation.XmlRootElement"
                    name="User">
                    </annox:annotate>
                </annox:annotate>
            </bindings>

        </bindings>
    </bindings>

maven插件配置

            <plugin>
                <groupId>org.jvnet.jaxb2.maven2</groupId>
                <artifactId>maven-jaxb2-plugin</artifactId>
                <version>0.8.1</version>
                <executions>
                    <execution>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <args>
                        <arg>-Xannotate</arg>
                        <arg>-nv</arg>
                        <arg>-Xnamespace-prefix</arg>
                    </args>
                    <extension>true</extension>
                    <schemaDirectory>src/main/resources/schema</schemaDirectory>
                    <schemaIncludes>
                        <schemaInclude>yourSchema.xsd</schemaInclude>
                    </schemaIncludes>
                    <bindingDirectory>src/main/resources/schema/xjb</bindingDirectory>
                    <bindingIncludes>
                        <include>*.xjb</include>
                    </bindingIncludes>
                    <debug>true</debug>
                    <verbose>true</verbose>
                    <episode>true</episode>
                    <forceRegenerate>true</forceRegenerate>
                    <plugins>
                        <plugin>
                            <groupId>org.jvnet.jaxb2_commons</groupId>
                            <artifactId>jaxb2-basics</artifactId>
                            <version>0.6.0</version>
                        </plugin>
                        <plugin>
                            <groupId>org.jvnet.jaxb2_commons</groupId>
                            <artifactId>jaxb2-basics-annotate</artifactId>
                            <version>0.6.0</version>
                        </plugin>
                        <plugin>
                            <groupId>org.jvnet.jaxb2_commons</groupId>
                            <artifactId>jaxb2-namespace-prefix</artifactId>
                            <version>1.1</version>
                        </plugin>
                    </plugins>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.hibernate.javax.persistence</groupId>
                        <artifactId>hibernate-jpa-2.0-api</artifactId>
                        <version>1.0.1.Final</version>
                    </dependency>
                </dependencies>
            </plugin>