Spring 项目中把 SQL 语句写在 .sql 文件中

我们在使用 JDBC 时, 如果把所有的 SQL 语句全写在 Java 文件中, 由于 Java 不支持 Here Document, 多行字符串要么用加号, 要么用 Java 8 的 String.join() 方法来连接, 同时不能对 SQL 语句进行语法加亮, 所以这样的 SQL 字符串阅读性很差. 别说为何不用 Hibernate 之类的而不直接写原始的 SQL 语句, 在操作复杂的系统时还是会用到 JdbcTemplate 吧.


所以我们希望能把 SQL 语句写在单独的 *.sql 文件里, 这样很多编辑器就能语法高亮显示, 或在输入时还能得到智能提示. 有种办法是把 *.sql 用作为属性文件, 那么在其中定义多行的 SQL 语句时就得这样
select.user=select id, firstname, lastname, address \
  from users \
  where id=?
加载后就能用 getProperty("select.user") 来引用相应的语句了. 属性文件的换行与 Bash  一样, 也是用  \, 但如此, 则 *.sql 并非一个纯粹的 SQL 文件, 不能正确的进行语法加亮, 一旦写上 SQL 的注释 -- 就更是在添乱了.

所以我们的第二个方案是: 首先 *.sql 就该是一个真正的  SQL 文件, 而不是伪装的属性文件, 为了能在程序中引用每一条 SQL 语句, 我们该如何表示各自的 Key 呢? 这里的灵感仍然是来自于 Linux Shell, 在 Linux Shell 中指定执行环境的用了特殊的注释方式 #!, 如
#!/bin/bash
#!/usr/bin/env python
依葫芦画瓢, SQL 的标准单注释是 --, 因而我们也创建一个特别的注释 --!, , 其后的字符串就是接下来 SQL 语句的 Key. 举例如下
--!select.user
select id, firstname, lastname, address
  from users
  where id=?

--!update.user
update ........
--! 之后是 key select.user, 往下在未到文件结束, 或是遇到下一个 --! 之前就是这个 key 对应的完整 SQL 语句的内容.

本文以 Spring 项目为例来演示如何应这个 SQL 文件, 其实在其他类型的 Java 项目中同样可以借鉴.

因为这是一个真正的 SQL 文件, 所以在 Spring 中我们无法直接作为属性文件来加载. 假设我们把该文件存储为 src/resources/sql/queries.sql, 因此我们不能直接用
@PropertySource(value = "classpath:sql/queries.sql")
public class AppConfig {  ......  }
加载该文件.

幸好 PropertySource 注解还有一个属性 factory, 类型为 PropertySourceFactory, 这就是我们作文章的地方, 马上着手自定义一个 SqlPropertySourceFactory, 在其中总有办法把一个 *.sql 的内容转换为 Properties. 因此将来我们要加载  sql/queries.sql 文件所用的注解形式就会是
@PropertySource(value = "classpath:sql/queries.sql", factory = SqlPropertySourceFactory.class)
public class AppConfig { ......}
接下来就是本文的关键, 看看 SqlPropertySourceFactory 的实现

SqlPropertySourceFactory.java
 1package cc.unmi;
 2
 3import org.springframework.core.env.MapPropertySource;
 4import org.springframework.core.env.PropertySource;
 5import org.springframework.core.io.support.EncodedResource;
 6import org.springframework.core.io.support.PropertySourceFactory;
 7
 8import java.io.BufferedReader;
 9import java.io.IOException;
10import java.util.*;
11import java.util.stream.Collectors;
12
13public class SqlPropertySourceFactory implements PropertySourceFactory {
14
15    private static final String KEY_LEADING = "--!";
16
17    @Override
18    public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {
19
20        Deque<Pair> queries = new LinkedList<>();
21
22        new BufferedReader(resource.getReader()).lines().forEach(line -> {
23            if (line.startsWith(KEY_LEADING)) {
24                queries.addLast(new Pair(line.replaceFirst(KEY_LEADING, "")));
25            } else if (line.startsWith("--")) {
26                //skip comment line
27            } else if (!line.trim().isEmpty()) {
28                Optional.ofNullable(queries.getLast()).ifPresent(pair -> pair.lines.add(line));
29            }
30        });
31
32        Map<String, Object> sqlMap = queries.stream()
33                .filter(pair -> !pair.lines.isEmpty())
34                .collect(Collectors.toMap(pair -> pair.key,
35                        pair -> String.join(System.lineSeparator(), pair.lines),
36                        (r, pair) -> r, LinkedHashMap::new));
37
38        System.out.println("Configured SQL statements:");
39        sqlMap.forEach((s, o) -> System.out.println(s + "=" + o));
40
41        return new MapPropertySource(resource.toString(), sqlMap);
42    }
43
44    private static class Pair {
45        private String key;
46        private List<String> lines = new LinkedList<>();
47
48        Pair(String key) {
49            this.key = key;
50        }
51    }
52}

我们定义的 src/resources/sql/queries.sql 文件内容如下:
 1--external queries in this file
 2
 3--!select_users_by_id
 4select id, firstname, lastname, address
 5 from users where id=?
 6
 7--!add_user
 8insert users(id, firstname, lastname, address)
 9  values(DEFAULT, ?, ?, ?)
10--
11
12--!no_statement
13---
14
15--!update
16update users set firstname=? where id=?

最后是如何应用它, 我们以 SpringBoot 的方式来启动一个 Spring 项目

DemoApplication.java
 1package cc.unmi;
 2
 3import org.springframework.beans.factory.annotation.Value;
 4import org.springframework.boot.SpringApplication;
 5import org.springframework.boot.autoconfigure.SpringBootApplication;
 6import org.springframework.context.EnvironmentAware;
 7import org.springframework.context.annotation.Bean;
 8import org.springframework.context.annotation.PropertySource;
 9import org.springframework.core.env.Environment;
10
11@SpringBootApplication
12@PropertySource(value = "classpath:sql/queries.sql", factory = SqlPropertySourceFactory.class)
13public class DemoApplication implements EnvironmentAware {
14
15    private Environment env;
16
17    @Value("${add_user}")
18    private String sqlAddUser;
19
20    @Bean
21    public String testBean() {
22        System.out.println("SQL_1:" + env.getProperty("select_users_by_id"));
23        System.out.println("SQL_2:" + sqlAddUser);
24        return "testBean";
25    }
26
27    public static void main(String[] args) {
28        SpringApplication.run(DemoApplication.class, args);
29    }
30
31    @Override
32    public void setEnvironment(Environment environment) {
33        env = environment;
34    }
35}

既然已转换为普通的属性了, 所以可以通过表达式 ${key}env.getProperty("key") 来引用它们.

执行上面的代码, 输出如下:
Configured SQL statements:
select_users_by_id=select id, firstname, lastname, address
 from users where id=?
add_user=insert users(id, firstname, lastname, address)
  values(DEFAULT, ?, ?, ?)
update=update users set firstname=? where id=?
SQL_1:select id, firstname, lastname, address
 from users where id=?
SQL_2:insert users(id, firstname, lastname, address)
  values(DEFAULT, ?, ?, ?)
就这么简单. 当然那个 *.sql 文件最好是写得严谨一些, 我们可以将来对 SqlPropertySourceFactory 进行逐步完善以应对更多的可能. 不管怎么说它是一个真正的 SQL 文件, 在代码中也能像任何别的属性那么方便的引用其中定义的  SQL 语句了. 永久链接 https://yanbin.blog/spring-external-sql-statements/, 来自 隔叶黄莺 Yanbin's Blog
[版权声明] 本文采用 署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0) 进行许可。