select.user=select id, firstname, lastname, address \ from users \ where id=?
#!/bin/bash #!/usr/bin/env python
--!select.user select id, firstname, lastname, address from users where id=? --!update.user update ........
@PropertySource(value = "classpath:sql/queries.sql")
public class AppConfig { ...... }
@PropertySource(value = "classpath:sql/queries.sql", factory = SqlPropertySourceFactory.class)
public class AppConfig { ......}
package cc.unmi;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.core.io.support.PropertySourceFactory;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;
public class SqlPropertySourceFactory implements PropertySourceFactory {
private static final String KEY_LEADING = "--!";
@Override
public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {
Deque<Pair> queries = new LinkedList<>();
new BufferedReader(resource.getReader()).lines().forEach(line -> {
if (line.startsWith(KEY_LEADING)) {
queries.addLast(new Pair(line.replaceFirst(KEY_LEADING, "")));
} else if (line.startsWith("--")) {
//skip comment line
} else if (!line.trim().isEmpty()) {
Optional.ofNullable(queries.getLast()).ifPresent(pair -> pair.lines.add(line));
}
});
Map<String, Object> sqlMap = queries.stream()
.filter(pair -> !pair.lines.isEmpty())
.collect(Collectors.toMap(pair -> pair.key,
pair -> String.join(System.lineSeparator(), pair.lines),
(r, pair) -> r, LinkedHashMap::new));
System.out.println("Configured SQL statements:");
sqlMap.forEach((s, o) -> System.out.println(s + "=" + o));
return new MapPropertySource(resource.toString(), sqlMap);
}
private static class Pair {
private String key;
private List<String> lines = new LinkedList<>();
Pair(String key) {
this.key = key;
}
}
}
--external queries in this file --!select_users_by_id select id, firstname, lastname, address from users where id=? --!add_user insert users(id, firstname, lastname, address) values(DEFAULT, ?, ?, ?) -- --!no_statement --- --!update update users set firstname=? where id=?
package cc.unmi;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
@SpringBootApplication
@PropertySource(value = "classpath:sql/queries.sql", factory = SqlPropertySourceFactory.class)
public class DemoApplication implements EnvironmentAware {
private Environment env;
@Value("${add_user}")
private String sqlAddUser;
@Bean
public String testBean() {
System.out.println("SQL_1:" + env.getProperty("select_users_by_id"));
System.out.println("SQL_2:" + sqlAddUser);
return "testBean";
}
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Override
public void setEnvironment(Environment environment) {
env = environment;
}
}
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, ?, ?, ?)
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有