使用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>
最新评论
楼主大宝宝听的那个asmr网站发我呗
如果是买的美国地域的,那是不是节点就只有美国的?
已经在内容偏好选择关闭了 还是看不到的 去搜索R18,TIKTOK直接显示社区没这样的内容
看不到R18内容的