@@ -9933,13 +9933,13 @@ public class Stable5Controller {
9933
9933
- http: // localhost/stable4/list
9934
9934
- http: // localhost/stable5/list
9935
9935
9936
- ### 多数据源 mysql and tdengine
9936
+ ## table - json 多数据源 mysql and tdengine
9937
9937
9938
9938
如何在 tio- boot 应用程序中配置多个数据源,这里以 MySQL 和 TDengine 为例。
9939
9939
9940
9940
#### ActiveRecordPluginConfiguration 类
9941
9941
9942
- ···
9942
+ ```java
9943
9943
import javax.sql. DataSource ;
9944
9944
9945
9945
import com.jfinal.template. Engine ;
@@ -9949,6 +9949,8 @@ import com.litongjava.jfinal.aop.annotation.AConfiguration;
9949
9949
import com.litongjava.jfinal.aop.annotation. AInitialization ;
9950
9950
import com.litongjava.jfinal.plugin.activerecord. ActiveRecordPlugin ;
9951
9951
import com.litongjava.jfinal.plugin.activerecord. OrderedFieldContainerFactory ;
9952
+ import com.litongjava.jfinal.plugin.activerecord.dialect. TdEngineDialect ;
9953
+ import com.litongjava.jfinal.plugin.hikaricp. DsContainer ;
9952
9954
import com.litongjava.tio.boot.constatns. ConfigKeys ;
9953
9955
import com.litongjava.tio.boot.server. TioBootServer ;
9954
9956
import com.litongjava.tio.utils.environment. EnvironmentUtils ;
@@ -9958,23 +9960,18 @@ import com.zaxxer.hikari.HikariDataSource;
9958
9960
@AConfiguration
9959
9961
public class ActiveRecordPluginConfiguration {
9960
9962
9961
- / \*
9962
-
9963
- -
9964
- - config ActiveRecordPlugin
9965
- \*/
9966
9963
@AInitialization
9967
9964
public void activeRecordPlugin () throws Exception {
9968
- configArpForMysql();
9969
- configArpForTdEngine();
9965
+ configArpForMysql();
9966
+ configArpForTdEngine();
9970
9967
9971
- }
9968
+ }
9972
9969
9973
- public DataSource getMysqlDataSource () {
9974
- String jdbcUrl = EnvironmentUtils . get(" jdbc.url" );
9975
- String jdbcUser = EnvironmentUtils . get(" jdbc.user" );
9976
- String jdbcPswd = EnvironmentUtils . get(" jdbc.pswd" );
9977
- int maximumPoolSize = EnvironmentUtils . getInt(" jdbc.MaximumPoolSize" , 2 );
9970
+ public DataSource getMysqlDataSource () {
9971
+ String jdbcUrl = EnvironmentUtils . get(" jdbc.url" );
9972
+ String jdbcUser = EnvironmentUtils . get(" jdbc.user" );
9973
+ String jdbcPswd = EnvironmentUtils . get(" jdbc.pswd" );
9974
+ int maximumPoolSize = EnvironmentUtils . getInt(" jdbc.MaximumPoolSize" , 2 );
9978
9975
9979
9976
HikariConfig config = new HikariConfig ();
9980
9977
// 设定基本参数
@@ -9986,11 +9983,10 @@ int maximumPoolSize = EnvironmentUtils.getInt("jdbc.MaximumPoolSize", 2);
9986
9983
HikariDataSource hikariDataSource = new HikariDataSource (config);
9987
9984
TioBootServer . addDestroyMethod(hikariDataSource:: close);
9988
9985
return hikariDataSource;
9986
+ }
9989
9987
9990
- }
9991
-
9992
- public ActiveRecordPlugin configArpForMysql () {
9993
- String property = EnvironmentUtils . get(ConfigKeys . APP_ENV );
9988
+ public ActiveRecordPlugin configArpForMysql () {
9989
+ String property = EnvironmentUtils . get(ConfigKeys . APP_ENV );
9994
9990
9995
9991
DataSource mysqlDataSource = getMysqlDataSource();
9996
9992
ActiveRecordPlugin mysqlArp = new ActiveRecordPlugin (mysqlDataSource);
@@ -10013,18 +10009,17 @@ String property = EnvironmentUtils.get(ConfigKeys.APP_ENV);
10013
10009
// add
10014
10010
TioRequestParamUtils . types. add(" bigint" );
10015
10011
return mysqlArp;
10012
+ }
10016
10013
10017
- }
10018
-
10019
- public DataSource getTdengineDataSource () {
10020
- HikariConfig config = new HikariConfig ();
10021
- // jdbc properties
10022
- String host = EnvironmentUtils . get(" tdengine.host" );
10023
- int port = EnvironmentUtils . getInt(" tdengine.port" );
10024
- String user = EnvironmentUtils . get(" tdengine.username" );
10025
- String pswd = EnvironmentUtils . get(" tdengine.password" );
10026
- String dbName = EnvironmentUtils . get(" tdengine.database" );
10027
- String driverClassName = EnvironmentUtils . get(" tdengine.driverClassName" );
10014
+ public DataSource getTdengineDataSource () {
10015
+ HikariConfig config = new HikariConfig ();
10016
+ // jdbc properties
10017
+ String host = EnvironmentUtils . get(" tdengine.host" );
10018
+ int port = EnvironmentUtils . getInt(" tdengine.port" );
10019
+ String user = EnvironmentUtils . get(" tdengine.username" );
10020
+ String pswd = EnvironmentUtils . get(" tdengine.password" );
10021
+ String dbName = EnvironmentUtils . get(" tdengine.database" );
10022
+ String driverClassName = EnvironmentUtils . get(" tdengine.driverClassName" );
10028
10023
10029
10024
// String driverClassName = "com.taosdata.jdbc.TSDBDriver";
10030
10025
@@ -10040,23 +10035,25 @@ String driverClassName = EnvironmentUtils.get("tdengine.driverClassName");
10040
10035
config. setConnectionTestQuery(" select server_status()" ); // validation query
10041
10036
10042
10037
HikariDataSource ds = new HikariDataSource (config); // create datasource
10038
+ DsContainer . setDataSource(ds);
10043
10039
TioBootServer . addDestroyMethod(ds:: close);
10044
10040
10045
10041
return ds;
10042
+ }
10046
10043
10047
- }
10048
-
10049
- private String getTdEngineJdbcUrl (String host , int port , String user , String pswd , String dbName ) {
10050
- // 添加 batchfetch=true 属性后得到的 Websocket 连接
10051
- return " jdbc:TAOS-RS://" + host + " :" + port + " /" + dbName + " ?user=" + user + " &password=" + pswd + " &batchfetch=true" ;
10052
- }
10044
+ private String getTdEngineJdbcUrl (String host , int port , String user , String pswd , String dbName ) {
10045
+ // 添加batchfetch=true属性后得到的Websocket连接
10046
+ return " jdbc:TAOS-RS://" + host + " :" + port + " /" + dbName + " ?user=" + user + " &password=" + pswd
10047
+ + " &batchfetch=true" ;
10048
+ }
10053
10049
10054
- public void configArpForTdEngine () {
10055
- String property = EnvironmentUtils . get(ConfigKeys . APP_ENV );
10056
- DataSource tdengineDataSource = getTdengineDataSource();
10057
- // 指定名称为 tdengine
10058
- ActiveRecordPlugin tdengineArp = new ActiveRecordPlugin (" tdengine" , tdengineDataSource);
10059
- tdengineArp. setContainerFactory(new OrderedFieldContainerFactory ());
10050
+ public void configArpForTdEngine () {
10051
+ String property = EnvironmentUtils . get(ConfigKeys . APP_ENV );
10052
+ DataSource tdengineDataSource = getTdengineDataSource();
10053
+ // 指定名称为 tdengine
10054
+ ActiveRecordPlugin tdengineArp = new ActiveRecordPlugin (" tdengine" , tdengineDataSource);
10055
+ tdengineArp. setDialect(new TdEngineDialect ());
10056
+ tdengineArp. setContainerFactory(new OrderedFieldContainerFactory ());
10060
10057
10061
10058
if (" dev" . equals(property)) {
10062
10059
tdengineArp. setDevMode(true );
@@ -10065,10 +10062,9 @@ tdengineArp.setContainerFactory(new OrderedFieldContainerFactory());
10065
10062
10066
10063
tdengineArp. start();
10067
10064
TioBootServer . addDestroyMethod(tdengineArp:: stop);
10068
-
10069
- }
10065
+ }
10070
10066
}
10071
- ···
10067
+ ```
10072
10068
10073
10069
1. ** 数据源配置** :
10074
10070
- `getMysqlDataSource()`: 此方法创建并配置 MySQL 的数据源。使用 HikariCP 作为连接池管理器。
@@ -10086,7 +10082,7 @@ tdengineArp.setContainerFactory(new OrderedFieldContainerFactory());
10086
10082
- 使用 `TioBootTest . before` 方法初始化测试环境。
10087
10083
- `test()` 方法中演示了如何使用 `DbJsonService ` 从 MySQL 查询数据,并使用 `Db . use(" tdengine" )` 从 TDengine 查询数据。
10088
10084
10089
- ···
10085
+ ```java
10090
10086
import org.junit. Before ;
10091
10087
import org.junit. Test ;
10092
10088
@@ -10106,35 +10102,35 @@ import lombok.extern.slf4j.Slf4j;
10106
10102
@Slf4j
10107
10103
public class ActiveRecordPluginConfigurationTest {
10108
10104
10109
- @Before
10110
- public void before () {
10111
- TioBootTest . before(ActiveRecordPluginConfiguration . class);
10112
- }
10105
+ @Before
10106
+ public void before () {
10107
+ TioBootTest . before(ActiveRecordPluginConfiguration . class);
10108
+ }
10113
10109
10114
- @Test
10115
- public void test () {
10116
- DbJsonService dbJsonService = Aop . get(DbJsonService . class);
10117
- // 查询 mysql
10118
- DbJsonBean<Page<Record > > page = dbJsonService. page(" sys_user_info" , Kv . create());
10119
- log. info(" size:{}" , page. getData(). getPageSize());
10120
- // 查询 tdgnine
10121
- DbPro dbPro = Db . use(" tdengine" );
10122
- SqlPara sqlPara = new SqlPara ();
10123
- sqlPara. setSql(" select \* from sensor_data" );
10124
- Page<Record > page2 = dbPro. paginate(1 , 10 , sqlPara);
10125
- log. info(" page size:{}" , page2. getPageSize());
10126
- }
10110
+ @Test
10111
+ public void test () {
10112
+ DbJsonService dbJsonService = Aop . get(DbJsonService . class);
10113
+ // 查询 mysql
10114
+ DbJsonBean<Page<Record > > page = dbJsonService. page(" sys_user_info" , Kv . create());
10115
+ log. info(" size:{}" , page. getData(). getPageSize());
10116
+ // 查询 tdgnine
10117
+ DbPro dbPro = Db . use(" tdengine" );
10118
+ SqlPara sqlPara = new SqlPara ();
10119
+ sqlPara. setSql(" select `*` from sensor_data" );
10120
+ Page<Record > page2 = dbPro. paginate(1 , 10 , sqlPara);
10121
+ log. info(" page size:{}" , page2. getPageSize());
10122
+ }
10127
10123
10128
10124
}
10129
- ···
10125
+ ```
10130
10126
10131
10127
#### 控制器测试类
10132
10128
10133
10129
1. ** PenRawDataController ** :
10134
10130
- 提供了一个 `page()` 方法,用于处理分页请求。
10135
10131
- 通过 `DbJsonService ` 和 `Db . use(" tdengine" )` 从特定的数据源(在这里是 TDengine )查询数据。
10136
10132
10137
- ···
10133
+ ```java
10138
10134
import java.util. Map ;
10139
10135
10140
10136
import com.jfinal.kit. Kv ;
@@ -10156,19 +10152,91 @@ import lombok.extern.slf4j.Slf4j;
10156
10152
@RequestPath (" /pen/raw/data" )
10157
10153
public class PenRawDataController {
10158
10154
10159
- @AAutowired
10160
- private DbJsonService dbJsonService;
10155
+ @AAutowired
10156
+ private DbJsonService dbJsonService;
10161
10157
10162
- @RequestPath (" /page" )
10163
- public DbJsonBean<DbPage<Kv > > page (HttpRequest request ) {
10164
- String f = " pen_raw_data_stable" ;
10165
- Map<String , Object > map = TioRequestParamUtils . getRequestMap(request);
10166
- map. remove(" f" );
10167
- Kv kv = KvUtils . camelToUnderscore(map);
10168
- log. info(" tableName:{},kv:{}" , f, kv);
10169
- DbPro dbPro = Db . use(" tdengine" );
10170
- return DbJsonBeanUtils . pageToDbPage(dbJsonService. page(dbPro, f, kv));
10158
+ @RequestPath (" /page" )
10159
+ public DbJsonBean<DbPage<Kv > > page (HttpRequest request ) {
10160
+ String f = " pen_raw_data_stable" ;
10161
+ Map<String , Object > map = TioRequestParamUtils . getRequestMap(request);
10162
+ map. remove(" f" );
10163
+ Kv kv = KvUtils . camelToUnderscore(map);
10164
+ log. info(" tableName:{},kv:{}" , f, kv);
10165
+ DbPro dbPro = Db . use(" tdengine" );
10166
+ return DbJsonBeanUtils . pageToDbPage(dbJsonService. page(dbPro, f, kv));
10167
+ }
10171
10168
}
10169
+ ```
10170
+
10171
+ #### 测试参数查询
10172
+
10173
+ ```java
10174
+ import java.sql. Connection ;
10175
+ import java.sql. PreparedStatement ;
10176
+ import java.sql. ResultSet ;
10177
+ import java.sql. SQLException ;
10178
+ import java.util. List ;
10179
+
10180
+ import org.junit. Before ;
10181
+ import org.junit. Test ;
10182
+
10183
+ import com.jfinal.kit. Kv ;
10184
+ import com.litongjava.data.model. DbJsonBean ;
10185
+ import com.litongjava.data.services. DbJsonService ;
10186
+ import com.litongjava.jfinal.aop. Aop ;
10187
+ import com.litongjava.jfinal.plugin.activerecord. Config ;
10188
+ import com.litongjava.jfinal.plugin.activerecord. Db ;
10189
+ import com.litongjava.jfinal.plugin.activerecord. DbPro ;
10190
+ import com.litongjava.jfinal.plugin.activerecord. Record ;
10191
+ import com.litongjava.jfinal.plugin.activerecord. RecordBuilder ;
10192
+ import com.litongjava.jfinal.plugin.hikaricp. DsContainer ;
10193
+ import com.litongjava.tio.boot.tesing. TioBootTest ;
10194
+
10195
+ import lombok. Cleanup ;
10196
+
10197
+ public class PenRawDataControllerTest {
10198
+
10199
+ @Before
10200
+ public void before () {
10201
+ TioBootTest . before(ActiveRecordPluginConfiguration . class);
10202
+ }
10203
+
10204
+ @Test
10205
+ public void query () throws SQLException {
10206
+ String sql = " select payload_ts,data_ts,client_id,user_id from manliang_pen.pen_page_stable where user_id=?" ;
10207
+ String userId = " 18374686479671623681" ;
10208
+ @Cleanup
10209
+ Connection conn = DsContainer . ds. getConnection();
10210
+ Config config = Db . use(" tdengine" ). getConfig();
10211
+
10212
+ List<Record > list = null ;
10213
+ try (PreparedStatement pstmt = conn. prepareStatement(sql)) {
10214
+ pstmt. setString(1 , userId);
10215
+ ResultSet executeQuery = pstmt. executeQuery();
10216
+ list = RecordBuilder . me. build(config, executeQuery);
10217
+ }
10218
+ System . out. println(list);
10219
+ }
10220
+
10221
+ @Test
10222
+ public void queryWithDb () {
10223
+ String sql = " select payload_ts,data_ts,client_id,user_id from manliang_pen.pen_page_stable where user_id=?" ;
10224
+ String userId = " 18374686479671623681" ;
10225
+ List<Record > find = Db . use(" tdengine" ). find(sql, userId);
10226
+ System . out. println(find);
10227
+ }
10228
+
10229
+ @Test
10230
+ public void queryWithDbJsonService () {
10231
+ DbJsonService dbJsonService = Aop . get(DbJsonService . class);
10232
+ Kv kv = Kv . create();
10233
+ kv. set(" table_name" , " manliang_pen.pen_page_stable" );
10234
+ kv. set(" columns" , " payload_ts,data_ts,client_id,user_id" );
10235
+ kv. set(" user_id" , " 18374686479671623681" );
10236
+ DbPro dbPro = Db . use(" tdengine" );
10237
+ DbJsonBean<List<Record > > dbJsonBean = dbJsonService. list(dbPro, kv);
10238
+ System . out. println(dbJsonBean);
10239
+ }
10172
10240
}
10173
10241
···
10174
10242
@@ -10618,7 +10686,7 @@ public class DbTestController {
10618
10686
10619
10687
使用 Junit 对连接数据库的部分进行单元测试
10620
10688
10621
- ```
10689
+ ```java
10622
10690
import java.util. List ;
10623
10691
10624
10692
import com.litongjava.jfinal.aop. Aop ;
0 commit comments