MyBatis中對(duì)于LocalDateTime類型數(shù)據(jù)的查詢限制主要包括以下幾點(diǎn):
MyBatis不支持直接對(duì)LocalDateTime類型數(shù)據(jù)進(jìn)行查詢,需要通過(guò)自定義TypeHandler來(lái)處理LocalDateTime類型的數(shù)據(jù)。
在Mapper.xml文件中,需要使用TypeHandler來(lái)處理LocalDateTime類型的參數(shù)和結(jié)果,例如:
<parameterMap type="map" id="myParameterMap">
<parameter property="startTime" javaType="java.time.LocalDateTime" jdbcType="TIMESTAMP" typeHandler="com.example.LocalDateTimeTypeHandler"/>
</parameterMap>
<select id="selectByTime" parameterMap="myParameterMap" resultType="com.example.User">
SELECT * FROM user WHERE start_time = #{startTime}
</select>
public class LocalDateTimeTypeHandler extends BaseTypeHandler<LocalDateTime> {
@Override
public void setNonNullParameter(PreparedStatement ps, int i, LocalDateTime parameter, JdbcType jdbcType) throws SQLException {
ps.setTimestamp(i, Timestamp.valueOf(parameter));
}
@Override
public LocalDateTime getNullableResult(ResultSet rs, String columnName) throws SQLException {
Timestamp timestamp = rs.getTimestamp(columnName);
return timestamp.toLocalDateTime();
}
@Override
public LocalDateTime getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
Timestamp timestamp = rs.getTimestamp(columnIndex);
return timestamp.toLocalDateTime();
}
@Override
public LocalDateTime getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
Timestamp timestamp = cs.getTimestamp(columnIndex);
return timestamp.toLocalDateTime();
}
}