MybatisPlus实现Tdengine增删改查功能
1 增加pom
java
<!-- https://mvnrepository.com/artifact/com.taosdata.jdbc/taos-jdbcdriver -->
<dependency>
<groupId>com.taosdata.jdbc</groupId>
<artifactId>taos-jdbcdriver</artifactId>
<version>3.1.0</version>
<exclusions>
<exclusion>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>4.2.0</version>
</dependency>
2 配置信息
yaml
spring:
datasource:
dynamic:
primary: master #设置默认的数据源或者数据源组,默认值即为master
strict: false #严格匹配数据源,默认false. true未匹配到指定数据源时抛异常,false使用默认数据源
datasource:
master:
# url: jdbc:mysql://20.6.1.241:3306/shine_industry?characterEncoding=utf-8&useSSL=false&keepAlive=true&autoReconnect=true&autoReconnectForPools=true&connectTimeout=30000&socketTimeout=60000
url: jdbc:mysql://127.0.0.1:3306/shine_test_db?characterEncoding=utf-8&useSSL=false&keepAlive=true&autoReconnect=true&autoReconnectForPools=true&connectTimeout=30000&socketTimeout=60000
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
druid:
initial-size: 5 # 初始化大小
minIdle: 10 # 最小连接数
maxActive: 20 # 最大连接数
max-wait: 60000 # 获取连接时的最大等待时间
min-evictable-idle-time-millis: 300000 # 一个连接在池中最小生存的时间,单位是毫秒
time-between-eviction-runs-millis: 60000 # 多久才进行一次检测需要关闭的空闲连接,单位是毫秒
filters: stat # 配置扩展插件:stat-监控统计,log4j-日志,wall-防火墙(防止SQL注入),去掉后,监控界面的sql无法统计 ,wall
validation-query: SELECT 1 # 检测连接是否有效的 SQL语句,为空时以下三个配置均无效
test-on-borrow: true # 申请连接时执行validationQuery检测连接是否有效,默认true,开启后会降低性能
test-on-return: false # 归还连接时执行validationQuery检测连接是否有效,默认false,开启后会降低性能
test-while-idle: true # 申请连接时如果空闲时间大于timeBetweenEvictionRunsMillis,执行validationQuery检测连接是否有效,默认false,建议开启,不影响性能
maxPoolPreparedStatementPerConnectionSize: 50
keep-alive: false
ng:
driver-class-name: com.taosdata.jdbc.rs.RestfulDriver
url: jdbc:TAOS-RS://127.0.0.1:6041?useSSL=false&user=root&password=taosdata
# using connection pools
type: com.alibaba.druid.pool.DruidDataSource
druid:
initial-size: 5
min-idle: 5
max-active: 20
max-wait: 60000
time-between-eviction-runs-millis: 60000
min-evictable-idle-time-millis: 300000
validation-query: SELECT 1
pool-prepared-statements: true
max-pool-prepared-statement-per-connection-size: 20
druid:
connect-timeout: 300
maxPoolPreparedStatementPerConnectionSize: 100
initialSize: 10 # 初始化大小
minIdle: 5 # 最小连接数
maxActive: 20 # 最大连接数
max-wait: 60000 # 获取连接时的最大等待时间
min-evictable-idle-time-millis: 300000 # 一个连接在池中最小生存的时间,单位是毫秒
time-between-eviction-runs-millis: 60000 # 多久才进行一次检测需要关闭的空闲连接,单位是毫秒
filters: stat # 配置扩展插件:stat-监控统计,log4j-日志,wall-防火墙(防止SQL注入),去掉后,监控界面的sql无法统计 ,wall
validation-query: SELECT 1 # 检测连接是否有效的 SQL语句,为空时以下三个配置均无效
test-on-borrow: false # 申请连接时执行validationQuery检测连接是否有效,默认true,开启后会降低性能
test-on-return: false # 归还连接时执行validationQuery检测连接是否有效,默认false,开启后会降低性能
test-while-idle: true # 申请连接时如果空闲时间大于timeBetweenEvictionRunsMillis,执行validationQuery检测连接是否有效,默认false,建议开启,不影响性能
stat-view-servlet: # http://127.0.0.1:8088/druid/
enabled: true # 是否开启 StatViewServlet
allow: 127.0.0.1 # 访问监控页面 白名单,默认127.0.0.1
deny: 192.168.56.1 # 访问监控页面 黑名单
login-username: admin # 访问监控页面 登陆账号
login-password: admin # 访问监控页面 登陆密码
url-pattern: /druid/*
filter:
stat:
enabled: true # 是否开启 FilterStat,默认true
log-slow-sql: true # 是否开启 慢SQL 记录,默认false
slow-sql-millis: 5000 # 慢 SQL 的标准,默认 3000,单位:毫秒
merge-sql: false # 合并多个连接池的监控数据,默认false
keep-alive: true
3 实现Model
创建自己实体,然后集成baseModel
java
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.lang3.StringUtils;
import org.springframework.format.annotation.DateTimeFormat;
import java.sql.Timestamp;
import java.time.LocalDateTime;
/**
* taos基础类
*/
@EqualsAndHashCode(callSuper = true)
@Data
public class BaseTdModel<T extends Model<T>> extends Model<T> implements TdBaseInterface {
private static final long serialVersionUID = 2537647301574172972L;
@JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@ApiModelProperty("时间戳")
private Timestamp ts;
@JsonIgnore
@TableField(exist = false)
@ApiModelProperty("数据库名")
private String databaseName;
@JsonIgnore
@TableField(exist = false)
@ApiModelProperty("超级表名")
private String stableName;
@JsonIgnore
@TableField(exist = false)
@ApiModelProperty("表名")
private String tableName;
@JsonIgnore
@TableField(exist = false)
@ApiModelProperty("insertValue相关语句")
private String insertValue;
@JsonIgnore
@TableField(exist = false)
@ApiModelProperty("insertTag相关语句")
private String insertTag;
@JsonIgnore
@TableField(exist = false)
@ApiModelProperty("建表相关语句")
private String createValues;
@JsonIgnore
@TableField(exist = false)
@ApiModelProperty("批量新增语句")
private String batchSql;
/**
* 时间戳构造方法
*
* @param ts 时间戳
*/
public void setTs(Timestamp ts) {
this.ts = ts;
}
/**
* 时间戳构造方法
*
* @param ts 时间戳
*/
public void setTs(Long ts) {
this.ts = new Timestamp(ts);
}
/**
* 时间戳构造方法
*
* @param ts 时间戳
*/
public void setTs(LocalDateTime ts) {
this.ts = Timestamp.valueOf(ts);
}
/**
* 获取时间戳
*
* @return 时间戳
*/
public Long getTsLong() {
if (ObjectUtils.isNotEmpty(ts)) {
return ts.getTime();
}
return null;
}
/**
* 获取时间戳
*
* @return 时间戳
*/
@JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
public LocalDateTime getTsLocalDateTime() {
if (ObjectUtils.isNotEmpty(ts)) {
return ts.toLocalDateTime();
}
return null;
}
/**
* 初始化相关语句方法
*
* @param baseTaosModel taos基础实体类
*/
public void init(BaseTdModel<?> baseTaosModel) {
// 如果时间戳为空,默认当前时间
if (ObjectUtils.isEmpty(this.ts)) {
this.ts = new Timestamp(System.currentTimeMillis());
}
// 如果数据库或超级表为空,默认从@TableName获取
if (StringUtils.isEmpty(this.stableName)) {
this.databaseName = getTableName(baseTaosModel)[0];
}
if (StringUtils.isEmpty(this.stableName)) {
this.stableName = getTableName(baseTaosModel)[1];
}
// 获取value和tag语句
this.insertValue = getInsertValue(baseTaosModel);
this.insertTag = getInsertTag(baseTaosModel);
this.createValues = getCreateSql();
}
/**
* 重写构造方法,初始化成员变量,减少taos的null值存储
*/
public BaseTdModel() {
setStringFieldsToEmpty(this);
}
}
java
BaseTdModel 实现接口 TaosBaseInterface
public interface TaosBaseInterface {
default String getDatabaseName(BaseTaosModel<?> baseTaosModel){
TableName tableName = baseTaosModel.getClass().getAnnotation(TableName.class);
if(StringUtils.isNotBlank(tableName.value())){
// 如果格式为DatabaseName.STableName则返回DatabaseName
String[] split = tableName.value().split("\\.");
if(StringUtils.isNotBlank(split[0])){
return split[0];
}
}
return null;
}
default String getSTableName(BaseTaosModel<?> baseTaosModel){
TableName tableName = baseTaosModel.getClass().getAnnotation(TableName.class);
if(StringUtils.isNotBlank(tableName.value())){
// 如果格式为DatabaseName.STableName则返回DatabaseName
String[] split = tableName.value().split("\\.");
if(StringUtils.isNotBlank(split[1])){
return split[1];
}else {
return tableName.value();
}
}
return null;
}
/**
* 根据实体类动态创建超级表语句
* @return 建表属性语句
*/
@JsonIgnore
default String getCreateSql(){
Field[] fields = this.getClass().getDeclaredFields();
// 获取对应字段列表
List<Field> valueFields = getTagsFields(fields, false);
List<Field> tagFields = getTagsFields(fields, true);
StringJoiner values = new StringJoiner(",", "(", ")");
StringJoiner tags = new StringJoiner(",", "(", ")");
// 添加默认时间戳属性
values.add("ts timestamp");
// 拼接values
for (Field field : valueFields) {
try {
// 获取字段名和状态名
String name = field.getName();
String typeName = field.getType().getSimpleName();
values.add(CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, name)+" "+typeName);
} catch (Exception ex) {
ex.printStackTrace();
}
}
// 拼接tags
for (Field field : tagFields) {
try {
// 获取字段名和状态名
String name = field.getName();
String typeName = field.getType().getSimpleName();
tags.add(CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, name)+" "+typeName);
} catch (Exception ex) {
ex.printStackTrace();
}
}
// 拼接建表属性相关语句,工具类替换优化性能 | 替换string类型为nchar(32) | 替换boolean为bool
return StringUtils.replaceEach(values + " tags " + tags,
new String[]{"String", "boolean", "Boolean","Long"},
new String[]{"nchar(32)", "bool", "bool","bigint unsigned"});
}
/**
* 通过反射拼接所有values
*
* @return values 所有taos属性value字符串
*/
default String getInsertValue(BaseTaosModel<?> baseTaosModel) {
return getValues(baseTaosModel,true) + " values " + getValues(baseTaosModel,false);
}
default String getInsertTag(BaseTaosModel<?> baseTaosModel) {
return getTags(baseTaosModel,true) + " tags " + getTags(baseTaosModel,false);
}
/**
* 通过反射拼接所有values
*
* @param baseTaosModel taos基础类
* @param isColumn 返回字段还是value值(true:返回字段 | false:返回value)
* @return values 所有taos属性value字符串
*/
default String getValues(BaseTaosModel<?> baseTaosModel,boolean isColumn) {
Field[] fields = this.getClass().getDeclaredFields();
List<Field> orderedField = getTagsFields(fields, false);
StringJoiner values = new StringJoiner(",", "(", ")");
StringJoiner keys = new StringJoiner(",", "(", ")");
keys.add("ts");
values.add(Long.toString(baseTaosModel.getTs().getTime()));
for (Field field : orderedField) {
boolean annotationPresent = field.isAnnotationPresent(TableField.class);
if(annotationPresent){
//有此注解
TableField annotation = field.getAnnotation(TableField.class);
boolean exist = annotation.exist();
if(!exist){
//无视其存在,不参与语句拼接
continue;
}
}
try {
// 添加字段名
String name = field.getName();
keys.add(CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, name));
// 添加字段值
field.setAccessible(true);
Object o = field.get(baseTaosModel);
List<String> valueList = StringUtil.change2String(o);
values.add(valueList.get(0));
} catch (Exception ex) {
ex.printStackTrace();
}
}
if(isColumn){
return keys.toString();
}
return values.toString();
}
/**
* 通过反射拼接所有tags
*
* @param baseTaosModel taos基础类
* @param isColumn 返回字段还是tags值(true:返回字段 | false:返回tags)
* @return values 所有taos属性tags字符串
*/
default String getTags(BaseTaosModel<?> baseTaosModel,boolean isColumn) {
Field[] fields = this.getClass().getDeclaredFields();
List<Field> orderedField = getTagsFields(fields, true);
StringJoiner values = new StringJoiner(",", "(", ")");
StringJoiner keys = new StringJoiner(",", "(", ")");
for (Field field : orderedField) {
try {
// 添加字段名
String name = field.getName();
keys.add(CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, name));
// 添加字段值
field.setAccessible(true);
Object o = field.get(baseTaosModel);
List<String> valueList = StringUtil.change2String(o);
values.add(valueList.get(0));
} catch (Exception ex) {
ex.printStackTrace();
}
}
if(isColumn){
return keys.toString();
}
return values.toString();
}
/**
* 获取values或tags对应的字段
*
* @param fields 反射获取的Field数组
* @param isTags 是否时tags
* @return 排序后Field列表
*/
default List<Field> getTagsFields(Field[] fields, boolean isTags) {
// 用来存放所有的属性域
List<Field> values = new ArrayList<>();
List<Field> tags = new ArrayList<>();
// 判断是否是tags字段
for (Field f : fields) {
if (f.getAnnotation(Tags.class) != null) {
tags.add(f);
} else {
values.add(f);
}
}
if (isTags) {
return tags;
}
return values;
}
default void setStringFieldsToEmpty(Object obj) {
Field[] fields = this.getClass().getDeclaredFields();
// tag不能为空字符串
List<Field> tagsFields = getTagsFields(fields, false);
for (Field field : tagsFields) {
if (field.getType() == String.class) {
try {
field.setAccessible(true);
field.set(obj, "");
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
}
}
4 创建service impl实现类
java
public interface BaseTaosService<T extends BaseTaosModel<T>> extends IService<T> {
/**
* 创建默认数据库
* 重点:最好手动在taos中创建数据库 | 本方法使用于taos初始化默认创建
*
* @param baseTaosModel taos基础实体
*/
void createDatabase(T baseTaosModel);
/**
* 创建具有过期时间的数据库
* 重点:最好手动在taos中创建数据库 | 本方法使用于taos初始化默认创建
*
* @param baseTaosModel taos基础实体
* @param duration 过期间隔时间
* @param keep 保留数据时间
*/
void createDatabaseDuration(T baseTaosModel, String duration, String keep);
/**
* 创建超级表
* 重点:最好手动在taos中创建超级表 | 本方法使用于taos初始化默认创建
* 重点:如需使用,请了解@TableName规则--
* --多数据库同结构超级表请手动设置数据库
*
* @param baseTaosModel taos实体
*/
void createSTable(T baseTaosModel);
/**
* 删除超级表
*
* @param baseTaosModel taos基础类
*/
void dropSuperTable(T baseTaosModel);
/**
* 删除表
*
* @param baseTaosModel taos基础类
*/
void dropTable(T baseTaosModel);
/**
* 新增数据
*
* @param baseTaosModel taos基础类
* @return 影响行数
*/
int insert(T baseTaosModel);
/**
* 批量新增方法
* @param baseTaosModels taos基础实体类列表
* @param count 批量数据上限 需自己预估 留好容错空间,默认1000行
* @return 影响行数
*/
int insertBatch(List<T> baseTaosModels, int count);
/**
* 批量新增方法
* @param baseTaosModels taos基础实体类列表
*/
int insertBatch(List<T> baseTaosModels);
/**
* 获取taos所有数据库名方法
*
* @return 数据库名list
*/
List<String> showDatabases();
/**
* 获取实体类对应数据库下所有超级表名方法
*
* @param baseTaosModel taos实体
* @return 数据库下所有超级表list
*/
List<String> showSTables(T baseTaosModel);
/**
* 获取实体类对应数据库下所有表名方法
*
* @param baseTaosModel taos实体
* @return 数据库下所有表list
*/
List<String> showTables(T baseTaosModel);
/**
* 切换数据库
* 重点 : 当前使用restFul连接,无法切换数据库 使用前确认连接方式
*
* @param baseTaosModel taos实体
*/
void useDatabase(T baseTaosModel);
/**
* 创建流式计算方法
*
* @param streamName 流名称
* @param streamOptions 流参数
* @param stbName 表名
* @param subquery 查询语句
*/
void createStream(String streamName, String streamOptions, String stbName, String subquery);
/**
* 初始化流式计算
*/
void initStream();
}
@Slf4j
@DS("tdengine")
public class BaseTaosServiceImpl<M extends BaseTaosMapper<T>, T extends BaseTaosModel<T>> extends ServiceImpl<M, T> implements BaseTaosService<T> {
@Override
protected Class<T> currentMapperClass() {
return (Class<T>) this.getResolvableType().as(BaseTaosServiceImpl.class).getGeneric(0).getType();
}
@Override
protected Class<T> currentModelClass() {
return (Class<T>) this.getResolvableType().as(BaseTaosServiceImpl.class).getGeneric(1).getType();
}
@Override
public void createDatabase(T baseTaosModel) {
baseTaosModel.init(baseTaosModel);
baseMapper.createDatabase(baseTaosModel);
}
@Override
public void createDatabaseDuration(T baseTaosModel, String duration, String keep) {
baseTaosModel.init(baseTaosModel);
baseMapper.createDatabaseDuration(baseTaosModel, duration, keep);
}
@Override
public void createSTable(T baseTaosModel) {
baseTaosModel.init(baseTaosModel);
baseMapper.createDatabase(baseTaosModel);
baseMapper.createSTable(baseTaosModel);
}
@Override
public void dropSuperTable(T baseTaosModel) {
baseTaosModel.init(baseTaosModel);
baseMapper.dropSuperTable(baseTaosModel);
}
@Override
public void dropTable(T baseTaosModel) {
baseTaosModel.init(baseTaosModel);
baseMapper.dropTable(baseTaosModel);
}
@Override
public int insert(T baseTaosModel) {
baseTaosModel.init(baseTaosModel);
return baseMapper.insert(baseTaosModel);
}
@Override
public int insertBatch(List<T> list, int count) {
// 默认值设为1000
if (count == 0) {
count = 1000;
}
// 根据表名转换map
Map<String, List<T>> baseModelMap = list.stream()
.filter(baseTaosModel -> ObjectUtils.isNotEmpty(baseTaosModel.getTableName()))
.collect(Collectors.groupingBy(BaseTaosModel::getTableName));
Set<String> strings = baseModelMap.keySet();
// 不同表遍历新增
int finalCount = count;
strings.forEach(tableName -> {
List<T> baseTaosModelList = baseModelMap.get(tableName);
if (!baseTaosModelList.isEmpty()) {
// sql字段标志位
AtomicInteger i = new AtomicInteger(0);
// StringJoiner无法清除字符,所以采用StringBuilder拼接空格
StringBuilder tags = new StringBuilder();
StringBuilder values = new StringBuilder();
baseTaosModelList.forEach(baseTaosModel -> {
// 如果超过传入次数
if (i.get() > finalCount) {
String insertSql = tags.append(values).toString();
baseTaosModel.init(baseTaosModel);
baseTaosModel.setBatchSql(insertSql);
baseMapper.insertBatch(baseTaosModel, insertSql);
// i重新设施为0 | tags和values置空
i.set(0);
tags.delete(0, tags.length());
values.delete(0, values.length());
}
// 拼接新增语句
if (i.getAndIncrement() == 0) {
baseTaosModel.init(baseTaosModel);
tags.append(baseTaosModel.getInsertTag()).append(" ");
values.append(baseTaosModel.getInsertValue()).append(" ");
} else {
values.append(baseTaosModel.getValues(baseTaosModel, false)).append(" ");
}
});
String insertSql = tags.append(values).toString();
T baseTaosModel = baseTaosModelList.get(0);
baseTaosModel.init(baseTaosModel);
baseTaosModel.setBatchSql(insertSql);
baseMapper.insertBatch(baseTaosModel, insertSql);
}
});
return 0;
}
@Override
public int insertBatch(List<T> list) {
// 根据表名转换map
Map<String, List<T>> baseModelMap = list.stream()
.filter(baseTaosModel -> ObjectUtils.isNotEmpty(baseTaosModel.getTableName()))
.collect(Collectors.groupingBy(BaseTaosModel::getTableName));
Set<String> strings = baseModelMap.keySet();
// 不同表遍历新增
strings.forEach(tableName -> {
List<T> baseTaosModelList = baseModelMap.get(tableName);
if (!baseTaosModelList.isEmpty()) {
// sql字段标志位
AtomicInteger i = new AtomicInteger(0);
// 拼接字段
StringJoiner tags = new StringJoiner(" ");
StringJoiner values = new StringJoiner(" ");
baseTaosModelList.forEach(baseTaosModel -> {
// 最大字节长度
int maxLength = 1000000;
// 预测长度
if (values.length() * 3 > maxLength) {
String insertSql = tags.merge(values).toString();
baseTaosModel.init(baseTaosModel);
baseTaosModel.setBatchSql(insertSql);
baseMapper.insertBatch(baseTaosModel, insertSql);
// i重新设施为0 | tags和values置空
i.set(0);
tags.setEmptyValue("");
values.setEmptyValue("");
}
// 拼接新增语句
if (i.getAndIncrement() == 0) {
baseTaosModel.init(baseTaosModel);
tags.add(baseTaosModel.getInsertTag());
values.add(baseTaosModel.getInsertValue());
} else {
values.add(baseTaosModel.getValues(baseTaosModel, false));
}
});
String insertSql = tags.merge(values).toString();
T baseTaosModel = baseTaosModelList.get(0);
baseTaosModel.init(baseTaosModel);
baseTaosModel.setBatchSql(insertSql);
baseMapper.insertBatch(baseTaosModel, insertSql);
}
});
return 0;
}
@Override
public List<String> showDatabases() {
return baseMapper.showDatabases();
}
@Override
public List<String> showSTables(T baseTaosModel) {
return baseMapper.showSTables(baseTaosModel);
}
@Override
public List<String> showTables(T baseTaosModel) {
return baseMapper.showTables(baseTaosModel);
}
@Override
public void useDatabase(T baseTaosModel) {
baseMapper.useDatabase(baseTaosModel);
}
@Override
public void createStream(String streamName, String streamOptions, String stbName, String subquery) {
baseMapper.createStream(streamName, streamOptions, stbName, subquery);
}
@Override
public void initStream() {
//原始数据数据库名
String originSource = TDengineDatabaseParamEnums.MONTH_THREE.getDatabaseName() + "." + TDengineConstants.TABLE_NUMERICAL_NAME;
TDengineDatabaseParamEnums[] values = TDengineDatabaseParamEnums.values();
for (TDengineDatabaseParamEnums value : values) {
String databaseName = value.getDatabaseName();
if (databaseName.equals(TDengineDatabaseParamEnums.MONTH_THREE.getDatabaseName())) {
continue;
}
//原始数据源
List<String> aggregateTimeList = StringUtil.getAggregateTimeByDatabaseName(databaseName);
aggregateTimeList.forEach(aggregateTime -> {
String streamName = TDengineConstants.STREAM_NAME_PREFIX + aggregateTime;
String stbName = TDengineConstants.STREAM_TABLE_PREFIX + aggregateTime;
//查询内容
String subquery = StrUtil.format(TDengineSqlConstants.STREAM_SUBQUERY, originSource, aggregateTime);
createStream(streamName, null, stbName, subquery);
});
}
}
}
5 创建实体
java
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.growatt.industry.dao.model.td.BaseTdModel;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
/**
* 收益超级表
*/
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode(callSuper = true)
@Data
@TableName(value = "devicedb.income", autoResultMap = true)
public class TdIncome extends BaseTdModel<TdIncome> {
/**
*
*/
@TableField("plant_Id")
private Integer plantId;
/**
*
*/
@TableField("sn")
private String sn;
/**
* 地址
*/
@TableField("addr")
private Integer addr;
@TableField("year")
private Integer year;
@TableField("month")
private Integer month;
@TableField("day")
private Integer day;
@TableField("hour")
private Integer hour;
@TableField("min")
private Integer min;
/**
* 总收益
*/
@TableField("income")
private Double income;
/**
* 补贴收益(光伏补贴||储能补贴*放电量)
*/
@TableField("sub_Income")
private Double subIncome;
/**
* 放电量收益/卖电收益(馈入电网收益)
*/
@TableField("discharge_Income")
private Double dischargeIncome;
/**
* 充电量费用
*/
@TableField("charge_Income")
private Double chargeIncome;
/**
* 自发自用收益
*/
@TableField("pv_Self_Income")
private Double pvSelfIncome;
/**
* 收益类型(1:光伏,2:储能)
*/
@TableField("income_Type")
private Integer incomeType;
/**
* 时区
*/
@TableField("timezone_value")
private String timezoneValue;
/**
* 创建时间
*/
@TableField("create_Time")
private Long createTime;
/**
* 更新时间
*/
@TableField("update_Time")
private Long updateTime;
@Tags
private String location;
@Tags
private Integer groupid;
public void initIncome(Integer plantId, String sn, Integer address, double pvEarn, int year, int monthValue, int dayOfMonth, int hour, int minute, Double subEarn, double pvToGridIncome, double i, double pvSelfIncome, int incomeType,Float timezone) {
this.plantId = plantId;
this.sn = sn;
this.addr = address;
this.income = pvEarn;
this.subIncome = subEarn;
this.year = year;
this.month = monthValue;
this.day = dayOfMonth;
this.hour = hour;
this.min = minute;
this.dischargeIncome = pvToGridIncome;
this.chargeIncome = i;
this.pvSelfIncome = pvSelfIncome;
this.incomeType = incomeType;
this.timezoneValue = timezone.toString();
this.createTime = System.currentTimeMillis();
this.updateTime = System.currentTimeMillis();
this.location = TDConstants.TD_LOCATION;
this.groupid = TDConstants.TD_ROUPID;
this.setTableName("income_"+sn);
}
}