resolve jdbc demo trivy issues
This commit is contained in:
parent
a2ad3d3922
commit
e540ff93a2
|
@ -5,7 +5,7 @@
|
||||||
<parent>
|
<parent>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-parent</artifactId>
|
<artifactId>spring-boot-starter-parent</artifactId>
|
||||||
<version>2.4.0</version>
|
<version>2.7.18</version>
|
||||||
<relativePath/> <!-- lookup parent from repository -->
|
<relativePath/> <!-- lookup parent from repository -->
|
||||||
</parent>
|
</parent>
|
||||||
<groupId>com.taosdata.example</groupId>
|
<groupId>com.taosdata.example</groupId>
|
||||||
|
@ -18,6 +18,18 @@
|
||||||
<java.version>1.8</java.version>
|
<java.version>1.8</java.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
|
<dependencyManagement>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.baomidou</groupId>
|
||||||
|
<artifactId>mybatis-plus-bom</artifactId>
|
||||||
|
<version>3.5.10.1</version>
|
||||||
|
<type>pom</type>
|
||||||
|
<scope>import</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
</dependencyManagement>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
@ -28,14 +40,21 @@
|
||||||
<artifactId>lombok</artifactId>
|
<artifactId>lombok</artifactId>
|
||||||
<optional>true</optional>
|
<optional>true</optional>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<!-- spring boot2 引入可选模块 -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.baomidou</groupId>
|
<groupId>com.baomidou</groupId>
|
||||||
<artifactId>mybatis-plus-boot-starter</artifactId>
|
<artifactId>mybatis-plus-boot-starter</artifactId>
|
||||||
<version>3.1.2</version>
|
</dependency>
|
||||||
|
|
||||||
|
<!-- jdk 8+ 引入可选模块 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.baomidou</groupId>
|
||||||
|
<artifactId>mybatis-plus-jsqlparser-4.9</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.h2database</groupId>
|
<groupId>com.h2database</groupId>
|
||||||
<artifactId>h2</artifactId>
|
<artifactId>h2</artifactId>
|
||||||
|
<version>2.3.232</version>
|
||||||
<scope>runtime</scope>
|
<scope>runtime</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
|
|
|
@ -1,34 +1,30 @@
|
||||||
package com.taosdata.example.mybatisplusdemo.config;
|
package com.taosdata.example.mybatisplusdemo.config;
|
||||||
|
|
||||||
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
|
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.annotation.DbType;
|
||||||
|
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
|
||||||
|
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
|
||||||
|
import org.mybatis.spring.annotation.MapperScan;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
|
|
||||||
|
@EnableTransactionManagement
|
||||||
@Configuration
|
@Configuration
|
||||||
|
@MapperScan("com.taosdata.example.mybatisplusdemo.mapper")
|
||||||
public class MybatisPlusConfig {
|
public class MybatisPlusConfig {
|
||||||
|
|
||||||
|
/**
|
||||||
/** mybatis 3.4.1 pagination config start ***/
|
* 添加分页插件
|
||||||
// @Bean
|
*/
|
||||||
// public MybatisPlusInterceptor mybatisPlusInterceptor() {
|
|
||||||
// MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
|
|
||||||
// interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
|
|
||||||
// return interceptor;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// @Bean
|
|
||||||
// public ConfigurationCustomizer configurationCustomizer() {
|
|
||||||
// return configuration -> configuration.setUseDeprecatedExecutor(false);
|
|
||||||
// }
|
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public PaginationInterceptor paginationInterceptor() {
|
public MybatisPlusInterceptor mybatisPlusInterceptor() {
|
||||||
// return new PaginationInterceptor();
|
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
|
||||||
PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
|
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL)); // 如果配置多个插件, 切记分页最后添加
|
||||||
//TODO: mybatis-plus do not support TDengine, use postgresql Dialect
|
// 如果有多数据源可以不配具体类型, 否则都建议配上具体的 DbType
|
||||||
paginationInterceptor.setDialectType("postgresql");
|
return interceptor;
|
||||||
|
|
||||||
return paginationInterceptor;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,6 +5,7 @@ import com.taosdata.example.mybatisplusdemo.domain.Meters;
|
||||||
import org.apache.ibatis.annotations.Insert;
|
import org.apache.ibatis.annotations.Insert;
|
||||||
import org.apache.ibatis.annotations.Param;
|
import org.apache.ibatis.annotations.Param;
|
||||||
import org.apache.ibatis.annotations.Update;
|
import org.apache.ibatis.annotations.Update;
|
||||||
|
import org.apache.ibatis.executor.BatchResult;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
@ -15,17 +16,6 @@ public interface MetersMapper extends BaseMapper<Meters> {
|
||||||
|
|
||||||
@Insert("insert into meters (tbname, ts, groupid, location, current, voltage, phase) values(#{tbname}, #{ts}, #{groupid}, #{location}, #{current}, #{voltage}, #{phase})")
|
@Insert("insert into meters (tbname, ts, groupid, location, current, voltage, phase) values(#{tbname}, #{ts}, #{groupid}, #{location}, #{current}, #{voltage}, #{phase})")
|
||||||
int insertOne(Meters one);
|
int insertOne(Meters one);
|
||||||
|
|
||||||
@Insert({
|
|
||||||
"<script>",
|
|
||||||
"insert into meters (tbname, ts, groupid, location, current, voltage, phase) values ",
|
|
||||||
"<foreach collection='list' item='item' index='index' separator=','>",
|
|
||||||
"(#{item.tbname}, #{item.ts}, #{item.groupid}, #{item.location}, #{item.current}, #{item.voltage}, #{item.phase})",
|
|
||||||
"</foreach>",
|
|
||||||
"</script>"
|
|
||||||
})
|
|
||||||
int insertBatch(@Param("list") List<Meters> metersList);
|
|
||||||
|
|
||||||
@Update("drop stable if exists meters")
|
@Update("drop stable if exists meters")
|
||||||
void dropTable();
|
void dropTable();
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,9 +11,6 @@ public interface TemperatureMapper extends BaseMapper<Temperature> {
|
||||||
@Update("CREATE TABLE if not exists temperature(ts timestamp, temperature float) tags(location nchar(64), tbIndex int)")
|
@Update("CREATE TABLE if not exists temperature(ts timestamp, temperature float) tags(location nchar(64), tbIndex int)")
|
||||||
int createSuperTable();
|
int createSuperTable();
|
||||||
|
|
||||||
@Update("create table #{tbName} using temperature tags( #{location}, #{tbindex})")
|
|
||||||
int createTable(@Param("tbName") String tbName, @Param("location") String location, @Param("tbindex") int tbindex);
|
|
||||||
|
|
||||||
@Update("drop table if exists temperature")
|
@Update("drop table if exists temperature")
|
||||||
void dropSuperTable();
|
void dropSuperTable();
|
||||||
|
|
||||||
|
|
|
@ -10,7 +10,7 @@ public interface WeatherMapper extends BaseMapper<Weather> {
|
||||||
@Update("CREATE TABLE if not exists weather(ts timestamp, temperature float, humidity int, location nchar(100))")
|
@Update("CREATE TABLE if not exists weather(ts timestamp, temperature float, humidity int, location nchar(100))")
|
||||||
int createTable();
|
int createTable();
|
||||||
|
|
||||||
@Insert("insert into weather (ts, temperature, humidity, location) values(#{ts}, #{temperature}, #{humidity}, #{location})")
|
@Insert("insert into weather (ts, temperature, humidity, location) values(#{ts}, #{temperature}, #{humidity}, #{location, jdbcType=NCHAR})")
|
||||||
int insertOne(Weather one);
|
int insertOne(Weather one);
|
||||||
|
|
||||||
@Update("drop table if exists weather")
|
@Update("drop table if exists weather")
|
||||||
|
|
|
@ -0,0 +1,19 @@
|
||||||
|
package com.taosdata.example.mybatisplusdemo.service;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import javax.sql.DataSource;
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class DatabaseConnectionService {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private DataSource dataSource;
|
||||||
|
|
||||||
|
public Connection getConnection() throws SQLException {
|
||||||
|
return dataSource.getConnection();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,26 @@
|
||||||
|
package com.taosdata.example.mybatisplusdemo.service;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.sql.Statement;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class TemperatureService {
|
||||||
|
@Autowired
|
||||||
|
private DatabaseConnectionService databaseConnectionService;
|
||||||
|
|
||||||
|
public void createTable(String tableName, String location, int tbIndex) throws SQLException {
|
||||||
|
|
||||||
|
|
||||||
|
try (Connection connection = databaseConnectionService.getConnection();
|
||||||
|
Statement statement = connection.createStatement()) {
|
||||||
|
// 执行 SQL 查询
|
||||||
|
statement.executeUpdate("create table " + tableName + " using temperature tags( '" + location +"', " + tbIndex + ")");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -5,6 +5,7 @@ import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||||
import com.taosdata.example.mybatisplusdemo.domain.Meters;
|
import com.taosdata.example.mybatisplusdemo.domain.Meters;
|
||||||
import com.taosdata.example.mybatisplusdemo.domain.Weather;
|
import com.taosdata.example.mybatisplusdemo.domain.Weather;
|
||||||
|
import org.apache.ibatis.executor.BatchResult;
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
@ -18,6 +19,8 @@ import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
|
||||||
|
import static java.sql.Statement.SUCCESS_NO_INFO;
|
||||||
|
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
@SpringBootTest
|
@SpringBootTest
|
||||||
public class MetersMapperTest {
|
public class MetersMapperTest {
|
||||||
|
@ -63,8 +66,19 @@ public class MetersMapperTest {
|
||||||
metersList.add(one);
|
metersList.add(one);
|
||||||
|
|
||||||
}
|
}
|
||||||
int affectRows = mapper.insertBatch(metersList);
|
List<BatchResult> affectRowsList = mapper.insert(metersList, 10000);
|
||||||
Assert.assertEquals(100, affectRows);
|
|
||||||
|
long totalAffectedRows = 0;
|
||||||
|
for (BatchResult batchResult : affectRowsList) {
|
||||||
|
int[] updateCounts = batchResult.getUpdateCounts();
|
||||||
|
for (int status : updateCounts) {
|
||||||
|
if (status == SUCCESS_NO_INFO) {
|
||||||
|
totalAffectedRows++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Assert.assertEquals(100, totalAffectedRows);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -93,7 +107,7 @@ public class MetersMapperTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSelectCount() {
|
public void testSelectCount() {
|
||||||
int count = mapper.selectCount(null);
|
long count = mapper.selectCount(null);
|
||||||
// Assert.assertEquals(5, count);
|
// Assert.assertEquals(5, count);
|
||||||
System.out.println(count);
|
System.out.println(count);
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||||
import com.taosdata.example.mybatisplusdemo.domain.Temperature;
|
import com.taosdata.example.mybatisplusdemo.domain.Temperature;
|
||||||
|
import com.taosdata.example.mybatisplusdemo.service.TemperatureService;
|
||||||
import org.junit.After;
|
import org.junit.After;
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
|
@ -13,6 +14,8 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
|
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
import java.sql.Timestamp;
|
import java.sql.Timestamp;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
@ -22,18 +25,20 @@ import java.util.Random;
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
@SpringBootTest
|
@SpringBootTest
|
||||||
public class TemperatureMapperTest {
|
public class TemperatureMapperTest {
|
||||||
|
@Autowired
|
||||||
|
private TemperatureService temperatureService;
|
||||||
|
|
||||||
private static Random random = new Random(System.currentTimeMillis());
|
private static Random random = new Random(System.currentTimeMillis());
|
||||||
private static String[] locations = {"北京", "上海", "深圳", "广州", "杭州"};
|
private static String[] locations = {"北京", "上海", "深圳", "广州", "杭州"};
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void before() {
|
public void before() throws SQLException {
|
||||||
mapper.dropSuperTable();
|
mapper.dropSuperTable();
|
||||||
// create table temperature
|
// create table temperature
|
||||||
mapper.createSuperTable();
|
mapper.createSuperTable();
|
||||||
// create table t_X using temperature
|
// create table t_X using temperature
|
||||||
for (int i = 0; i < 10; i++) {
|
for (int i = 0; i < 10; i++) {
|
||||||
mapper.createTable("t" + i, locations[random.nextInt(locations.length)], i);
|
temperatureService.createTable("t" + i, locations[i % locations.length], i);
|
||||||
}
|
}
|
||||||
// insert into table
|
// insert into table
|
||||||
int affectRows = 0;
|
int affectRows = 0;
|
||||||
|
@ -107,7 +112,7 @@ public class TemperatureMapperTest {
|
||||||
* **/
|
* **/
|
||||||
@Test
|
@Test
|
||||||
public void testSelectCount() {
|
public void testSelectCount() {
|
||||||
int count = mapper.selectCount(null);
|
long count = mapper.selectCount(null);
|
||||||
Assert.assertEquals(10, count);
|
Assert.assertEquals(10, count);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -52,7 +52,7 @@ public class WeatherMapperTest {
|
||||||
one.setTemperature(random.nextFloat() * 50);
|
one.setTemperature(random.nextFloat() * 50);
|
||||||
one.setHumidity(random.nextInt(100));
|
one.setHumidity(random.nextInt(100));
|
||||||
one.setLocation("望京");
|
one.setLocation("望京");
|
||||||
int affectRows = mapper.insert(one);
|
int affectRows = mapper.insertOne(one);
|
||||||
Assert.assertEquals(1, affectRows);
|
Assert.assertEquals(1, affectRows);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -82,7 +82,7 @@ public class WeatherMapperTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSelectCount() {
|
public void testSelectCount() {
|
||||||
int count = mapper.selectCount(null);
|
long count = mapper.selectCount(null);
|
||||||
// Assert.assertEquals(5, count);
|
// Assert.assertEquals(5, count);
|
||||||
System.out.println(count);
|
System.out.println(count);
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,7 @@
|
||||||
<parent>
|
<parent>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-parent</artifactId>
|
<artifactId>spring-boot-starter-parent</artifactId>
|
||||||
<version>2.6.15</version>
|
<version>2.7.18</version>
|
||||||
<relativePath/> <!-- lookup parent from repository -->
|
<relativePath/> <!-- lookup parent from repository -->
|
||||||
</parent>
|
</parent>
|
||||||
<groupId>com.taosdata.example</groupId>
|
<groupId>com.taosdata.example</groupId>
|
||||||
|
|
Loading…
Reference in New Issue