springboot对数据库单表插入大量数据测试

需求:
数据同步到es,数据库没有数据,需要插入大量数据到数据库中
插入后的数据
springboot对数据库单表插入大量数据测试_第1张图片
java代码实现
1.实体类(对应数据库表中的字段名)

@Data
public class Visit {

    private Integer id;

    @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
    @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
    private Date created_date;

    private Integer business_id;

    private Integer store_id;

    private Integer product_id;

    private Integer member_id;

    private Integer area_id;

}

2.映射文件编写

<mapper namespace="com.towker.mall.report.mapper.SaveDao">

    <insert id="saveSheeet" parameterType="java.util.List">
        insert into ts_visit_tracks (id,created_date,business_id, store_id, product_id, member_id, area_id)
        values
        <foreach collection="list" item="item" index="index"
                 separator=",">
            (#{item.id},#{item.created_date}, #{item.business_id}, #{item.store_id},
            #{item.product_id}, #{item.member_id}, #{item.area_id})
        </foreach>
    </insert>
</mapper>

3.dao层接口

public interface SaveDao {

    void saveSheeet(List<Visit> list);
}

4.Service层

@Service
public class SaveService {

    @Autowired
    private SaveDao saveDao;

    public void update(List<Visit> list){
        saveDao.saveSheeet(list);
    }
}

5.测试类

@Autowired
    private SaveService saveService;

    @Test
    public void test2(){
        List<Visit> list = new ArrayList<>();
        for (int i = 0; i < 3300 ; i++) {  //想要多少条数据,设置i就可以
            Visit visit = new Visit();
            visit.setCreated_date(new Date());
           int q = 1 + i;
            visit.setId(q);
           int a= 2018518+ i;
            visit.setBusiness_id(a);
            int b= 2033757 + i;
            visit.setStore_id(b);
            int c= 2018537 + i;
            visit.setProduct_id(c);
            int d= 2018518 + i;
            visit.setMember_id(d);
            int e= 1 + i;
            visit.setArea_id(e);
            list.add(visit);
        }
        saveService.update(list);
    }

你可能感兴趣的:(springboot)