@@ -25,11 +25,16 @@ INSERT INTO "student" VALUES (1, '沈', '一年级');
25
25
添加依赖
26
26
27
27
```
28
-
28
+ <!--https://central.sonatype.com/artifact/com.litongjava/table-to-json-->
29
+ <dependency>
30
+ <groupId>com.litongjava</groupId>
31
+ <artifactId>table-to-json</artifactId>
32
+ <version>1.2.7</version>
33
+ </dependency>
29
34
<dependency>
30
- <groupId>com.zaxxer </groupId>
31
- <artifactId>HikariCP </artifactId>
32
- <version>4.0.3 </version>
35
+ <groupId>com.alibaba </groupId>
36
+ <artifactId>druid </artifactId>
37
+ <version>1.1.23 </version>
33
38
</dependency>
34
39
35
40
<!-- sqlite-jdbc -->
@@ -38,26 +43,161 @@ INSERT INTO "student" VALUES (1, '沈', '一年级');
38
43
<artifactId>sqlite-jdbc</artifactId>
39
44
<version>3.7.2</version>
40
45
</dependency>
41
-
42
- <dependency>
43
- <groupId>com.litongjava</groupId>
44
- <artifactId>table-to-json</artifactId>
45
- <version>1.2.4</version>
46
- </dependency>
47
46
```
48
47
49
48
添加配置文件 app.properties
50
49
51
50
```
52
51
#jdbc-sqlliste
52
+ jdbc.driverClass=org.sqlite.JDBC
53
53
jdbc.url=jdbc:sqlite:D:/sqllite/student.db
54
54
jdbc.user=
55
55
jdbc.pswd=
56
56
jdbc.showSql=true
57
+ jdbc.validationQuery=select 1
57
58
```
58
59
59
60
添加配置类
60
61
62
+ ```
63
+ package com.litongjava.tio.boot.sqllite.config;
64
+
65
+ import javax.sql.DataSource;
66
+
67
+ import com.alibaba.druid.pool.DruidDataSource;
68
+ import com.jfinal.template.Engine;
69
+ import com.jfinal.template.source.ClassPathSourceFactory;
70
+ import com.litongjava.jfinal.aop.annotation.AConfiguration;
71
+ import com.litongjava.jfinal.aop.annotation.AInitialization;
72
+ import com.litongjava.jfinal.plugin.activerecord.ActiveRecordPlugin;
73
+ import com.litongjava.jfinal.plugin.activerecord.OrderedFieldContainerFactory;
74
+ import com.litongjava.jfinal.plugin.activerecord.dialect.Sqlite3Dialect;
75
+ import com.litongjava.jfinal.plugin.hikaricp.DsContainer;
76
+ import com.litongjava.tio.boot.constatns.ConfigKeys;
77
+ import com.litongjava.tio.boot.server.TioBootServer;
78
+ import com.litongjava.tio.utils.environment.EnvironmentUtils;
79
+
80
+ @AConfiguration
81
+ public class TableToJsonConfig {
82
+
83
+ /**
84
+ * create datasource
85
+ * @return
86
+ */
87
+ @AInitialization(priority = 10)
88
+ public DataSource dataSource() {
89
+ // get parameter from config file
90
+ String jdbcUrl = EnvironmentUtils.get("jdbc.url");
91
+ String jdbcUser = EnvironmentUtils.get("jdbc.user");
92
+ String jdbcPswd = EnvironmentUtils.get("jdbc.pswd");
93
+ String jdbcValidationQuery = EnvironmentUtils.get("jdbc.validationQuery");
94
+
95
+ // create datasource
96
+ DruidDataSource druidDataSource = new DruidDataSource();
97
+
98
+ // set basic parameter
99
+ druidDataSource.setUrl(jdbcUrl);
100
+ druidDataSource.setUsername(jdbcUser);
101
+ druidDataSource.setPassword(jdbcPswd);
102
+ druidDataSource.setValidationQuery(jdbcValidationQuery);
103
+ // save datasource
104
+ DsContainer.setDataSource(druidDataSource);
105
+ // close datasource while server close
106
+ TioBootServer.me().addDestroyMethod(druidDataSource::close);
107
+ return druidDataSource;
108
+ }
109
+
110
+ /**
111
+ * create ActiveRecordPlugin
112
+ * @return
113
+ * @throws Exception
114
+ */
115
+ @AInitialization
116
+ public ActiveRecordPlugin activeRecordPlugin() throws Exception {
117
+ // get datasource from DsContainer
118
+ DataSource dataSource = DsContainer.ds;
119
+
120
+ // get parameter from config file
121
+ Boolean tioDevMode = EnvironmentUtils.getBoolean(ConfigKeys.TIO_DEV_MODE, false);
122
+ boolean jdbcShowSql = EnvironmentUtils.getBoolean("jdbc.showSql", false);
123
+ // cretae plugin
124
+ ActiveRecordPlugin arp = new ActiveRecordPlugin(dataSource);
125
+ // set parameter
126
+ arp.setDialect(new Sqlite3Dialect());
127
+ arp.setContainerFactory(new OrderedFieldContainerFactory());
128
+ arp.setShowSql(jdbcShowSql);
129
+
130
+ if (tioDevMode) {
131
+ arp.setDevMode(true);
132
+ }
133
+
134
+ // config engine
135
+ Engine engine = arp.getEngine();
136
+ engine.setSourceFactory(new ClassPathSourceFactory());
137
+ engine.setCompressorOn(' ');
138
+ engine.setCompressorOn('\n');
139
+ // arp.addSqlTemplate("/sql/all_sqls.sql");
140
+ // start plugin
141
+ arp.start();
142
+ // close plugin while server close
143
+ TioBootServer.me().addDestroyMethod(arp::stop);
144
+ return arp;
145
+ }
146
+ }
147
+ ```
148
+
149
+ 添加启动类
150
+
151
+ ```
152
+ package com.litongjava.tio.boot.sqllite;
153
+
154
+ import com.litongjava.hotswap.wrapper.tio.boot.TioApplicationWrapper;
155
+ import com.litongjava.jfinal.aop.annotation.AComponentScan;
156
+
157
+ @AComponentScan
158
+ public class TioBootSqlLiteApp {
159
+
160
+ public static void main(String[] args) {
161
+ long start = System.currentTimeMillis();
162
+ TioApplicationWrapper.run(TioBootSqlLiteApp.class, args);
163
+ long end = System.currentTimeMillis();
164
+ System.out.println((end - start) + "ms");
165
+ }
166
+
167
+ }
168
+ ```
169
+
61
170
添加 Controller
62
171
63
- 启动测试
172
+ ```
173
+ package com.litongjava.tio.boot.sqllite.controller;
174
+
175
+ import java.util.List;
176
+
177
+ import com.litongjava.data.model.DbJsonBean;
178
+ import com.litongjava.data.services.DbJsonService;
179
+ import com.litongjava.jfinal.aop.annotation.AAutowired;
180
+ import com.litongjava.jfinal.plugin.activerecord.Record;
181
+ import com.litongjava.tio.http.server.annotation.RequestPath;
182
+
183
+ @RequestPath()
184
+ public class TestController {
185
+
186
+ @AAutowired
187
+ private DbJsonService dbJsonService;
188
+
189
+ @RequestPath("/list")
190
+ public DbJsonBean<List<Record>> list() {
191
+ DbJsonBean<List<Record>> listAll = dbJsonService.listAll("student");
192
+ return listAll;
193
+ }
194
+ }
195
+ ```
196
+
197
+ 访问测试
198
+
199
+ - http://localhost/list
200
+
201
+ 测试代码
202
+
203
+ - https://github.com/litongjava/java-ee-tio-boot-study/tree/main/tio-boot-latest-study/tio-boot-sqllite-demo
0 commit comments