[color=blue][/color][size=large]spring+struts+ibatis 动态数据源配置 (二)
经过上一步对spring配置文件的配置,接下来我们新建一个接口文件:
public interface IBaseSqlMapClientDaoSupport {
/**
* 多数据源选择函数
* @param name sqlClient名称
*/
public void choseSqlClient(String name);
}
同时也新建一个实现类:
public class BaseSqlMapClientDaoSupport extends SqlMapClientDaoSupport implements ApplicationContextAware {
protected ApplicationContext context;
protected ApplicationContext getContext() {
// return WebApplicationContextUtils
// .getWebApplicationContext(ServletActionContext
// .getServletContext());
return context;
}
public void choseSqlClient(String name) {
SqlMapClient client = (SqlMapClient) getContext().getBean(name);
setSqlMapClientTemplate(new SqlMapClientTemplate(client));
afterPropertiesSet();
}
public void setApplicationContext(ApplicationContext context) throws BeansException {
this.context = context;
}
}
到此为止,值得注意的地方都配置完毕,最后就是我们DAO接口和实现类的配置了,在这里我就简单的贴下代码:
public interface DemoDAO extends IBaseSqlMapClientDaoSupport{
...操作方法...
}
public class DemoDaoImpl extends BaseSqlMapClientDaoSupport implements DemoDAO {
...操作方法...
比如
public void addDemo(DemoDO demoDo) {
choseSqlClient("设置指定的数据源");【这步最好放到server实现类里面,在你要调用具体的Dao的前面设置即可。】
getSqlMapClientTemplate.insert("..",demoDo);
}
}
以上就是全部的实现流程,如有不清楚之处,还请大家指点,同时可以联系我(eye_linjing@163.com和qq:429281723)[/size]