最近发现如果sql语句比较长,且参数比较多的时候(很多很多?),在sqlserver2000上预编译速度超慢,sqlserver2005快一倍左右,但还是没有直接执行静态的sql快,本来2秒就能搞定,结果在2000上要20多秒,2005上要10秒左右,jtds驱动预编译更慢. 真是比较晕,为了解决性能问题只能抛弃PreparedStatement了,直接拼装sql执行。好像跑题了,在IBATIS中使用PreparedStatement预编译sql,在定义参数的时候只要写成#id:INTEGER#即可了,如果要直接替换字符串需要改写为'$id$',如果一个一个文件手工修改的话那就太麻烦了,于是写了个正则小程序。因为代码实在太简单了,没啥可注释的,也没什么技术含量,发出来可能会帮助到一些懒人,呵呵。另外过几天有时间的时候要整理整理正则的东西了,每次写正则都不顺利嗨。
后记:替换后还是有些小工作要做的,因为在sqlserver中null和 '' 的含义是不相同的,一些诸如#id:INTEGER# is null的写法就不灵了。
public
class
ReplacePara {
public static void main(String[] args) throws IOException {
File dir = new File( " src/main/resources/com/xxxx/xxxx/model/sqlserver/ " );
File[] files = dir.listFiles( new FileFilter() {
public boolean accept(File pathname) {
if (pathname.getName().startsWith( " demo.xml " ))
return true ;
return false ;
}
});
for ( int i = 0 ; i < files.length; i ++ ) {
File file = files[i];
if (file.exists() && file.isFile()) {
replaceFile(file);
}
}
}
public static void replaceFile(File file) {
try {
String str = org.apache.commons.io.FileUtils.readFileToString(file,
" UTF-8 " );
str = str.replaceAll( " #([^#]+?):(VARCHAR|INTEGER&&[^#])# " ,
" '/$$1/$' " );
org.apache.commons.io.FileUtils.writeStringToFile(file, str,
" UTF-8 " );
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) throws IOException {
File dir = new File( " src/main/resources/com/xxxx/xxxx/model/sqlserver/ " );
File[] files = dir.listFiles( new FileFilter() {
public boolean accept(File pathname) {
if (pathname.getName().startsWith( " demo.xml " ))
return true ;
return false ;
}
});
for ( int i = 0 ; i < files.length; i ++ ) {
File file = files[i];
if (file.exists() && file.isFile()) {
replaceFile(file);
}
}
}
public static void replaceFile(File file) {
try {
String str = org.apache.commons.io.FileUtils.readFileToString(file,
" UTF-8 " );
str = str.replaceAll( " #([^#]+?):(VARCHAR|INTEGER&&[^#])# " ,
" '/$$1/$' " );
org.apache.commons.io.FileUtils.writeStringToFile(file, str,
" UTF-8 " );
} catch (IOException e) {
e.printStackTrace();
}
}
}