最近做项目,要求<sqlMap resource="">支持通配符,翻了翻ibatis的源码,修改了原来的类com.ibatis.sqlmap.engine.builder.xml.SqlMapConfigParser 的addSqlMapNodelets()方法,
可以像这样
<sqlMap resource="classpath*:com/liwj/example/ibatis/sql/*.xml" />
来配置
原方法源码如下
parser.addNodelet("/sqlMapConfig/sqlMap", new Nodelet() {
public void process(Node node) throws Exception {
vars.errorCtx.setActivity("loading the SQL Map resource");
Properties attributes = NodeletUtils.parseAttributes(node, vars.properties);
String resource = attributes.getProperty("resource");
String url = attributes.getProperty("url");
if (usingStreams) {
InputStream inputStream = null;
if (resource != null) {
vars.errorCtx.setResource(resource);
inputStream = Resources.getResourceAsStream(resource);
} else if (url != null) {
vars.errorCtx.setResource(url);
inputStream = Resources.getUrlAsStream(url);
} else {
throw new SqlMapException("The <sqlMap> element requires either a resource or a url attribute.");
}
if (vars.sqlMapConv != null) {
inputStream = vars.sqlMapConv.convertXml(inputStream);
}
new SqlMapParser(vars).parse(inputStream);
} else {
Reader reader = null;
if (resource != null) {
vars.errorCtx.setResource(resource);
reader = Resources.getResourceAsReader(resource);
} else if (url != null) {
vars.errorCtx.setResource(url);
reader = Resources.getUrlAsReader(url);
} else {
throw new SqlMapException("The <sqlMap> element requires either a resource or a url attribute.");
}
if (vars.sqlMapConv != null) {
reader = vars.sqlMapConv.convertXml(reader);
}
new SqlMapParser(vars).parse(reader);
}
}
});
针对此方法,修改后的代为如下
private class ResourceUtil extends PathMatchingResourcePatternResolver{
protected String determineRootDir(String arg0) {
// TODO Auto-generated method stub
String rootDir= super.determineRootDir(arg0);
if(rootDir.startsWith(ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX));
rootDir=rootDir.substring(ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX.length());
return rootDir;
}
}
protected String[] getAllResource(String resource){
String[] file_resource=null;
ResourceLoader resourceLoader=new PathMatchingResourcePatternResolver();
try{
Resource[] a_resource=((ResourcePatternResolver) resourceLoader).getResources(resource);
ResourceUtil resourceUtil=new ResourceUtil();
String rootDir=resourceUtil.determineRootDir(resource);
if(a_resource!=null){
file_resource=new String[a_resource.length];
for(int i=0;i<a_resource.length;i++){
file_resource[i]=rootDir+"/"+a_resource[i].getFilename();
System.out.println("fileName="+file_resource[i]);
}
}
}catch(IOException e){
throw new SqlMapException("The <sqlMap> element requires either a resource or a url attribute.");
}
return file_resource;
}
protected void addSqlMapNodelets() {
parser.addNodelet("/sqlMapConfig/sqlMap", new Nodelet() {
public void process(Node node) throws Exception {
vars.errorCtx.setActivity("loading the SQL Map resource");
Properties attributes = NodeletUtils.parseAttributes(node, vars.properties);
String resource = attributes.getProperty("resource");
String url = attributes.getProperty("url");
if(resource.indexOf("*")>0){
String[] files=getAllResource(resource);
for(int i=0;i<files.length;i++){
addSqlMapConfigFiles(files[i], url);
}
}else{
addSqlMapConfigFiles(resource, url);
}
}
代码中使用了spring的PathMatchingResourcePatternResolver类来分析带有通配符的resource的值。由于PathMatchingResourcePatternResolver.determineRootDir方法是protected,故写了内部类继承PathMatchingResourcePatternResolver。
由于水平有限,只能改源代码如上,大家有什么改进或者优化之类的,可以继续修改,大家共同学习,共同进步
分享到:
相关推荐
ibatis 配置文件详解
ibatis配置ibatis配置ibatis配置ibatis配置ibatis配置 以及相关资料
iBATIS是以SQL为中心的持久化层框架。能支持懒加载、关联查询、继承等特性
ibatis配置文件、映射文件详解.doc
spring ibatis 配置spring ibatis 配置spring ibatis 配置spring ibatis 配置
常用IBATIS配置,返回新建数据ID,分页查询,查询条件,模糊查询,date时间段,list集合,截止日期
详细的ibatis配置文件,初来乍到,先打个招呼 ...
ibatis用xml配置文件配置的使用,基本包括了ibatis遇到的所有问题
ibatis配置文件模板.包括(SqlMap.properties和SqlMapConfig.xml以及跟javabean的映射文件)
ibatis-sqlmap-2.3.0中sqlmap支持通配符
oracle ibatis 配置
ibatis 配置教程 本人通过此文档学会写ibatis实例 ibatis 配置教程 本人通过此文档学会写ibatis实例 ibatis 配置教程 本人通过此文档学会写ibatis实例 ibatis 配置教程 本人通过此文档学会写ibatis实例
使用ibatis可以很好的操作数据库,对数据进行怎删改查,自动封装数据
spring和ibatis配置与模板
ibatis配置和教程
Ibatis基本配置---[环境搭建
log4j和ibatis配置文档 留在这里,大家可以参考,也可以用来给自己备忘。
ibatis 实例 配置 源码 依赖包 ibatis-2.3.4.726.jar commons-logging-api.jar commons-logging-1.1.jar commons-dbcp-1.2.2.jar commons-pool.jar
springMVC整合ibatis,springMVC采用注解方式,配置详细
ibatis配置文件、映射文件详解,里面详细介绍了ibatis的配置文件以及映射文件的详细解释,通过这你能很好的了解ibatis配置文件以及映射文件的含义。。。