@@ -227,6 +227,103 @@ public class TableToJsonConfig {
227
227
228
228
```
229
229
230
+ #### 解析 DSN
231
+
232
+ 配置 dsn
233
+
234
+ ```
235
+ DATABASE_DSN=postgresql://postgres:[email protected] /student
236
+ ```
237
+
238
+ 解析并配置
239
+
240
+ ``` java
241
+ import javax.sql.DataSource ;
242
+
243
+ import com.jfinal.template.Engine ;
244
+ import com.jfinal.template.source.ClassPathSourceFactory ;
245
+ import com.litongjava.jfinal.aop.annotation.AConfiguration ;
246
+ import com.litongjava.jfinal.aop.annotation.AInitialization ;
247
+ import com.litongjava.jfinal.plugin.activerecord.ActiveRecordPlugin ;
248
+ import com.litongjava.jfinal.plugin.activerecord.OrderedFieldContainerFactory ;
249
+ import com.litongjava.jfinal.plugin.activerecord.dialect.PostgreSqlDialect ;
250
+ import com.litongjava.jfinal.plugin.hikaricp.DsContainer ;
251
+ import com.litongjava.tio.boot.constatns.TioBootConfigKeys ;
252
+ import com.litongjava.tio.boot.server.TioBootServer ;
253
+ import com.litongjava.tio.utils.dsn.DSNParser ;
254
+ import com.litongjava.tio.utils.dsn.JdbcInfo ;
255
+ import com.litongjava.tio.utils.environment.EnvironmentUtils ;
256
+ import com.zaxxer.hikari.HikariConfig ;
257
+ import com.zaxxer.hikari.HikariDataSource ;
258
+
259
+ @AConfiguration
260
+ public class TableToJsonConfig {
261
+
262
+ public DataSource dataSource () {
263
+ String dsn = EnvironmentUtils . get(" DATABASE_DSN" );
264
+ if (dsn == null ) {
265
+ return null ;
266
+ }
267
+
268
+ JdbcInfo jdbc = new DSNParser (). parse(dsn);
269
+
270
+ int maximumPoolSize = EnvironmentUtils . getInt(" jdbc.MaximumPoolSize" , 2 );
271
+
272
+ HikariConfig config = new HikariConfig ();
273
+ // config
274
+ config. setJdbcUrl(jdbc. getUrl());
275
+ config. setUsername(jdbc. getUser());
276
+ config. setPassword(jdbc. getPswd());
277
+ config. setMaximumPoolSize(maximumPoolSize);
278
+
279
+ HikariDataSource hikariDataSource = new HikariDataSource (config);
280
+
281
+ // set datasource
282
+ DsContainer . setDataSource(hikariDataSource);
283
+ // add destroy
284
+ TioBootServer . me(). addDestroyMethod(hikariDataSource:: close);
285
+ return hikariDataSource;
286
+ }
287
+ /*
288
+ *
289
+ * config ActiveRecordPlugin
290
+ */
291
+
292
+ @AInitialization
293
+ public void activeRecordPlugin () {
294
+ // get dataSource
295
+ DataSource dataSource = dataSource();
296
+ if (dataSource == null ) {
297
+ return ;
298
+ }
299
+ // get env key
300
+ String property = EnvironmentUtils . get(TioBootConfigKeys . APP_ENV );
301
+
302
+ // create arp
303
+ ActiveRecordPlugin arp = new ActiveRecordPlugin (dataSource);
304
+ arp. setContainerFactory(new OrderedFieldContainerFactory ());
305
+ if (" dev" . equals(property)) {
306
+ arp. setDevMode(true );
307
+ }
308
+
309
+ arp. setDialect(new PostgreSqlDialect ());
310
+
311
+ // config engine
312
+ Engine engine = arp. getEngine();
313
+ engine. setSourceFactory(new ClassPathSourceFactory ());
314
+ engine. setCompressorOn(' ' );
315
+ engine. setCompressorOn(' \n ' );
316
+ // add sql file
317
+ // arp.addSqlTemplate("/sql/all_sqls.sql");
318
+ // start
319
+ arp. start();
320
+ // add stop
321
+ TioBootServer . me(). addDestroyMethod(arp:: stop);
322
+ }
323
+ }
324
+
325
+ ```
326
+
230
327
#### 编写 Controller
231
328
232
329
查询 student 表中的所有数据,代码如下
0 commit comments