diff --git a/tests/examples/JDBC/SpringJdbcTemplate/pom.xml b/tests/examples/JDBC/SpringJdbcTemplate/pom.xml index b796d52d28fbc641800a889fd3f81bc9c62f9147..15aed1cf03b4e10e5a69e16be99ea2320a24609e 100644 --- a/tests/examples/JDBC/SpringJdbcTemplate/pom.xml +++ b/tests/examples/JDBC/SpringJdbcTemplate/pom.xml @@ -1,85 +1,91 @@ - 4.0.0 + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + 4.0.0 - com.taosdata.jdbc - SpringJdbcTemplate - 1.0-SNAPSHOT + com.taosdata.jdbc + SpringJdbcTemplate + 1.0-SNAPSHOT - SpringJdbcTemplate - http://www.taosdata.com + SpringJdbcTemplate + http://www.taosdata.com - - UTF-8 - 1.8 - 1.8 - + + UTF-8 + 1.8 + 1.8 + - + - - org.springframework - spring-context - 4.3.2.RELEASE - + + org.springframework + spring-context + 5.2.8.RELEASE + - - org.springframework - spring-jdbc - 4.3.2.RELEASE - + + org.springframework + spring-jdbc + 5.1.9.RELEASE + - - junit - junit - 4.11 - test - + + org.springframework + spring-test + 5.1.9.RELEASE + - - com.taosdata.jdbc - taos-jdbcdriver - 2.0.2 - + + junit + junit + 4.13 + test + - + + com.taosdata.jdbc + taos-jdbcdriver + 2.0.4 + - - - - maven-compiler-plugin - 3.8.0 - - 1.8 - 1.8 - - - - org.apache.maven.plugins - maven-assembly-plugin - 3.1.0 - - - - com.taosdata.jdbc.App - - - - jar-with-dependencies - - - - - make-assembly - package - - single - - - - - - + + + + + + maven-compiler-plugin + 3.8.0 + + 1.8 + 1.8 + + + + org.apache.maven.plugins + maven-assembly-plugin + 3.1.0 + + + + com.taosdata.jdbc.example.jdbcTemplate.App + + + + jar-with-dependencies + + + + + make-assembly + package + + single + + + + + + diff --git a/tests/examples/JDBC/SpringJdbcTemplate/src/main/java/com/taosdata/jdbc/App.java b/tests/examples/JDBC/SpringJdbcTemplate/src/main/java/com/taosdata/jdbc/App.java deleted file mode 100644 index 3230af46a8016fee3d58c89ea3b2c1ddcf39cea5..0000000000000000000000000000000000000000 --- a/tests/examples/JDBC/SpringJdbcTemplate/src/main/java/com/taosdata/jdbc/App.java +++ /dev/null @@ -1,44 +0,0 @@ -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> resultList = jdbcTemplate.queryForList("select * from db.tb"); - - if(!CollectionUtils.isEmpty(resultList)){ - for (Map row : resultList){ - System.out.printf("%s, %d, %s\n", row.get("ts"), row.get("temperature"), row.get("humidity")); - } - } - - } - -} diff --git a/tests/examples/JDBC/SpringJdbcTemplate/src/main/java/com/taosdata/jdbc/example/jdbcTemplate/App.java b/tests/examples/JDBC/SpringJdbcTemplate/src/main/java/com/taosdata/jdbc/example/jdbcTemplate/App.java new file mode 100644 index 0000000000000000000000000000000000000000..a03ca3924f9d86199c461a21d887b78e433ef6fe --- /dev/null +++ b/tests/examples/JDBC/SpringJdbcTemplate/src/main/java/com/taosdata/jdbc/example/jdbcTemplate/App.java @@ -0,0 +1,48 @@ +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 weatherList = weatherDao.queryForList(limit, offset); + for (Weather w : weatherList) { + System.out.println(w); + } + + } + +} diff --git a/tests/examples/JDBC/SpringJdbcTemplate/src/main/java/com/taosdata/jdbc/example/jdbcTemplate/dao/ExecuteAsStatement.java b/tests/examples/JDBC/SpringJdbcTemplate/src/main/java/com/taosdata/jdbc/example/jdbcTemplate/dao/ExecuteAsStatement.java new file mode 100644 index 0000000000000000000000000000000000000000..f146684cc01f75a211182a90e5cfee4058aea413 --- /dev/null +++ b/tests/examples/JDBC/SpringJdbcTemplate/src/main/java/com/taosdata/jdbc/example/jdbcTemplate/dao/ExecuteAsStatement.java @@ -0,0 +1,6 @@ +package com.taosdata.jdbc.example.jdbcTemplate.dao; + +public interface ExecuteAsStatement{ + + void doExecute(String sql); +} diff --git a/tests/examples/JDBC/SpringJdbcTemplate/src/main/java/com/taosdata/jdbc/example/jdbcTemplate/dao/WeatherDao.java b/tests/examples/JDBC/SpringJdbcTemplate/src/main/java/com/taosdata/jdbc/example/jdbcTemplate/dao/WeatherDao.java new file mode 100644 index 0000000000000000000000000000000000000000..28962ee1e6803898a0ec24e2231a62d91bcbf6d6 --- /dev/null +++ b/tests/examples/JDBC/SpringJdbcTemplate/src/main/java/com/taosdata/jdbc/example/jdbcTemplate/dao/WeatherDao.java @@ -0,0 +1,17 @@ +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 weatherList); + + List queryForList(int limit, int offset); + + int count(); +} diff --git a/tests/examples/JDBC/SpringJdbcTemplate/src/main/java/com/taosdata/jdbc/example/jdbcTemplate/dao/impl/ExecuteAsStatementImpl.java b/tests/examples/JDBC/SpringJdbcTemplate/src/main/java/com/taosdata/jdbc/example/jdbcTemplate/dao/impl/ExecuteAsStatementImpl.java new file mode 100644 index 0000000000000000000000000000000000000000..2700e701cc42034975ff902ebe1da01f040b5a69 --- /dev/null +++ b/tests/examples/JDBC/SpringJdbcTemplate/src/main/java/com/taosdata/jdbc/example/jdbcTemplate/dao/impl/ExecuteAsStatementImpl.java @@ -0,0 +1,19 @@ +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); + } +} diff --git a/tests/examples/JDBC/SpringJdbcTemplate/src/main/java/com/taosdata/jdbc/example/jdbcTemplate/dao/impl/WeatherDaoImpl.java b/tests/examples/JDBC/SpringJdbcTemplate/src/main/java/com/taosdata/jdbc/example/jdbcTemplate/dao/impl/WeatherDaoImpl.java new file mode 100644 index 0000000000000000000000000000000000000000..1e0e0ab68c63bdb87ad172f277fe2a65df79d229 --- /dev/null +++ b/tests/examples/JDBC/SpringJdbcTemplate/src/main/java/com/taosdata/jdbc/example/jdbcTemplate/dao/impl/WeatherDaoImpl.java @@ -0,0 +1,64 @@ +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 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 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); + } +} diff --git a/tests/examples/JDBC/SpringJdbcTemplate/src/main/java/com/taosdata/jdbc/example/jdbcTemplate/domain/Weather.java b/tests/examples/JDBC/SpringJdbcTemplate/src/main/java/com/taosdata/jdbc/example/jdbcTemplate/domain/Weather.java new file mode 100644 index 0000000000000000000000000000000000000000..023b301481829b8d73a8af566d418aa8232ef899 --- /dev/null +++ b/tests/examples/JDBC/SpringJdbcTemplate/src/main/java/com/taosdata/jdbc/example/jdbcTemplate/domain/Weather.java @@ -0,0 +1,54 @@ +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; + } + + +} diff --git a/tests/examples/JDBC/SpringJdbcTemplate/src/main/resources/applicationContext.xml b/tests/examples/JDBC/SpringJdbcTemplate/src/main/resources/applicationContext.xml index 41128148ec3fb69f342c634cc8e9dd9fbd3c0037..19ac433385fabe53548dcba25d5bb10965df17af 100644 --- a/tests/examples/JDBC/SpringJdbcTemplate/src/main/resources/applicationContext.xml +++ b/tests/examples/JDBC/SpringJdbcTemplate/src/main/resources/applicationContext.xml @@ -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"> - + - - + + + + diff --git a/tests/examples/JDBC/SpringJdbcTemplate/src/test/java/com/taosdata/jdbc/AppTest.java b/tests/examples/JDBC/SpringJdbcTemplate/src/test/java/com/taosdata/jdbc/AppTest.java index d6a699598e73470663af4eb04e03a9a6b083bc4c..d0219f3db73d0c260420b642f997663acdd36f87 100644 --- a/tests/examples/JDBC/SpringJdbcTemplate/src/test/java/com/taosdata/jdbc/AppTest.java +++ b/tests/examples/JDBC/SpringJdbcTemplate/src/test/java/com/taosdata/jdbc/AppTest.java @@ -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); } } diff --git a/tests/examples/JDBC/SpringJdbcTemplate/src/test/java/com/taosdata/jdbc/example/jdbcTemplate/BatcherInsertTest.java b/tests/examples/JDBC/SpringJdbcTemplate/src/test/java/com/taosdata/jdbc/example/jdbcTemplate/BatcherInsertTest.java new file mode 100644 index 0000000000000000000000000000000000000000..7b6c4b13e223b97668205f3d80dba7875e65059b --- /dev/null +++ b/tests/examples/JDBC/SpringJdbcTemplate/src/test/java/com/taosdata/jdbc/example/jdbcTemplate/BatcherInsertTest.java @@ -0,0 +1,65 @@ +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 = 1496732686000l; + 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 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); + } + + +}