再谈让Java支持多行文本


声明:本文转载自https://my.oschina.net/drinkjava2/blog/1611028,转载目的在于传递更多信息,仅供学习交流之用。如有侵权行为,请联系我,我会及时删除。

打算抄袭一下MyBatis的Java注解方式配置,添加到在正在开发的持久层工具里,只需要定义一个接口方法,就可以用一个动态代理来使用它,这种方式的优点是可以利用Java的导舤功能快速定位SQL,比文本方式保存的模板定位方便,而且方法名和参数都是Java强类型,支持重构:

@Select("select * from users where id = #{id}") User getUserById(Integer id);

但还是老问题,Java对多行文本的支持太差,MyBatis的解决方法个人不是太喜欢,破坏了SQL的可读性:

private String selectPersonSql() {   return new SQL() {{     SELECT("P.ID, P.USERNAME, P.PASSWORD, P.FULL_NAME");     SELECT("P.LAST_NAME, P.CREATED_ON, P.UPDATED_ON");     FROM("PERSON P");     FROM("ACCOUNT A");     INNER_JOIN("DEPARTMENT D on D.ID = P.DEPARTMENT_ID");     INNER_JOIN("COMPANY C on D.COMPANY_ID = C.ID");     WHERE("P.ID = A.ID");     WHERE("P.FIRST_NAME like ?");     OR();     WHERE("P.LAST_NAME like ?");     GROUP_BY("P.ID");     HAVING("P.LAST_NAME like ?");     OR();     HAVING("P.FIRST_NAME like ?");     ORDER_BY("P.ID");     ORDER_BY("P.FULL_NAME");   }}.toString(); }

本人以前发过一篇文章,试图用Java注释的方式来支持多行文本,以方便SQL的定位(见https://my.oschina.net/drinkjava2/blog/892309),但是那篇文章中的方法有三个问题,一是Java文件在布署时,要手工拷贝一份到resource目录中去,不方便;二是不小心注释会被编辑器给格式化了;三是需要手工在程序中给出Java源文件的绝对路径。最近绞尽脑汁,又找到一个好一点的方法,依然是利用Java注释,但是没有以上三个缺点了:

做法: 
1. 在pom.xml中添加一个build-helper-maven-plugin插件,将resource目录加入编译范围,插件会将所有在resources下的所有Java文件当作资源文件打包,这样就不用再手工拷贝一份到resouces目录下去了。而且经实测,这个插件在运行 mvn eclipse:eclipse 命令时能正确添加resources目录到eclipse中的编译目录:

 <build>     <plugins>       ......       <plugin>         <groupId>org.codehaus.mojo</groupId>         <artifactId>build-helper-maven-plugin</artifactId>         <version>1.7</version>         <executions>           <execution>             <id>add-source</id>             <phase>generate-sources</phase>             <goals>               <goal>add-source</goal>             </goals>             <configuration>               <sources>                 <source>src/main/resources</source>                 <source>src/test/resources</source>               </sources>             </configuration>           </execution>         </executions>       </plugin>    <plugins>  </build>     

2. 写一个工具类,用来读取Java源码, 在Eclipse环境下时,从project/src/main或project/src/text下读,在布署环境下时,从类根目录下读取(因为resources下的Java文件被当作资源文件打包):

public class TextUtils {  	private static final Map<Class<?>, String> javaFileCache = new HashMap<Class<?>, String>();  	@SuppressWarnings("all") 	public static String getJavaSourceCodeUTF8(Class<?> clazz) { 		return getJavaSourceCode(clazz, "UTF-8"); 	}  	@SuppressWarnings("all") 	public static String getJavaSourceCode(Class<?> clazz, String encoding) { 		if (javaFileCache.containsKey(clazz)) 			return javaFileCache.get(clazz); 		String classPathName = StringUtils.substringBefore(clazz.getName(), "$");// aa.bb.Cc 		classPathName = "/" + StringUtils.replace(classPathName, ".", "/");// aa/bb/Cc 		String fileName = classPathName + ".java";// /aa/bb/Cc.java 		InputStream inputStream = null; 		try { 			inputStream = TextUtils.class.getResourceAsStream(fileName); 			if (inputStream == null) {// Not found, it means in eclipse environment 				File file = new File(clazz.getResource(classPathName + ".class").getFile()); 				String absPath = file.getAbsolutePath(); 				absPath = StringUtils.replace(absPath, "\\", "/"); 				String projectFolder = StringUtils.substringBefore(absPath, "/target/classes/"); 				String realPath = projectFolder + "/src/main/resources" + classPathName + ".java"; 				file = new File(realPath); 				if (!file.exists()) { 					realPath = projectFolder + "/src/test/resources" + classPathName + ".java"; 					file = new File(realPath); 				} 				if (!file.exists()) 					throw new IOException("Can not find " + clazz.getName() + ".java in resources folder"); 				inputStream = new FileInputStream(file); 				if (inputStream == null) 					throw new IOException("Can not find " + clazz.getName() + ".java in resources folder"); 			} 			ByteArrayOutputStream result = new ByteArrayOutputStream(); 			byte[] buffer = new byte[1024]; 			int length; 			while ((length = inputStream.read(buffer)) != -1) 				result.write(buffer, 0, length); 			String javaSrc = result.toString(encoding); 			javaFileCache.put(clazz, javaSrc); 			return javaSrc; 		} catch (IOException e) { 			throw new RuntimeException(e); 		} finally { 			if (inputStream != null) 				try { 					inputStream.close(); 				} catch (IOException e) { 					throw new RuntimeException(e); 				} 		} 	}  }

3. 在src/main/resources或src/test/resources目录下添加需要访问源码的Java文件如:

public interface TextSample1 { 	@SQL("select *from User") 	public <T> T retrieveAllUsers();  	@Handler(MapListHandler.class) //DbUtils自带的 	public <T> T retrieveUserById(String firstName, String lastName); 	/*- 	 SELECT P.ID, P.USERNAME, P.PASSWORD, P.FULL_NAME  	 P.LAST_NAME,P.CREATED_ON, P.UPDATED_ON   	 FROM PERSON P, ACCOUNT A  	 INNER JOIN DEPARTMENT D on D.ID = P.DEPARTMENT_ID   	 INNER JOIN COMPANY C on D.COMPANY_ID = C.ID  	 WHERE (P.ID = A.ID AND P.FIRST_NAME like #{firstName})  	 OR (P.LAST_NAME like #{lastName})   	 GROUP BY P.ID   	 HAVING (P.LAST_NAME like #{lastName})   	 OR (P.FIRST_NAME like #{firstName})   	 ORDER BY P.ID, P.FULL_NAME 	 */   }

4.在程序的任何地方,就可以用以下代码获取java的源码,以便做进一步处理了(抽取出对应方法的多行文本,这一步我还没有做,但1到4都测试过了,所以想当然是没问题的)。

TextUtils.getJavaSourceCodeUTF8(TextSample1.class)

对了,差点忘了说,所有注释必须用/*-开头,这样就不会被Eclipse给误格式化了。
以上方法只在Eclipse+Maven 开发方式下实测过,其它编辑器不在考虑范围内。

 

 

 

 

 

 

 

本文发表于2018年01月22日 16:32
(c)注:本文转载自https://my.oschina.net/drinkjava2/blog/1611028,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责。如有侵权行为,请联系我们,我们会及时删除.

阅读 3985 讨论 0 喜欢 0

抢先体验

扫码体验
趣味小程序
文字表情生成器

闪念胶囊

你要过得好哇,这样我才能恨你啊,你要是过得不好,我都不知道该恨你还是拥抱你啊。

直抵黄龙府,与诸君痛饮尔。

那时陪伴我的人啊,你们如今在何方。

不出意外的话,我们再也不会见了,祝你前程似锦。

这世界真好,吃野东西也要留出这条命来看看

快捷链接
网站地图
提交友链
Copyright © 2016 - 2021 Cion.
All Rights Reserved.
京ICP备2021004668号-1