Skip to content
DemoApplicationTests.java 2.22 KiB
Newer Older
cuixiaowei's avatar
cuixiaowei committed
package com.example.demo;

import cn.hutool.core.lang.Console;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.json.JSONUtil;
import com.example.demo.mybatis.BatchService;
import com.example.demo.mybatis.Entity;
import com.example.demo.mybatis.EntityMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SpringBootTest
class DemoApplicationTests {

    @Resource
    private EntityMapper entityMapper;
    @Autowired
    private BatchService batchService;

    //                   测试类型转换
    @Test
    void contextLoads() {
        Entity entity = new Entity();
        Map<String, Object> context = new HashMap<>();
        context.put("111", "111");
        context.put("222", "222");
        context.put("333", "333");
        context.put("444", "444");
        entity.setId(IdUtil.simpleUUID())
                .setName(RandomUtil.randomString(4))
                .setContext(context);
        entityMapper.insert(entity);
    }

    @Test
    void select() {
        Entity id = entityMapper.findById("de7b193a8d6aa6aw73w46a5vb6");
        Console.log(id);
        Console.log(JSONUtil.toJsonStr(id));
    }

    //                   测试批量处
    @Test
    void batch() {
        List<Entity> entities = new ArrayList<Entity>();
        for (int i = 0; i < 10000; i++) {
            Entity entity = new Entity();
            entity.setName(RandomUtil.randomString("ces测试", 4));
            entities.add(entity);
        }
        batchService.batchInsertEntity(entities);

        this.noBath(entities);
    }

    void noBath(List<Entity> entities) {
        //批量保存执行前时间
        long start = System.currentTimeMillis();
        for (Entity entity : entities) {
            entity.setId(IdUtil.simpleUUID());
            entityMapper.insert(entity);
        }
        long end = System.currentTimeMillis();
        long time2 = end - start;
        //批量保存执行后的时间
        Console.log("执行时长" + time2);
    }


}