再谈让Java支持多行文本

打算抄袭一下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中的编译目录:

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

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

public abstract class TextUtils {// NOSONAR

	private static final Map<Class<?>, String> javaFileCache = new ConcurrentHashMap<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 = StrUtils.substringBefore(clazz.getName(), "$");// aa.bb.Cc
		classPathName = "/" + StrUtils.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
				File file = new File(clazz.getResource(classPathName + ".class").getFile());
				String absPath = file.getAbsolutePath();
				absPath = StrUtils.replace(absPath, "\\", "/");
				String projectFolder = StrUtils.substringBefore(absPath, "/target/");
				String realFile = projectFolder + "/src/main/resources" + classPathName + ".java";
				file = new File(realFile);
				if (!file.exists()) {
					realFile = projectFolder + "/src/test/resources" + classPathName + ".java";
					file = new File(realFile);
				}
				if (!file.exists())
					throw new IOException("Can not find '" + realFile + "' in resources folder");
				inputStream = new FileInputStream(file);
				if (inputStream == null)
					throw new IOException("Error happen when open file '" + realFile + "'");
			}
			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 开发方式下实测过,其它编辑器不在考虑范围内。

 

 

 

 

 

 

 

转载于:https://my.oschina.net/drinkjava2/blog/1611028

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值