使用Mybatis将Java实体类映射到Oracle数据库,实体类中属性createdate
为javax.xml.datatype.XMLGregorianCalendar
类型。项目启动是对Mapper文件解析时报错,提示如下:
17:23:04.445 [main] [] ERROR c.s.c.u.m.SqlSessionFactoryBean - Failed to parse mapping resource: 'file [C:\workspace\SD_IERP\target\classes\com\sinoprof\sscm\importShipAdviceInfoRouteSrv\dao\oracle\ImportShipAdviceInfoRouteSrvDaoMapper.xml]'
17:23:06.679 [main] [] ERROR c.s.c.utils.myBatis.XMLMapperBuilder - Error parsing Mapper XML. Cause: java.lang.IllegalStateException: Type handler was null on parameter mapping for property 'createdate'. It was either not specified and/or could not be found for the javaType / jdbcType combination specified.
默认的类型处理器无法实现对javax.xml.datatype.XMLGregorianCalendar
类型到java.sql.Timestamp
或者java.sql.Date
直接的映射。其中javax.xml.datatype.XMLGregorianCalendar
类型的传参格式为2013-05-31T14:58:31.609+08:00
。
自定义TypeHandler的方式如下:
1.代码
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
import java.sql.*;
import java.util.Date;
import java.util.GregorianCalendar;
@MappedJdbcTypes(JdbcType.TIMESTAMP)
@MappedTypes(Timestamp.class)
public class GregorianCalendarToDateTypeHandler extends BaseTypeHandler<XMLGregorianCalendar> {
@Override
public void setNonNullParameter(PreparedStatement preparedStatement, int i, XMLGregorianCalendar xmlGregorianCalendar, JdbcType jdbcType) throws SQLException {
long time = xmlGregorianCalendar.toGregorianCalendar().getTime().getTime();
java.sql.Timestamp sqlDate = new java.sql.Timestamp(time);
preparedStatement.setTimestamp(i, sqlDate);
}
@Override
public XMLGregorianCalendar getNullableResult(ResultSet resultSet, String s) throws SQLException {
java.sql.Timestamp sqlDate = resultSet.getTimestamp(s);
if (sqlDate != null) {
return date2XMLGregorianCalendar(sqlDate);
}
return null;
}
@Override
public XMLGregorianCalendar getNullableResult(ResultSet resultSet, int i) throws SQLException {
java.sql.Timestamp sqlDate = resultSet.getTimestamp(i);
if (sqlDate != null) {
return date2XMLGregorianCalendar(sqlDate);
}
return null;
}
@Override
public XMLGregorianCalendar getNullableResult(CallableStatement callableStatement, int i) throws SQLException {
java.sql.Timestamp sqlDate = callableStatement.getTimestamp(i);
if (sqlDate != null) {
return date2XMLGregorianCalendar(sqlDate);
}
return null;
}
/**
* java.sql.Timestamp格式转换为javax.xml.datatype.XMLGregorianCalendar格式
* @param sqlDate
* @return
*/
private XMLGregorianCalendar date2XMLGregorianCalendar(java.sql.Timestamp sqlDate) {
Date date = new Date(sqlDate.getTime());
GregorianCalendar gc = new GregorianCalendar();
gc.setTime(date);
XMLGregorianCalendar xmlDate = null;
try {
xmlDate = DatatypeFactory.newInstance().newXMLGregorianCalendar(gc);
} catch (DatatypeConfigurationException e) {
throw new RuntimeException(e);
}
return xmlDate;
}
}
2.自定义TypeHandler使用
插入数据:
<insert id="insertShipAdviceInfoRouteSrv" parameterType="wsstub.sscm.importShipAdviceInfoRouteSrv.INPUTCOLLECTIONITEM" >
insert into SOA_SHIP_ADVICE_INFO_ROUTE
(pri_key,
create_date,
send_time,
expect_time,
suggest_time)
values
(#{prikey},
#{createdate, typeHandler=com.sinoprof.core.myBatis.GregorianCalendarToDateTypeHandler},
#{sendtime, typeHandler=com.sinoprof.core.myBatis.GregorianCalendarToDateTypeHandler},
#{expecttime, typeHandler=com.sinoprof.core.myBatis.GregorianCalendarToDateTypeHandler},
#{suggesttime, typeHandler=com.sinoprof.core.myBatis.GregorianCalendarToDateTypeHandler}
</insert>
最新评论
网飞没问题, 迪士尼+有解决方案么?
pp助手是安卓手机用的,根本下载用不来苹果
已解决
这样的话数据库里的结构为{"attachment":{"content":"xxx"}}, 要怎么才能变成{"content":"xxx"},从而使结构保持一致?
赞! make test不过的坑都写到的,谢谢楼主~
谢谢你
用了root用户还是一直502是怎么回事呢
student id 是空的