想在拦截器里面设置对象的updateTime, 下面的写法好像实现不了
DaoStatement st = chain.getDaoStatement();
if (st instanceof NutPojo) {
if (st.isUpdate()) {
((NutPojo) st).addParamsBy(Chain.make("updateTime", new Date()));
}
}
chain.doChain();
想在拦截器里面设置对象的updateTime, 下面的写法好像实现不了
DaoStatement st = chain.getDaoStatement();
if (st instanceof NutPojo) {
if (st.isUpdate()) {
((NutPojo) st).addParamsBy(Chain.make("updateTime", new Date()));
}
}
chain.doChain();
@wendal 那可以在哪里进行拦截赋值呢?
尝试做了一下,还是可以做到的
package org.nutz.dao.impl.interceptor;
import java.lang.reflect.Field;
import java.util.ArrayList;
import org.nutz.dao.DaoException;
import org.nutz.dao.DaoInterceptor;
import org.nutz.dao.DaoInterceptorChain;
import org.nutz.dao.impl.jdbc.NutPojo;
import org.nutz.dao.impl.sql.pojo.UpdateFieldsPItem;
import org.nutz.dao.sql.DaoStatement;
import org.nutz.dao.sql.PItem;
import org.nutz.dao.util.cri.Static;
import org.nutz.lang.Mirror;
import org.nutz.log.Log;
import org.nutz.log.Logs;
public class SayHiDaoInterceptor implements DaoInterceptor {
private static final Log log = Logs.get();
protected Field itemField;
public SayHiDaoInterceptor() throws NoSuchFieldException, SecurityException {
itemField = NutPojo.class.getDeclaredField("items");
itemField.setAccessible(true);
}
@SuppressWarnings("unchecked")
public void filter(DaoInterceptorChain chain) throws DaoException {
log.debug("before >>> " + chain.getDaoStatement().toPreparedStatement());
DaoStatement ds = chain.getDaoStatement();
if (ds.isUpdate() && ds instanceof NutPojo) {
NutPojo np = (NutPojo)ds;
try {
ArrayList<PItem> items = (ArrayList<PItem>) itemField.get(np);
for (int i = 0; i < items.size(); i++) {
if (items.get(i) instanceof UpdateFieldsPItem) {
items.add(i+1, new Static(", updateTime=now()"));
break;
}
}
}
catch (Throwable e) {
e.printStackTrace();
}
}
chain.doChain();
log.debug("after <<< " + chain.getDaoStatement().toPreparedStatement());
}
}