jdbcTemplate
连接
使用springboot jdbcTemplate
连接TDengine
时序数据库
添加TDengine
驱动依赖包
<!--pom.xml-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>com.taosdata.jdbc</groupId>
<artifactId>taos-jdbcdriver</artifactId>
<version>3.5.1</version>
</dependency>
查询代码
// JdbcTemplateConfig.java
@Configuration
public class JdbcTemplateConfig {
@Autowired
private DataSource dataSource;
@Bean
public JdbcTemplate jdbcTemplate() {
return new JdbcTemplate(dataSource);
}
}
// DataService.java
@Service
public class DataService {
private final JdbcTemplate jdbcTemplate;
@Autowired
public DataService(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public List<Map<String, Object>> queryData(String sqlStr) {
return jdbcTemplate.queryForList(sqlStr);
}
}
// TdengineApplicationTests.java 测试类
@SpringBootTest
@Slf4j
class TdengineApplicationTests {
@Autowired
private DataService dataService;
@Test
void contextLoads() {
List<Map<String, Object>> maps1 = dataService.queryData("SELECT AVG(current), MAX(voltage), MIN(phase) FROM meters;");
log.info(maps1.toString());
}
}
druid
连接池
使用druid
连接池连接TDengine时序数据库
添加依赖包
<!--pom.xml-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.6</version>
</dependency>
<dependency>
<groupId>com.taosdata.jdbc</groupId>
<artifactId>taos-jdbcdriver</artifactId>
<version>3.5.1</version>
</dependency>
Restful
、WebSocket
连接配置
spring:
datasource:
url: jdbc:TAOS-RS://localhost:6041/test
#url: jdbc:TAOS-WS://localhost:6041/test
username: root
password: taosdata
driver-class-name: com.taosdata.jdbc.rs.RestfulDriver
#driver-class-name: com.taosdata.jdbc.ws.WebSocketDriver
jpa:
show-sql: true #JPA是否显示sql语句
# 使用mysql的方言,否则报错
database-platform: org.hibernate.dialect.MySQLDialect
原生连接配置
spring:
datasource:
url: jdbc:TAOS://localhost:6030/test
username: root
password: taosdata
driver-class-name: com.taosdata.jdbc.TSDBDriver
jpa:
show-sql: true #JPA是否显示sql语句
# 使用mysql的方言,否则报错
database-platform: org.hibernate.dialect.MySQLDialect
原生连接需要安装TDengine
客户端驱动taosc
否则会报错,为避免客户端驱动和服务端不兼容,使用一致的版本。
客户端驱动下载地址
https://docs.taosdata.com/develop/connect/#安装客户端驱动-taosc
java.lang.UnsatisfiedLinkError: no taos in java.library.path: [\Java\jdk-11.0.2\bin, C:\Windows\Sun\Java\bin, C:\Windows\system32, C:\Windows, C:\Program Files (x86)\Common Files\Oracle\Java\javapath, C:\Windows\system32, C:\Windows, C:\Windows\System32\Wbem, C:\Windows\System32\WindowsPowerShell\v1.0\, C:\Windows\System32\OpenSSH\, D:\javaJDK\bin, D:\maven\apache-maven-3.9.6\bin, D:\Git\Git\cmd, D:\redis, C:\Users\Administrator\AppData\Local\Microsoft\WindowsApps, .]
at java.base/java.lang.ClassLoader.loadLibrary(ClassLoader.java:2660) ~[na:na]
at java.base/java.lang.Runtime.loadLibrary0(Runtime.java:829) ~[na:na]
at java.base/java.lang.System.loadLibrary(System.java:1867) ~[na:na]
at com.taosdata.jdbc.TSDBJNIConnector.<clinit>(TSDBJNIConnector.java:36) ~[taos-jdbcdriver-3.5.1.jar:na]
at com.taosdata.jdbc.TSDBDriver.connect(TSDBDriver.java:176) ~[taos-jdbcdriver-3.5.1.jar:na]
at com.alibaba.druid.pool.DruidAbstractDataSource.createPhysicalConnection(DruidAbstractDataSource.java:1657) ~[druid-1.2.6.jar:1.2.6]
at com.alibaba.druid.pool.DruidAbstractDataSource.createPhysicalConnection(DruidAbstractDataSource.java:1723) ~[druid-1.2.6.jar:1.2.6]
at com.alibaba.druid.pool.DruidDataSource$CreateConnectionThread.run(DruidDataSource.java:2801) ~[druid-1.2.6.jar:1.2.6]
配置好之后可以直接使用正常jpa
的代码,直接对数据进行增删改查。
参考
https://docs.taosdata.com/develop/connect/
https://github.com/taosdata/TDengine/tree/main/docs/examples/JDBC
标签:taosdata,java,TDengine,Spring,boot,druid,jdbc,jdbcTemplate,com From: https://www.cnblogs.com/akapril/p/18663992