oracle mybatis resultMap返回时间格式化
时间: 2024-01-18 11:41:50 浏览: 34
可以使用Java中的SimpleDateFormat类对时间进行格式化。具体实现可以参考以下代码示例:
```xml
<resultMap id="userMap" type="User">
<result property="id" column="id"/>
<result property="name" column="name"/>
<result property="age" column="age"/>
<result property="birthday" column="birthday"
jdbcType="TIMESTAMP"
javaType="java.util.Date"
typeHandler="org.apache.ibatis.type.DateTypeHandler"
resultMap="timeMap"/>
</resultMap>
<resultMap id="timeMap" type="java.util.Date">
<result property="date" column="birthday"
jdbcType="TIMESTAMP"
javaType="java.util.Date"
typeHandler="org.apache.ibatis.type.DateTypeHandler">
<typeHandler>
<arg column="pattern" javaType="String" value="yyyy-MM-dd HH:mm:ss"/>
<handler type="org.apache.ibatis.type.CustomDateTypeHandler"/>
</typeHandler>
</result>
</resultMap>
```
以上是在mapper.xml文件中配置resultMap的方法,其中使用了DateTypeHandler和CustomDateTypeHandler两个类型处理器对时间进行格式化。值得注意的是,CustomDateTypeHandler需要自己实现,具体实现方法可以参考MyBatis官方文档或者其他相关资料。
另外,如果使用Java代码进行时间格式化,可以参考以下示例代码:
```java
import java.text.SimpleDateFormat;
import java.util.Date;
public class Test {
public static void main(String[] args) {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String formattedDate = sdf.format(date);
System.out.println(formattedDate);
}
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)