问题描述
我在apache-camel中运行MyBatis。
I am running MyBatis in apache-camel.
查询的结果包含一些jdbcType =DATE格式的数据。我编写了自定义ObjectTypeHandler,它处理SQL日期和XMLGregorianCalendar日期类型之间的转换,我用它来将数据设置为JAXB对象(从xsd方案生成)。
The result from the query contains some data in jdbcType="DATE" format. I wrote my custom ObjectTypeHandler which handles conversion between SQL date and XMLGregorianCalendar date type that I am using to set data into JAXB object (generate from xsd scheme).
问题是并非JAXB类中的所有字段都接受原始XMLGregorianCalendar值,其中一些以他们需要JAXBElement的方式定义。
The problem is that not all the fields in JAXB class accept raw XMLGregorianCalendar value, some of them defined in such manner that they need JAXBElement.
我可以使用ObjectFactory实例来解决这个问题,但是那么我需要为每个以JAXBElement作为输入的字段编写自定义ObjectTypeHandler(因为它们在ObjectFactory中有自己的create *方法)并且有很多。这听起来好像很多样板和重复的代码。
I can use ObjectFactory instance to solve this, but then I would need to write custom ObjectTypeHandler for every field that takes JAXBElement as input (as they have their own create* method in ObjectFactory) and there are many of them. It sounds like a lot of boilerplate and duplicated code.
是否有可能更优雅地处理它?
Is it possible to handle it more graciously?
推荐答案
我使用@Vadim推荐解决了这个问题:
I solved this problem using @Vadim recommendation:
只需定义 xjb
文件( generateElementProprety
设置为 false
以避免生成 JAXBElement
包装器)并在从 XSD
方案生成POJO期间将其提供给 XJC
编译器。
Just define xjb
file (with generateElementProprety
set to false
to avoid generation of JAXBElement
wrappers) and supply it to XJC
compiler during POJO generation from XSD
scheme.
例如:
bindings.xjb
bindings.xjb
<?xml version="1.0" encoding="UTF-8"?>
<jaxb:bindings version="2.1" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<jaxb:globalBindings generateElementProperty="false"/>
</jaxb:bindings>
通过命令启动XJC编译器:
Start XJC compiler via command:
xjc *.xsd -d ../../java -b bindings.xjb
此示例从方案文件生成POJO文件到 ../../ java
目录( *。xsd
)使用 bindings.xjb
文件中的其他设置位于工作目录中。
This example generate POJO files into ../../java
directory from scheme files (*.xsd
) located in working directory using additional settings from bindings.xjb
file.
谢谢,@ Vadim,指向正确的方向!
Thanks, @Vadim, for pointing into right direction!
这篇关于ObjectFactory和MyBatis,将结果映射到JAXBElement< XmlGregorianCalendar>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!