针对轻量级数据(单次包数据量10万)处理,提供数据转移存储到MySQL、Kafka、ES、HBase、Redis 配置相应的数据源,注入IUpser对象并使用upsert方法
我们将通过一个简单的 Demo 来阐述 Upsert功能,在此之前,我们假设您已经:
现有一个数据库 temp
创建一个空的 Spring Boot 工程
可以使用 Spring Initializer (opens new window)快速初始化一个 Spring Boot 工程
引入 Spring Boot Starter 父工程:
org.springframework.boot
spring-boot-starter-parent
2.4.4
top.wu2020
wu-easy-upsert-starter
引入 spring-boot-starter
、wu-easy-upsert-starter依赖:
Spring Boot 启动类:
@SpringBootApplication
public class QuickStartApplication {
public static void main(String[] args) {
SpringApplication.run(QuickStartApplication.class, args);
}
@Bean(name = "dataSourceMySQL")
public DataSource dataSourceMySQL() {
MysqlDataSource build = DataSourceBuilder.create().type(MysqlDataSource.class).build();
build.setUrl("jdbc:mysql://127.0.0.1:3306/temp?rewriteBatchedStatements=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai");
build.setUser("root");
build.setPassword("wujiawei");
return build;
}
}
编写实体类 UserLog.java
(此处使用了 Lombok 简化代码)
@Data
@EasySmart(perfectTable = true)
public class UserLog {
private Integer userId;
@EasySmartField(name = "`current_time`")
private LocalDateTime currentTime;
@EasySmartField(name = "`content`")
private String content;
@EasySmartField(name = "is_succeed")
private boolean isSucceed;
private String type;
}
编写实体类 UpsertBinary.java
(此处使用了 Lombok 简化代码)
@EasySmart(perfectTable = true)
@Data
public class UpsertBinary {
private File file = new File("/Users/wujiawei/Desktop/aa.mp3");
private String name = file.getName();
}
/**
* description 创建测试数据
*
* @param
* @return
* @exception/throws
* @author 吴佳伟
* @date 2021/4/19 上午10:09
*/
public List createUserLog(Integer size) {
List userLogList = new ArrayList<>();
size = size == null ? 10000 : size;
for (int i = 0; i < size; i++) {
UserLog userLog = new UserLog();
userLog.setCurrentTime(LocalDateTime.now());
userLog.setContent("创建时间:" + userLog.getCurrentTime());
userLog.setUserId(i);
userLogList.add(userLog);
}
return userLogList;
}
/**
* description IUpsert操作数据入DB
*
* @param
* @return
* @exception/throws
* @author 吴佳伟
* @date 2021/4/15 上午9:50
*/
@EasyUpsertDS(type = EasyUpsertType.MySQL)
@ApiOperation(tags = "MySQL快速插入数据", value = "IUpsert操作数据入DB")
@GetMapping()
public List upsert(@RequestParam(required = false, defaultValue = "100") Integer size) {
List userLogList = createUserLog(size);
iUpsert.upsert(userLogList, userLogList, new UserLog());
return userLogList;
}
/**
* description 使用注解实现数据插入
*
* @param
* @return
* @exception/throws
* @author 吴佳伟
* @date 2021/4/19 上午10:11
*/
@QuickEasyUpsert(type = EasyUpsertType.MySQL)
@ApiOperation(tags = "MySQL快速插入数据", value = "使用注解实现数据插入")
@GetMapping("/size")
public List upsertSize(@RequestParam(required = false, defaultValue = "100") Integer size) {
return createUserLog(size);
}
@QuickEasyUpsert(type = EasyUpsertType.MySQL)
@ApiOperation(tags = "MySQL快速插入数据", value = "复杂数据EasyHashMap")
@GetMapping("/easyHashMap")
public List easyHashMap(@RequestParam(required = false, defaultValue = "1000") Integer size) {
List easyHashMapList = new ArrayList<>();
for (int i = 0; i < size; i++) {
EasyHashMap easyHashMap = new EasyHashMap("uniqueLabel");
easyHashMap.put("第一个字段", "第一个字段");
easyHashMap.put("第二个字段", "第二个字段");
easyHashMap.put("第三个字段", "第三个字段");
easyHashMap.put("第四个字段", "第四个字段");
easyHashMapList.add(easyHashMap);
}
return easyHashMapList;
}
/**
* description binary 或者文件类型数据插入
*
* @param
* @return
* @exception/throws
* @author 吴佳伟
* @date 2021/4/19 上午10:11
*/
@QuickEasyUpsert(type = EasyUpsertType.MySQL)
@ApiOperation(tags = "MySQL快速插入数据", value = "binary 数据插入")
@GetMapping("/binary")
public List binary(@RequestParam(required = false, defaultValue = "1000") Integer size) {
List upsertBinaryList = new ArrayList<>();
for (int i = 0; i < size; i++) {
upsertBinaryList.add(new UpsertBinary());
}
return upsertBinaryList;
}
介绍 upsert 注解包相关类详解(更多详细描述可点击查看源码注释)
属性 | 类型 | 必须指定 | 默认值 | 描述 |
---|---|---|---|---|
value | String | 否 | 类名驼峰转下滑线 | 表名 |
tableName | String | 否 | 类名驼峰转下滑线 | 表名 |
perfectTable | boolean | 否 | false | 是否完善表,当表不存在创建表、表字段格式不一致会、数据库表为主,实体字段为辅 |
comment | String | 否 | "" | 表注释(创建表时起效) |
dataDrillDown | boolean | 否 | false | 是否支持数据下钻(当类中属性含有@SmartMark并且不是基本数据类型时有效) |
schema | String | 否 | "" | schema |
关于`EasySmart`的说明:
EasySmart注解集成LayerClass注解、LazyTable注解并支持自定义继承当前注解:
属性 | 类型 | 必须指定 | 默认值 | 描述 |
---|---|---|---|---|
value | String | 否 | 字段驼峰转换下划线 | 数据库字段名 |
name | String | 否 | 字段驼峰转换下划线 | 数据库字段名 |
exist | boolean | 否 | true | 是否为数据库表字段 |
type | String | 否 | "" | 数据库对应字段类型,创建数据时有效 |
comment | String | 否 | "" | 字段描述 |
fieldDefaultValue | String | 否 | "" | 字段默认值(字段为空是有效) |
iEnum | Enum | 否 | DefaultIEnum | 当前字段值和枚举类中的code值进行比较,如果找到将item值赋值给字段,转换失败赋值-1 |
indexType | Enum | 否 | LayerField.LayerFieldType.FILE_TYPE | 字段类型FILE_TYPE,ID, UNIQUE, AUTOMATIC; |
关于`EasyUpsertDS`的说明:
EasyUpsertDS支持自定义继承当前注解:
@Bean(name = "dataSourceMySQL")中的name
关于`QuickEasyUpsert`的说明:
QuickEasyUpsert继承了EasyUpsertDS支持自定义继承当前注解:
@Bean(name = "dataSourceMySQL")中的name
iUpsert.upsert();
模版项目地址