往数据库里存一个Map:
读取出来:
Map<String, Integer> temp = new HashMap<String, Integer>();
temp.put("mon", 1);
temp.put("tus", 1);
temp.put("wen", 3);
temp.put("thr", 1);
temp.put("fri", 1);
temp.put("sat", 0);
temp.put("sun", 0);
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(bos);
out.writeObject(temp);
final LobHandler lobhander = new DefaultLobHandler();
final ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
StringBuffer sql = new StringBuffer("insert into test(testid,name,accrual) values(?, ?, ?)");
this.getJdbcTemplate().execute(sql.toString(),
new AbstractLobCreatingPreparedStatementCallback(lobhander) {
protected void setValues(PreparedStatement ps,
LobCreator lobCreator) throws SQLException,
DataAccessException {
ps.setInt(1, 1);
ps.setString(2, "aaa");
lobCreator.setBlobAsBinaryStream(ps, 3, bis, bis.available());
}
}
);
} catch(Exception e) {
e.printStackTrace();
}
读取出来:
final LobHandler lobhander = new DefaultLobHandler();
this.getJdbcTemplate().query("select * from test",
new RowMapper() {
public Object mapRow(ResultSet rs, int arg1) throws SQLException {
Map result = new HashMap();
Blob blob = rs.getBlob("accrual");
//byte[] bbb = lobhander.getBlobAsBytes(rs, "accrual");
ByteArrayInputStream bais = new ByteArrayInputStream(blob.getBytes(1, (int)blob.length()));
try {
ObjectInputStream oo = new ObjectInputStream(bais);
HashMap hm = (HashMap)oo.readObject();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
);