easy-upsert-starter 轻量级数据转换存储服务-MySQL使用(二)

easy-upsert-starter 轻量级数据转换存储服务

简介:

    针对轻量级数据(单次包数据量10万)处理,提供数据转移存储到MySQL、Kafka、ES、HBase、Redis 配置相应的数据源,注入IUpser对象并使用upsert方法

 

快速开始:

我们将通过一个简单的 Demo 来阐述 Upsert功能,在此之前,我们假设您已经:

  • 拥有 Java 开发环境以及相应 IDE
  • 熟悉 Spring Boot
  • 熟悉 Maven

现有一个数据库 temp

 

初始化工程

创建一个空的 Spring Boot 工程

可以使用 Spring Initializer (opens new window)快速初始化一个 Spring Boot 工程

添加依赖

引入 Spring Boot Starter 父工程:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.4.4</version>
    <relativePath/>
</parent>


    <dependencies>
        <dependency>
            <groupId>top.wu2020</groupId>
            <artifactId>wu-easy-upsert-starter</artifactId>
        </dependency>
    </dependencies>

引入 spring-boot-starter、wu-easy-upsert-starter依赖:

 

MySQL 数据源配置和使用

配置

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();
}

开始使用

  • 添加测试类,使用IUpsert测试:
  
    /**
     * description 创建测试数据
     *
     * @param
     * @return
     * @exception/throws
     * @author 吴佳伟
     * @date 2021/4/19 上午10:09
     */
    public List<UserLog> createUserLog(Integer size) {
        List<UserLog> 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<UserLog> upsert(@RequestParam(required = false, defaultValue = "100") Integer size) {
        List<UserLog> 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<UserLog> upsertSize(@RequestParam(required = false, defaultValue = "100") Integer size) {
        return createUserLog(size);
    }
  • 使用注解&特殊实体EasyHashMap测试
 @QuickEasyUpsert(type = EasyUpsertType.MySQL)
    @ApiOperation(tags = "MySQL快速插入数据", value = "复杂数据EasyHashMap")
    @GetMapping("/easyHashMap")
    public List<EasyHashMap> easyHashMap(@RequestParam(required = false, defaultValue = "1000") Integer size) {
        List<EasyHashMap> 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<UpsertBinary> binary(@RequestParam(required = false, defaultValue = "1000") Integer size) {
        List<UpsertBinary> upsertBinaryList = new ArrayList<>();
        for (int i = 0; i < size; i++) {
            upsertBinaryList.add(new UpsertBinary());
        }
        return upsertBinaryList;
    }

注解

介绍 upsert 注解包相关类详解(更多详细描述可点击查看源码注释)

@EasySmart

  • 描述注解 简单灵性数据标注 (表注解)
属性类型必须指定默认值描述
valueString类名驼峰转下滑线表名
tableNameString类名驼峰转下滑线表名
perfectTablebooleanfalse是否完善表,当表不存在创建表、表字段格式不一致会、数据库表为主,实体字段为辅
commentString""表注释(创建表时起效)
dataDrillDownbooleanfalse是否支持数据下钻(当类中属性含有@SmartMark并且不是基本数据类型时有效)
schemaString""schema

 

关于`EasySmart`的说明:

EasySmart注解集成LayerClass注解、LazyTable注解并支持自定义继承当前注解:

  1. 定义在comment中的描述当调用自动创建表语句时有效如:SQLConverter.creatTableSQL(UserLog.class) 控制台打印sql
  2. value与tableName声明同个字段表名

 

@EasySmartField

  • 描述注解  灵性字段注解 (表对应的字段) 
属性类型必须指定默认值描述
valueString字段驼峰转换下划线数据库字段名
nameString字段驼峰转换下划线数据库字段名
existbooleantrue是否为数据库表字段
typeString""数据库对应字段类型,创建数据时有效
commentString""字段描述
fieldDefaultValueString""字段默认值(字段为空是有效)
iEnumEnumDefaultIEnum当前字段值和枚举类中的code值进行比较,如果找到将item值赋值给字段,转换失败赋值-1
indexTypeEnumLayerField.LayerFieldType.FILE_TYPE

字段类型FILE_TYPE,ID, UNIQUE, AUTOMATIC;

@EasyUpsertDS

  • 描述注解 进行数据源切换(mysql配置多数据源进行数据源切换)

关于`EasyUpsertDS`的说明:

EasyUpsertDS支持自定义继承当前注解:

  1. 作用域方法上、类上
  2. MySQL下使用,支持MyBatis的动态数据源配置的多数据源,当自定义注入DataSource数据源名称为
    @Bean(name = "dataSourceMySQL")中的name

@QuickEasyUpsert

  • 描述注解 进行数据源切换并将返回的数据保存(mysql配置多数据源进行数据源切换)

关于`QuickEasyUpsert`的说明:

QuickEasyUpsert继承了EasyUpsertDS支持自定义继承当前注解:

  1. 作用域方法上、类上,作用将方法返回数据直接存储
  2. MySQL下使用,支持MyBatis的动态数据源配置的多数据源,当自定义注入DataSource数据源名称为
    @Bean(name = "dataSourceMySQL")中的name

核心功能

iUpsert.upsert();

拓展

 

架构设计

 

模版项目地址

 

 

  • 30
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小吴小吴bug全无

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值