Here's a quick and dirty DynamicMapper I created
MyDao myDao = dynamicMapper.daoProxy(MyDao.class)
creates the proxy implementation
I use Guice for DI and have a Guice provider for this and that makes it even
simpler
public class DynamicMapper {
private SqlMapClient sqlMap;
public void setSqlMap(SqlMapClient sqlMap) {
this.sqlMap = sqlMap;
}
public <T> T daoProxy(Class<T> clazz) {
InvocationHandler handler = new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method,
Object[] args)
throws Throwable {
Class<?> resultClazz = method.getReturnType();
String methodName = method.getName();
Object param = null ;
if (args!=null) param = args[0];
if (methodName.startsWith("insert")) {
return sqlMap.insert(methodName, param);
}
else if (methodName.startsWith("update")) {
return sqlMap.update(methodName, param);
}
else if (methodName.startsWith("delete")) {
return sqlMap.delete(methodName, param);
}
else if
(java.util.List.class.isAssignableFrom(resultClazz)) {
return sqlMap.queryForList(methodName,
param);
}
else if
(java.util.Map.class.isAssignableFrom(resultClazz)) {
String key;
if (args.length==1) {
param = null;
key = (String)args[0];
}
else {
key = (String)args[1];
}
return sqlMap.queryForMap(methodName,
param, key);
}
else {
return
sqlMap.queryForObject(methodName, param);
}
}
};
return (T)Proxy.newProxyInstance(clazz.getClassLoader(),
new Class[]
{clazz},
handler);
}
}
Ronald Borman wrote:
>
> I got started with iBATIS this week. I bought 'iBATIS in Action'
> (finishing the last chapter tomorrow), did some tests and browsed through
> the mailing lists. It looks like a compact, comprehensible and fun to use
> framework. Kudos to the team!
>
> On the incubator mailing list I found a May 2005 announcement which caught
> my attention: interface binding
> (http://www.mail-archive.com/[EMAIL PROTECTED]/msg02403.html).
> It reminded me of an IBM DeveloperWorks article about Generic DAOs and I
> was eager to try it out. Unfortunately SqlMapClientImpl.getMapper() hasn't
> survived (although it is still listed in the online dev Javadoc).
>
> The 2.1.7 source code only calls the static method
> MapperProxy.newMapperProxy(), so perhaps I could compile/externalize the
> missing classes. Is this feasable?
>
> The 3.0 whiteboard discussion also mentions interface binding. Is it
> likely that this functionality will return in a future version?
>
> Thanks
>
>
> Ronald
>
>
--
View this message in context:
http://www.nabble.com/Interface-binding-tp19352392p19401342.html
Sent from the iBATIS - User - Java mailing list archive at Nabble.com.