@ColDefine(adaptor=ValueAdaptor.class) 可以自行扩展为任意类型的读写
不理解这个怎么实现
6 回复
@wendal
接口的原理是什么?这个不清楚,有一些丈二和尚摸不到头脑
接口已实现
我想实现 存储字段类型为VARCHAR,里面的内容为array数据,我想实现之间互转
但是我不知道怎么调用,在字段定义里面怎么写
@ColDefine(type = ColType.VARCHAR, width = 225,adaptor=ArrayAdaptor.class)
private List promotionArea;
是这样写?
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.rekoe.adaptor.impl;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import org.nutz.dao.jdbc.ValueAdaptor;
/**
*
* @author hange
*/
public class arrayAdaptor implements ValueAdaptor {
@Override
public Object get ( ResultSet rs, String colName ) throws SQLException{
boolean re = rs.getBoolean(colName);
return rs.wasNull() ? null : re;
}
@Override
public void set ( PreparedStatement stat, Object obj, int index ) throws SQLException{
if (null == obj) {
stat.setNull(index, Types.INTEGER);
} else {
boolean v;
if (obj instanceof Boolean)
v = (Boolean) obj;
else if (obj instanceof Number)
v = ((Number) obj).intValue() > 0;
else if (obj instanceof Character)
v = Character.toUpperCase((Character) obj) == 'T';
else
v = Boolean.valueOf(obj.toString());
stat.setBoolean(index, v);
}
}
}
@qq_8136064a
public abstract class JsonValueAdaptor implements ValueAdaptor {
@Override
public Object get(ResultSet rs, String colName) throws SQLException{
String obj = rs.getString(colName);
return Json.fromJsonAsList(JsonClassType(), obj == null ? "[]" : obj);
}
@Override
public void set(PreparedStatement stat, Object obj, int index) throws SQLException{
stat.setString(index, (obj == null) ? "[]" : Json.toJson(obj, JsonFormat.compact()));
}
public abstract Class<?> JsonClassType();
}
code
public class IntegerJsonValueAdaptor extends JsonValueAdaptor {
@Override
public Class<?> JsonClassType() {
return Integer.class;
}
}
@Default 可以写字符型的默认值, 然而大部分数据库都没有"boolean"类型的字段类型, 更不能写true/false之类的默认值
添加回复
请先登陆