R
R
Rocket-API
Search…
自定义数据源
实现接口com.github.alenfive.rocketapi.datasource.DataSourceDialect

SQL默认实现:

1
/**
2
* 关系型数据源,JdbcTemplate所操作的数据源
3
*/
4
public class SqlDataSource extends DataSourceDialect {
5
6
protected JdbcTemplate jdbcTemplate;
7
8
protected NamedParameterJdbcTemplate parameterJdbcTemplate;
9
10
private SqlDataSource(){}
11
12
public SqlDataSource(JdbcTemplate jdbcTemplate) {
13
this.jdbcTemplate = jdbcTemplate;
14
this.parameterJdbcTemplate = new NamedParameterJdbcTemplate(jdbcTemplate);
15
}
16
17
public SqlDataSource(JdbcTemplate jdbcTemplate, boolean storeApi) {
18
this.storeApi = storeApi;
19
this.jdbcTemplate = jdbcTemplate;
20
this.parameterJdbcTemplate = new NamedParameterJdbcTemplate(jdbcTemplate);
21
}
22
23
24
@Override
25
public void saveApiInfo(ApiInfo apiInfo) {
26
ApiJpaUtil.insert(parameterJdbcTemplate,apiInfo);
27
}
28
29
@Override
30
public ApiInfo findApiInfoById(ApiInfo apiInfo) {
31
return ApiJpaUtil.findById(parameterJdbcTemplate,apiInfo);
32
}
33
34
@Override
35
public void deleteApiInfo(ApiInfo apiInfo) {
36
ApiJpaUtil.deleteById(parameterJdbcTemplate,apiInfo);
37
}
38
39
@Override
40
public void updateApiInfo(ApiInfo apiInfo) {
41
ApiJpaUtil.updateById(parameterJdbcTemplate,apiInfo);
42
}
43
44
@Override
45
public List<ApiInfo> listApiInfoByEntity(ApiInfo apiInfo) {
46
return ApiJpaUtil.listByEntity(parameterJdbcTemplate,apiInfo);
47
}
48
49
@Override
50
public void saveApiInfoHistory(ApiInfoHistory apiInfoHistory) {
51
ApiJpaUtil.insert(parameterJdbcTemplate,apiInfoHistory);
52
}
53
54
@Override
55
public List<ApiInfoHistory> listApiInfoHistoryByEntity(ApiInfoHistory apiInfoHistory, IApiPager apiPager, Page page) {
56
return ApiJpaUtil.pageByEntity(parameterJdbcTemplate,apiInfoHistory,this,apiPager,page);
57
}
58
59
@Override
60
public void saveApiExample(ApiExample apiExample) {
61
ApiJpaUtil.insert(parameterJdbcTemplate,apiExample);
62
}
63
64
@Override
65
public List<ApiExample> listApiExampleByEntity(ApiExample apiExample, IApiPager apiPager, Page page) {
66
return ApiJpaUtil.pageByEntity(parameterJdbcTemplate,apiExample,this,apiPager,page);
67
}
68
69
@Override
70
public void deleteExample(ApiExample apiExample) {
71
ApiJpaUtil.deleteById(parameterJdbcTemplate,apiExample);
72
}
73
74
@Override
75
public void saveApiConfig(ApiConfig apiConfig) {
76
ApiJpaUtil.insert(parameterJdbcTemplate,apiConfig);
77
}
78
79
@Override
80
public void updateApiConfig(ApiConfig apiConfig) {
81
ApiJpaUtil.updateById(parameterJdbcTemplate,apiConfig);
82
}
83
84
@Override
85
public List<ApiConfig> listApiConfigByEntity(ApiConfig apiConfig) {
86
return ApiJpaUtil.listByEntity(parameterJdbcTemplate,apiConfig);
87
}
88
89
90
@Override
91
public List<Map<String,Object>> find(StringBuilder script, ApiInfo apiInfo, ApiParams apiParams) {
92
List<Map<String,Object>> resultList = jdbcTemplate.queryForList(script.toString());
93
return resultList.stream().map(this::toReplaceKeyLow).collect(Collectors.toList());
94
}
95
96
@Override
97
public Long update(StringBuilder script, ApiInfo apiInfo, ApiParams apiParams) {
98
return Long.valueOf(jdbcTemplate.update(script.toString()));
99
}
100
101
@Override
102
public Long remove(StringBuilder script, ApiInfo apiInfo, ApiParams apiParams) {
103
return Long.valueOf(jdbcTemplate.update(script.toString()));
104
}
105
106
@Override
107
public Object insert(StringBuilder script, ApiInfo apiInfo, ApiParams apiParams) {
108
KeyHolder keyHolder = new GeneratedKeyHolder();
109
PreparedStatementCreator preparedStatementCreator = con -> {
110
PreparedStatement ps = con.prepareStatement(script.toString(), Statement.RETURN_GENERATED_KEYS);
111
return ps;
112
};
113
jdbcTemplate.update(preparedStatementCreator, keyHolder);
114
return keyHolder.getKeyList().stream().map(item->item.get("GENERATED_KEY")).collect(Collectors.toList());
115
}
116
117
@Override
118
public String buildCountScript(String script, ApiInfo apiInfo, ApiParams apiParams, IApiPager apiPager,Page page) {
119
return script;
120
}
121
122
@Override
123
public String buildPageScript(String script, ApiInfo apiInfo, ApiParams apiParams, IApiPager apiPager,Page page) {
124
return script;
125
}
126
127
@Override
128
public String transcoding(String param) {
129
return param
130
.replace("\'","\\\'");
131
}
132
133
@Override
134
public List<TableInfo> buildTableInfo() {
135
return null;
136
}
137
}
138
Copied!
Last modified 10mo ago
Copy link
Contents