简体   繁体   English

是否可以直接从MyBATIS获取XMLGregorianCalendar?

[英]Is it possible to get XMLGregorianCalendar directly from MyBATIS?

I work with public StackOverflow dump and have everything stored in MySQL. 我使用公共StackOverflow转储,并将所有内容存储在MySQL中。 Now, I want to perform some analyses using Java and MyBATIS. 现在,我想使用Java和MyBATIS进行一些分析。 For some of these I need to extract timeseries for a user, or a particular question etc. So for this, lets say posts and their timestamps, I have a MyBatis query: 对于其中的一些,我需要为用户或特定问题等提取时间序列。为此,让我们说帖子及其时间戳,我有一个MyBatis查询:

 <select id="GetAllPostsTimeStamps" resultType="java.util.Date">
     SELECT creationDate from posts
 </select>

which hands the timestamp data to MyBatis ResultHandler, where I do this transformation: 它将时间戳数据传递给MyBatis ResultHandler,在这里我进行此转换:

@Override
public void handleResult(ResultContext context) {
   TimeZone tz = TimeZone.getTimeZone("UTC");
   TimeZone.setDefault(tz);
   Date tstamp = (Date) context.getResultObject();
   XMLGregorianCalendar xmlTstam = Tstamp.makeTimestamp(tstamp.getTime());
   ...etc...
}

I really need to use XMLGregorianCalendar because all my downstream logic relies on this type. 我真的需要使用XMLGregorianCalendar因为我所有的下游逻辑都依赖于这种类型。

Is it possible to tell MyBatis with its handlers feature to make transformation of SQL datetime to XMLGregorianCalendar internally (ie I will code the handler only once), so I will get proper timestamps out of it everytime I need? 是否可以告诉MyBatis使用其处理程序功能在内部将SQL日期时间转换为XMLGregorianCalendar(即,我将只对处理程序进行一次编码),以便每次需要时都可以从中获得适当的时间戳?

Editing to add more clarity. 编辑以增加清晰度。

So, when I save java objects with XMLGregorianCalendar fields into DB (MySQL datetime type) I use an implementation of MyBatis handler: 因此,当我将带有XMLGregorianCalendar字段的Java对象保存到DB(MySQL datetime类型)中时,我使用MyBatis处理程序的实现:

XMLGregorianCalendarDateTypeHandler implements TypeHandler<XMLGregorianCalendar>
...

which I specify in the INSERT query like this: 我在INSERT查询中指定的INSERT如下:

    ..., #{creationDate,
           javaType=javax.xml.datatype.XMLGregorianCalendar,
           jdbcType=TIMESTAMP,                
           typeHandler=...XMLGregorianCalendarTypeHandler},...

is there a way to specify the backward transform handler for SELECT type queries? 有没有一种方法可以为SELECT类型查询指定反向转换处理程序?

I found why it wasnt working, maybe it will help somebody: 我发现了为什么它不起作用,也许会帮助某人:

So, when I configure a handler specifying both types: JDBC and Java, this doesnt work: 因此,当我配置同时指定两种类型的处理程序:JDBC和Java时,这不起作用:

<typeHandler javaType="javax.xml.datatype.XMLGregorianCalendar" 
      jdbcType="TIMESTAMP"
      handler="my.package.XMLGregorianCalendarTimestampTypeHandler" />

But if I specify just Java type it works. 但是,如果我仅指定Java类型,它就可以工作。 Magic: 魔法:

<typeHandler javaType="javax.xml.datatype.XMLGregorianCalendar" 
      handler="my.package.XMLGregorianCalendarTimestampTypeHandler" />

Not sure if it is a bug or a feature. 不确定是错误还是功能。

I don't know MyBatis, but will this help? 我不认识MyBatis,但这有帮助吗?

DatatypeFactory factory = DatatypeFactory.newInstance();
XMLGregorianCalendar calendar = factory.newXMLGregorianCalendar();
calendar.setMillisecond(tstamp.getTime());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM