步驟一 、 VO類
import java.sql.Date;
public class User {
//實體類的屬性和表的字段名稱一一對應
private int id;
private String name;
private int age;
private Date hire_date;
}
步驟二 、數據庫表:
/*
Navicat MySQL Data Transfer
Source Server : localhost
Source Server Version : 50051
Source Host : localhost:3306
Source Database : test
Target Server Type : MYSQL
Target Server Version : 50051
File Encoding : 65001
Date: 2016-05-17 20:46:04
*/
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for `user`
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(30) default NULL,
`age` int(11) default NULL,
`hire_date` datetime default NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES ('1', 'zhangsan', '12', '2016-05-17 20:06:38');
步驟三、開始轉換
Exception in thread "main" net.sf.json.JSONException: java.lang.reflect.InvocationTargetException
at net.sf.json.JSONObject.defaultBeanProcessing(JSONObject.java:818)
at net.sf.json.JSONObject._fromBean(JSONObject.java:699)
at net.sf.json.JSONObject.fromObject(JSONObject.java:172)
at net.sf.json.AbstractJSON._processValue(AbstractJSON.java:274)
at net.sf.json.JSONObject._processValue(JSONObject.java:2655)
at net.sf.json.JSONObject.processValue(JSONObject.java:2721)
at net.sf.json.JSONObject.setInternal(JSONObject.java:2736)
at net.sf.json.JSONObject.setValue(JSONObject.java:1424)
at net.sf.json.JSONObject.defaultBeanProcessing(JSONObject.java:765)
at net.sf.json.JSONObject._fromBean(JSONObject.java:699)
at net.sf.json.JSONObject.fromObject(JSONObject.java:172)
at net.sf.json.JSONObject.fromObject(JSONObject.java:134)
at mybatistest.mybatisdemo.MultiDataSource.main(MultiDataSource.java:50)
解決方案一:datetime -->String
select id,name ,age,CONCAT(hire_date,'') from user
解決方案二:增加函數轉為util.Date進行處理
/**
* 將java.util.Date日期轉化為java.sql.Date
* @param udate
* @return
*/
public static java.sql.Date converUtilToSql(java.util.Date udate) {
return new java.sql.Date(udate.getTime());
}
/**
* 將java.sql.Date日期轉化為java.util.Date
* @param udate
* @return
*/
public static java.util.Date converSqlToUtil(java.sql.Date udate) {
return new java.util.Date(udate.getTime());
}
處理結果如下:
{"age":12,"hire_date":{"date":17,"day":2,"hours":0,"minutes":0,"month":4,"seconds":0,"time":1463414400000,"timez
oneOffset":-480,"year":116},"id":1,"name":"zhangsan"}
解決方案三:自定義轉換器
核心代碼如下:
JsonConfig jsonConfig = new JsonConfig();
jsonConfig.registerJsonValueProcessor(java.sql.Date.class, new JsonDateValueProcessor());
JSONObject json = JSONObject.fromObject(user,jsonConfig);
System.out.println("=====total count getUser====" + json);
public class JsonDateValueProcessor implements JsonValueProcessor {
private String format = "yyyy-MM-dd";
public Object processArrayValue(Object arg0, JsonConfig arg1) {
System.out.println("====processArrayValue======== "+arg0);
return null;
}
public Object processObjectValue(String arg0, Object arg1, JsonConfig arg2) {
System.out.println("====processObjectValue======== "+arg0 +",arg1="+arg1);
return processJsonDateValue(arg1);
}
private Object processJsonDateValue(Object arg0) {
SimpleDateFormat sdf=new SimpleDateFormat(format);
return sdf.format(arg0);
}
}
處理結果如下:
=====total count getUser====User [id=1, name=zhangsan, age=12,hire_date=2016-05-17]
====processObjectValue======== hire_date,arg1=2016-05-17
=====total count getUser===={"age":12,"hire_date":"2016-05-17","id":1,"name":"zhangsan"}