Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit df2123a

Browse files
Java:新增兼容 Oracle DATETIME, TIMESTAMP 等日期时间类型的值来写库的示例代码
1 parent a1c8be5 commit df2123a

File tree

3 files changed

+107
-17
lines changed

3 files changed

+107
-17
lines changed

‎APIJSON-Java-Server/APIJSONBoot-MultiDataSource/src/main/java/apijson/demo/DemoSQLConfig.java‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,5 +270,25 @@ public String getDBPassword() {
270270
// public String getKey(String key) {
271271
// return super.getKey(ColumnUtil.compatInputKey(key, getTable(), getMethod()));
272272
// }
273-
273+
274+
// 取消注释来兼容 Oracle DATETIME, TIMESTAMP 等日期时间类型的值来写库
275+
// public Object getValue(@NotNull Object value) {
276+
// if (isOracle() && RequestMethod.isQueryMethod(getMethod()) == false && value instanceof String) {
277+
// try {
278+
// SimpleDateFormat parser = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
279+
// parser.parse((String) value);
280+
// if (isPrepared()) {
281+
// preparedValueList.add(value);
282+
// }
283+
// return "to_date(" + (isPrepared() ? "?" : getSQLValue(value)) + ",'yyyy-mm-dd hh24:mi:ss')";
284+
// }
285+
// catch (Throwable e) {
286+
// if (Log.DEBUG) {
287+
// e.printStackTrace();
288+
// }
289+
// }
290+
// }
291+
// return super.getValue(value);
292+
// }
293+
274294
}

‎APIJSON-Java-Server/APIJSONBoot/src/main/java/apijson/demo/DemoSQLConfig.java‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,4 +271,24 @@ public String getDBPassword() {
271271
// return super.getKey(ColumnUtil.compatInputKey(key, getTable(), getMethod()));
272272
// }
273273

274+
// 取消注释来兼容 Oracle DATETIME, TIMESTAMP 等日期时间类型的值来写库
275+
// public Object getValue(@NotNull Object value) {
276+
// if (isOracle() && RequestMethod.isQueryMethod(getMethod()) == false && value instanceof String) {
277+
// try {
278+
// SimpleDateFormat parser = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
279+
// parser.parse((String) value);
280+
// if (isPrepared()) {
281+
// preparedValueList.add(value);
282+
// }
283+
// return "to_date(" + (isPrepared() ? "?" : getSQLValue(value)) + ",'yyyy-mm-dd hh24:mi:ss')";
284+
// }
285+
// catch (Throwable e) {
286+
// if (Log.DEBUG) {
287+
// e.printStackTrace();
288+
// }
289+
// }
290+
// }
291+
// return super.getValue(value);
292+
// }
293+
274294
}

‎APIJSON-Java-Server/APIJSONFinal/src/main/java/apijson/demo/DemoSQLConfig.java‎

Lines changed: 66 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import apijson.orm.AbstractSQLConfig;
2727

2828

29-
/**SQL配置
29+
/**SQL 配置
3030
* TiDB 用法和 MySQL 一致
3131
* @author Lemon
3232
*/
@@ -40,7 +40,7 @@ public DemoSQLConfig(RequestMethod method, String table) {
4040
}
4141

4242
static {
43-
DEFAULT_DATABASE = DATABASE_MYSQL; //TODO 默认数据库类型,改成你自己的
43+
DEFAULT_DATABASE = DATABASE_MYSQL; //TODO 默认数据库类型,改成你自己的。TiDB, MariaDB, OceanBase 这类兼容 MySQL 的可当做 MySQL 使用
4444
DEFAULT_SCHEMA = "sys"; //TODO 默认数据库名/模式,改成你自己的,默认情况是 MySQL: sys, PostgreSQL: public, SQL Server: dbo, Oracle:
4545

4646
//表名和数据库不一致的,需要配置映射关系。只使用 APIJSONORM 时才需要;
@@ -63,7 +63,7 @@ public AbstractSQLConfig getSQLConfig(RequestMethod method, String database, Str
6363

6464
//取消注释来实现自定义各个表的主键名
6565
// @Override
66-
// public String getIdKey(String database, String schema, String table) {
66+
// public String getIdKey(String database, String schema, String datasource, String table) {
6767
// return StringUtil.firstCase(table + "Id"); // userId, comemntId ...
6868
// // return StringUtil.toLowerCase(t) + "_id"; // user_id, comemnt_id ...
6969
// // return StringUtil.toUpperCase(t) + "_ID"; // USER_ID, COMMENT_ID ...
@@ -76,19 +76,32 @@ public String getUserIdKey(String database, String schema, String datasource, St
7676

7777
//取消注释来实现数据库自增 id
7878
// @Override
79-
// public Object newId(RequestMethod method, String database, String schema, String table) {
79+
// public Object newId(RequestMethod method, String database, String schema, String datasource, String table) {
8080
// return null; // return null 则不生成 id,一般用于数据库自增 id
8181
// }
82-
83-
// @Override
84-
// public void onMissingKey4Combine(String name, JSONObject request, String combine, String item, String key) throws Exception {
85-
//// super.onMissingKey4Combine(name, request, combine, item, key);
86-
// }
82+
83+
// @Override
84+
// public void onMissingKey4Combine(String name, JSONObject request, String combine, String item, String key) throws Exception {
85+
//// super.onMissingKey4Combine(name, request, combine, item, key);
86+
// }
8787
};
8888

89+
// 自定义原始 SQL 片段,其它功能满足不了时才用它,只有 RAW_MAP 配置了的 key 才允许前端传
90+
RAW_MAP.put("`to`.`id`", ""); // 空字符串 "" 表示用 key 的值 `to`.`id`
91+
RAW_MAP.put("to.momentId", "`to`.`momentId`"); // 最终以 `to`.`userId` 拼接 SQL,相比以上写法可以让前端写起来更简单
92+
RAW_MAP.put("(`Comment`.`userId`=`to`.`userId`)", ""); // 已经是一个条件表达式了,用 () 包裹是为了避免 JSON 中的 key 拼接在前面导致 SQL 出错
93+
RAW_MAP.put("sum(if(userId%2=0,1,0))", ""); // 超过单个函数的 SQL 表达式
94+
RAW_MAP.put("sumUserIdIsEven", "sum(if(`userId`%2=0,1,0)) AS sumUserIdIsEven"); // 简化前端传参
95+
RAW_MAP.put("substring_index(substring_index(content,',',1),',',-1)", ""); // APIAuto 不支持 ',可以用 Postman 测
96+
RAW_MAP.put("substring_index(substring_index(content,'.',1),'.',-1) AS subContent", ""); // APIAuto 不支持 ',可以用 Postman 测
97+
RAW_MAP.put("commentWhereItem1","(`Comment`.`userId` = 38710 AND `Comment`.`momentId` = 470)");
98+
RAW_MAP.put("to_days(now())-to_days(`date`)<=7",""); // 给 @having 使用
99+
89100
}
90101

91102

103+
// 如果 DemoSQLExecutor.getConnection 能拿到连接池的有效 Connection,则这里不需要配置 dbVersion, dbUri, dbAccount, dbPassword
104+
92105
@Override
93106
public String getDBVersion() {
94107
if (isMySQL()) {
@@ -108,15 +121,17 @@ public String getDBVersion() {
108121
}
109122
return null;
110123
}
111-
124+
112125
@JSONField(serialize = false) // 不在日志打印 账号/密码 等敏感信息,用了 UnitAuto 则一定要加
113126
@Override
114127
public String getDBUri() {
115128
if (isMySQL()) {
116-
return "jdbc:mysql://localhost:3306"; //TODO 改成你自己的,TiDB 可以当成 MySQL 使用,默认端口为 4000
129+
// 这个是 MySQL 8.0 及以上,要加 userSSL=false return "jdbc:mysql://localhost:3306?userSSL=false&serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=UTF-8";
130+
// 以下是 MySQL 5.7 及以下
131+
return "jdbc:mysql://localhost:3306?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=UTF-8"; //TODO 改成你自己的,TiDB 可以当成 MySQL 使用,默认端口为 4000
117132
}
118133
if (isPostgreSQL()) {
119-
return "jdbc:postgresql://localhost:5432/postgres"; //TODO 改成你自己的
134+
return "jdbc:postgresql://localhost:5432/postgres?stringtype=unspecified"; //TODO 改成你自己的
120135
}
121136
if (isSQLServer()) {
122137
return "jdbc:jtds:sqlserver://localhost:1433/pubs;instance=SQLEXPRESS"; //TODO 改成你自己的
@@ -129,7 +144,7 @@ public String getDBUri() {
129144
}
130145
return null;
131146
}
132-
147+
133148
@JSONField(serialize = false) // 不在日志打印 账号/密码 等敏感信息,用了 UnitAuto 则一定要加
134149
@Override
135150
public String getDBAccount() {
@@ -150,7 +165,7 @@ public String getDBAccount() {
150165
}
151166
return null;
152167
}
153-
168+
154169
@JSONField(serialize = false) // 不在日志打印 账号/密码 等敏感信息,用了 UnitAuto 则一定要加
155170
@Override
156171
public String getDBPassword() {
@@ -171,8 +186,12 @@ public String getDBPassword() {
171186
}
172187
return null;
173188
}
174-
175-
//取消注释后,默认的 APIJSON 配置表会由业务表所在数据库模式 schema 改为自定义的
189+
190+
//取消注释后,默认的 APIJSON 配置表会由业务表所在 数据库类型 database 和 数据库模式 schema 改为自定义的
191+
// @Override
192+
// public String getConfigDatabase() {
193+
// return DATABASE_POSTGRESQL;
194+
// }
176195
// @Override
177196
// public String getConfigSchema() {
178197
// return "apijson";
@@ -207,4 +226,35 @@ public String getDBPassword() {
207226
// return false;
208227
// }
209228

229+
230+
// 取消注释支持 !key 反选字段 和 字段名映射,需要先依赖插件 https://github.com/APIJSON/apijson-column
231+
// @Override
232+
// public AbstractSQLConfig setColumn(List<String> column) {
233+
// return super.setColumn(ColumnUtil.compatInputColumn(column, getTable(), getMethod()));
234+
// }
235+
// @Override
236+
// public String getKey(String key) {
237+
// return super.getKey(ColumnUtil.compatInputKey(key, getTable(), getMethod()));
238+
// }
239+
240+
// 取消注释来兼容 Oracle DATETIME, TIMESTAMP 等日期时间类型的值来写库
241+
// public Object getValue(@NotNull Object value) {
242+
// if (isOracle() && RequestMethod.isQueryMethod(getMethod()) == false && value instanceof String) {
243+
// try {
244+
// SimpleDateFormat parser = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
245+
// parser.parse((String) value);
246+
// if (isPrepared()) {
247+
// preparedValueList.add(value);
248+
// }
249+
// return "to_date(" + (isPrepared() ? "?" : getSQLValue(value)) + ",'yyyy-mm-dd hh24:mi:ss')";
250+
// }
251+
// catch (Throwable e) {
252+
// if (Log.DEBUG) {
253+
// e.printStackTrace();
254+
// }
255+
// }
256+
// }
257+
// return super.getValue(value);
258+
// }
259+
210260
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /