Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
taosdata
TDengine
提交
8c677755
T
TDengine
项目概览
taosdata
/
TDengine
大约 1 年 前同步成功
通知
1185
Star
22015
Fork
4786
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
T
TDengine
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
8c677755
编写于
9月 10, 2020
作者:
Z
zyyang-taosdata
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
update the jdbcTemplate example and test cases
上级
aa2ab56d
变更
11
显示空白变更内容
内联
并排
Showing
11 changed file
with
359 addition
and
125 deletion
+359
-125
tests/examples/JDBC/SpringJdbcTemplate/pom.xml
tests/examples/JDBC/SpringJdbcTemplate/pom.xml
+77
-71
tests/examples/JDBC/SpringJdbcTemplate/src/main/java/com/taosdata/jdbc/App.java
...ringJdbcTemplate/src/main/java/com/taosdata/jdbc/App.java
+0
-44
tests/examples/JDBC/SpringJdbcTemplate/src/main/java/com/taosdata/jdbc/example/jdbcTemplate/App.java
...main/java/com/taosdata/jdbc/example/jdbcTemplate/App.java
+48
-0
tests/examples/JDBC/SpringJdbcTemplate/src/main/java/com/taosdata/jdbc/example/jdbcTemplate/dao/ExecuteAsStatement.java
...ata/jdbc/example/jdbcTemplate/dao/ExecuteAsStatement.java
+6
-0
tests/examples/JDBC/SpringJdbcTemplate/src/main/java/com/taosdata/jdbc/example/jdbcTemplate/dao/WeatherDao.java
...om/taosdata/jdbc/example/jdbcTemplate/dao/WeatherDao.java
+17
-0
tests/examples/JDBC/SpringJdbcTemplate/src/main/java/com/taosdata/jdbc/example/jdbcTemplate/dao/impl/ExecuteAsStatementImpl.java
...example/jdbcTemplate/dao/impl/ExecuteAsStatementImpl.java
+19
-0
tests/examples/JDBC/SpringJdbcTemplate/src/main/java/com/taosdata/jdbc/example/jdbcTemplate/dao/impl/WeatherDaoImpl.java
...ta/jdbc/example/jdbcTemplate/dao/impl/WeatherDaoImpl.java
+64
-0
tests/examples/JDBC/SpringJdbcTemplate/src/main/java/com/taosdata/jdbc/example/jdbcTemplate/domain/Weather.java
...om/taosdata/jdbc/example/jdbcTemplate/domain/Weather.java
+54
-0
tests/examples/JDBC/SpringJdbcTemplate/src/main/resources/applicationContext.xml
...ingJdbcTemplate/src/main/resources/applicationContext.xml
+6
-5
tests/examples/JDBC/SpringJdbcTemplate/src/test/java/com/taosdata/jdbc/AppTest.java
...JdbcTemplate/src/test/java/com/taosdata/jdbc/AppTest.java
+3
-5
tests/examples/JDBC/SpringJdbcTemplate/src/test/java/com/taosdata/jdbc/example/jdbcTemplate/BatcherInsertTest.java
...taosdata/jdbc/example/jdbcTemplate/BatcherInsertTest.java
+65
-0
未找到文件。
tests/examples/JDBC/SpringJdbcTemplate/pom.xml
浏览文件 @
8c677755
...
...
@@ -22,26 +22,32 @@
<dependency>
<groupId>
org.springframework
</groupId>
<artifactId>
spring-context
</artifactId>
<version>
4.3.2
.RELEASE
</version>
<version>
5.2.8
.RELEASE
</version>
</dependency>
<dependency>
<groupId>
org.springframework
</groupId>
<artifactId>
spring-jdbc
</artifactId>
<version>
4.3.2.RELEASE
</version>
<version>
5.1.9.RELEASE
</version>
</dependency>
<dependency>
<groupId>
org.springframework
</groupId>
<artifactId>
spring-test
</artifactId>
<version>
5.1.9.RELEASE
</version>
</dependency>
<dependency>
<groupId>
junit
</groupId>
<artifactId>
junit
</artifactId>
<version>
4.11
</version>
<version>
4.13
</version>
<scope>
test
</scope>
</dependency>
<dependency>
<groupId>
com.taosdata.jdbc
</groupId>
<artifactId>
taos-jdbcdriver
</artifactId>
<version>
2.0.2
</version>
<version>
2.0.4
</version>
</dependency>
</dependencies>
...
...
@@ -63,7 +69,7 @@
<configuration>
<archive>
<manifest>
<mainClass>
com.taosdata.jdbc
.App
</mainClass>
<mainClass>
com.taosdata.jdbc.example.jdbcTemplate
.App
</mainClass>
</manifest>
</archive>
<descriptorRefs>
...
...
tests/examples/JDBC/SpringJdbcTemplate/src/main/java/com/taosdata/jdbc/App.java
已删除
100644 → 0
浏览文件 @
aa2ab56d
package
com.taosdata.jdbc
;
import
org.springframework.context.ApplicationContext
;
import
org.springframework.context.support.ClassPathXmlApplicationContext
;
import
org.springframework.jdbc.core.JdbcTemplate
;
import
org.springframework.util.CollectionUtils
;
import
java.util.List
;
import
java.util.Map
;
public
class
App
{
public
static
void
main
(
String
[]
args
)
{
ApplicationContext
ctx
=
new
ClassPathXmlApplicationContext
(
"applicationContext.xml"
);
JdbcTemplate
jdbcTemplate
=
(
JdbcTemplate
)
ctx
.
getBean
(
"jdbcTemplate"
);
// create database
jdbcTemplate
.
execute
(
"create database if not exists db "
);
// create table
jdbcTemplate
.
execute
(
"create table if not exists db.tb (ts timestamp, temperature int, humidity float)"
);
String
insertSql
=
"insert into db.tb values(now, 23, 10.3) (now + 1s, 20, 9.3)"
;
// insert rows
int
affectedRows
=
jdbcTemplate
.
update
(
insertSql
);
System
.
out
.
println
(
"insert success "
+
affectedRows
+
" rows."
);
// query for list
List
<
Map
<
String
,
Object
>>
resultList
=
jdbcTemplate
.
queryForList
(
"select * from db.tb"
);
if
(!
CollectionUtils
.
isEmpty
(
resultList
)){
for
(
Map
<
String
,
Object
>
row
:
resultList
){
System
.
out
.
printf
(
"%s, %d, %s\n"
,
row
.
get
(
"ts"
),
row
.
get
(
"temperature"
),
row
.
get
(
"humidity"
));
}
}
}
}
tests/examples/JDBC/SpringJdbcTemplate/src/main/java/com/taosdata/jdbc/example/jdbcTemplate/App.java
0 → 100644
浏览文件 @
8c677755
package
com.taosdata.jdbc.example.jdbcTemplate
;
import
com.taosdata.jdbc.example.jdbcTemplate.dao.ExecuteAsStatement
;
import
com.taosdata.jdbc.example.jdbcTemplate.dao.WeatherDao
;
import
com.taosdata.jdbc.example.jdbcTemplate.domain.Weather
;
import
org.springframework.context.ApplicationContext
;
import
org.springframework.context.support.ClassPathXmlApplicationContext
;
import
java.sql.Timestamp
;
import
java.util.Date
;
import
java.util.List
;
import
java.util.Random
;
public
class
App
{
private
static
Random
random
=
new
Random
(
System
.
currentTimeMillis
());
public
static
void
main
(
String
[]
args
)
{
ApplicationContext
ctx
=
new
ClassPathXmlApplicationContext
(
"applicationContext.xml"
);
ExecuteAsStatement
executor
=
ctx
.
getBean
(
ExecuteAsStatement
.
class
);
// drop database
executor
.
doExecute
(
"drop database if exists test"
);
// create database
executor
.
doExecute
(
"create database if not exists test"
);
//use database
executor
.
doExecute
(
"use test"
);
// create table
executor
.
doExecute
(
"create table if not exists test.weather (ts timestamp, temperature int, humidity float)"
);
WeatherDao
weatherDao
=
ctx
.
getBean
(
WeatherDao
.
class
);
Weather
weather
=
new
Weather
(
new
Timestamp
(
new
Date
().
getTime
()),
random
.
nextFloat
()
*
50.0f
,
random
.
nextInt
(
100
));
// insert rows
int
affectedRows
=
weatherDao
.
add
(
weather
);
System
.
out
.
println
(
"insert success "
+
affectedRows
+
" rows."
);
// query for list
int
limit
=
10
,
offset
=
0
;
List
<
Weather
>
weatherList
=
weatherDao
.
queryForList
(
limit
,
offset
);
for
(
Weather
w
:
weatherList
)
{
System
.
out
.
println
(
w
);
}
}
}
tests/examples/JDBC/SpringJdbcTemplate/src/main/java/com/taosdata/jdbc/example/jdbcTemplate/dao/ExecuteAsStatement.java
0 → 100644
浏览文件 @
8c677755
package
com.taosdata.jdbc.example.jdbcTemplate.dao
;
public
interface
ExecuteAsStatement
{
void
doExecute
(
String
sql
);
}
tests/examples/JDBC/SpringJdbcTemplate/src/main/java/com/taosdata/jdbc/example/jdbcTemplate/dao/WeatherDao.java
0 → 100644
浏览文件 @
8c677755
package
com.taosdata.jdbc.example.jdbcTemplate.dao
;
import
com.taosdata.jdbc.example.jdbcTemplate.domain.Weather
;
import
java.util.List
;
public
interface
WeatherDao
{
int
add
(
Weather
weather
);
int
[]
batchInsert
(
List
<
Weather
>
weatherList
);
List
<
Weather
>
queryForList
(
int
limit
,
int
offset
);
int
count
();
}
tests/examples/JDBC/SpringJdbcTemplate/src/main/java/com/taosdata/jdbc/example/jdbcTemplate/dao/impl/ExecuteAsStatementImpl.java
0 → 100644
浏览文件 @
8c677755
package
com.taosdata.jdbc.example.jdbcTemplate.dao.impl
;
import
com.taosdata.jdbc.example.jdbcTemplate.dao.ExecuteAsStatement
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.jdbc.core.JdbcTemplate
;
import
org.springframework.stereotype.Repository
;
@Repository
public
class
ExecuteAsStatementImpl
implements
ExecuteAsStatement
{
@Autowired
private
JdbcTemplate
jdbcTemplate
;
@Override
public
void
doExecute
(
String
sql
)
{
jdbcTemplate
.
execute
(
sql
);
}
}
tests/examples/JDBC/SpringJdbcTemplate/src/main/java/com/taosdata/jdbc/example/jdbcTemplate/dao/impl/WeatherDaoImpl.java
0 → 100644
浏览文件 @
8c677755
package
com.taosdata.jdbc.example.jdbcTemplate.dao.impl
;
import
com.taosdata.jdbc.example.jdbcTemplate.dao.WeatherDao
;
import
com.taosdata.jdbc.example.jdbcTemplate.domain.Weather
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.jdbc.core.BatchPreparedStatementSetter
;
import
org.springframework.jdbc.core.JdbcTemplate
;
import
org.springframework.jdbc.core.namedparam.SqlParameterSourceUtils
;
import
org.springframework.jdbc.core.simple.SimpleJdbcInsert
;
import
org.springframework.stereotype.Repository
;
import
java.sql.PreparedStatement
;
import
java.sql.SQLException
;
import
java.sql.Timestamp
;
import
java.util.HashMap
;
import
java.util.List
;
import
java.util.Map
;
@Repository
public
class
WeatherDaoImpl
implements
WeatherDao
{
@Autowired
private
JdbcTemplate
jdbcTemplate
;
@Override
public
int
add
(
Weather
weather
)
{
return
jdbcTemplate
.
update
(
"insert into test.weather(ts, temperature, humidity) VALUES(?,?,?)"
,
weather
.
getTs
(),
weather
.
getTemperature
(),
weather
.
getHumidity
()
);
}
@Override
public
int
[]
batchInsert
(
List
<
Weather
>
weatherList
)
{
return
jdbcTemplate
.
batchUpdate
(
"insert into test.weather(ts, temperature, humidity) values( ?, ?, ?)"
,
new
BatchPreparedStatementSetter
()
{
@Override
public
void
setValues
(
PreparedStatement
ps
,
int
i
)
throws
SQLException
{
ps
.
setTimestamp
(
1
,
weatherList
.
get
(
i
).
getTs
());
ps
.
setFloat
(
2
,
weatherList
.
get
(
i
).
getTemperature
());
ps
.
setInt
(
3
,
weatherList
.
get
(
i
).
getHumidity
());
}
@Override
public
int
getBatchSize
()
{
return
weatherList
.
size
();
}
});
}
@Override
public
List
<
Weather
>
queryForList
(
int
limit
,
int
offset
)
{
return
jdbcTemplate
.
query
(
"select * from test.weather limit ? offset ?"
,
(
rs
,
rowNum
)
->
{
Timestamp
ts
=
rs
.
getTimestamp
(
"ts"
);
float
temperature
=
rs
.
getFloat
(
"temperature"
);
int
humidity
=
rs
.
getInt
(
"humidity"
);
return
new
Weather
(
ts
,
temperature
,
humidity
);
},
limit
,
offset
);
}
@Override
public
int
count
()
{
return
jdbcTemplate
.
queryForObject
(
"select count(*) from test.weather"
,
Integer
.
class
);
}
}
tests/examples/JDBC/SpringJdbcTemplate/src/main/java/com/taosdata/jdbc/example/jdbcTemplate/domain/Weather.java
0 → 100644
浏览文件 @
8c677755
package
com.taosdata.jdbc.example.jdbcTemplate.domain
;
import
java.sql.Timestamp
;
public
class
Weather
{
private
Timestamp
ts
;
private
float
temperature
;
private
int
humidity
;
public
Weather
()
{
}
public
Weather
(
Timestamp
ts
,
float
temperature
,
int
humidity
)
{
this
.
ts
=
ts
;
this
.
temperature
=
temperature
;
this
.
humidity
=
humidity
;
}
@Override
public
String
toString
()
{
return
"Weather{"
+
"ts="
+
ts
+
", temperature="
+
temperature
+
", humidity="
+
humidity
+
'}'
;
}
public
Timestamp
getTs
()
{
return
ts
;
}
public
void
setTs
(
Timestamp
ts
)
{
this
.
ts
=
ts
;
}
public
float
getTemperature
()
{
return
temperature
;
}
public
void
setTemperature
(
float
temperature
)
{
this
.
temperature
=
temperature
;
}
public
int
getHumidity
()
{
return
humidity
;
}
public
void
setHumidity
(
int
humidity
)
{
this
.
humidity
=
humidity
;
}
}
tests/examples/JDBC/SpringJdbcTemplate/src/main/resources/applicationContext.xml
浏览文件 @
8c677755
...
...
@@ -5,20 +5,21 @@
xsi:schemaLocation=
"
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
"
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"
default-autowire=
"byName"
>
<bean
id=
"dataSource"
class=
"org.springframework.jdbc.datasource.DriverManagerDataSource"
>
<property
name=
"driverClassName"
value=
"com.taosdata.jdbc.TSDBDriver"
></property>
<property
name=
"url"
value=
"jdbc:TAOS://1
27.0.0.1:6030/log
"
></property>
<property
name=
"url"
value=
"jdbc:TAOS://1
92.168.236.137:6030/
"
></property>
<property
name=
"username"
value=
"root"
></property>
<property
name=
"password"
value=
"taosdata"
></property>
</bean>
<bean
id
=
"jdbcTemplate"
class=
"org.springframework.jdbc.core.JdbcTemplate"
>
<property
name=
"dataSource"
ref
=
"dataSource"
></property>
<bean
id
=
"jdbcTemplate"
class=
"org.springframework.jdbc.core.JdbcTemplate"
>
<property
name=
"dataSource"
ref
=
"dataSource"
></property>
</bean>
<context:component-scan
base-package=
"com.taosdata.jdbc.example.jdbcTemplate"
/>
</beans>
tests/examples/JDBC/SpringJdbcTemplate/src/test/java/com/taosdata/jdbc/AppTest.java
浏览文件 @
8c677755
...
...
@@ -7,14 +7,12 @@ import org.junit.Test;
/**
* Unit test for simple App.
*/
public
class
AppTest
{
public
class
AppTest
{
/**
* Rigorous Test :-)
*/
@Test
public
void
shouldAnswerWithTrue
()
{
assertTrue
(
true
);
public
void
shouldAnswerWithTrue
()
{
assertTrue
(
true
);
}
}
tests/examples/JDBC/SpringJdbcTemplate/src/test/java/com/taosdata/jdbc/example/jdbcTemplate/BatcherInsertTest.java
0 → 100644
浏览文件 @
8c677755
package
com.taosdata.jdbc.example.jdbcTemplate
;
import
com.taosdata.jdbc.example.jdbcTemplate.dao.ExecuteAsStatement
;
import
com.taosdata.jdbc.example.jdbcTemplate.dao.WeatherDao
;
import
com.taosdata.jdbc.example.jdbcTemplate.domain.Weather
;
import
org.junit.Before
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.test.context.ContextConfiguration
;
import
org.springframework.test.context.junit4.SpringJUnit4ClassRunner
;
import
java.sql.Timestamp
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.Random
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
@RunWith
(
SpringJUnit4ClassRunner
.
class
)
@ContextConfiguration
({
"classpath:applicationContext.xml"
})
public
class
BatcherInsertTest
{
@Autowired
private
WeatherDao
weatherDao
;
@Autowired
private
ExecuteAsStatement
executor
;
private
static
final
int
numOfRecordsPerTable
=
1000
;
private
static
long
ts
=
1496732686000
l
;
private
static
Random
random
=
new
Random
(
System
.
currentTimeMillis
());
@Before
public
void
before
()
{
// drop database
executor
.
doExecute
(
"drop database if exists test"
);
// create database
executor
.
doExecute
(
"create database if not exists test"
);
//use database
executor
.
doExecute
(
"use test"
);
// create table
executor
.
doExecute
(
"create table if not exists test.weather (ts timestamp, temperature int, humidity float)"
);
}
@Test
public
void
batchInsert
()
{
List
<
Weather
>
weatherList
=
new
ArrayList
<>();
for
(
int
i
=
0
;
i
<
numOfRecordsPerTable
;
i
++)
{
ts
+=
1000
;
Weather
weather
=
new
Weather
(
new
Timestamp
(
ts
),
random
.
nextFloat
()
*
50.0f
,
random
.
nextInt
(
100
));
weatherList
.
add
(
weather
);
}
long
start
=
System
.
currentTimeMillis
();
weatherDao
.
batchInsert
(
weatherList
);
long
end
=
System
.
currentTimeMillis
();
System
.
out
.
println
(
"batch insert("
+
numOfRecordsPerTable
+
" rows) time cost ==========> "
+
(
end
-
start
)
+
" ms"
);
int
count
=
weatherDao
.
count
();
assertEquals
(
count
,
numOfRecordsPerTable
);
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录