【Mybatis】向MySql数据库插入千万记录 单条插入方式,用时 1h16m30s
本例代码下载:https://files.cnblogs.com/files/xiandedanteng/InsertMillionComparison20191012.rar
相对于批量插入,这种方式可谓虚耗时光。
代码:
package com.hy.action;
import java.io.Reader;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.log4j.Logger;
import com.hy.entity.Employee;
import com.hy.mapper.EmpMapper;
public class SingleInsert1003 {
private static Logger logger = Logger.getLogger(SelectById.class);
public static void main(String[] args) throws Exception{
long startTime = System.currentTimeMillis();
Reader reader=Resources.getResourceAsReader("mybatis-config.xml");
SqlSessionFactory ssf=new SqlSessionFactoryBuilder().build(reader);
reader.close();
SqlSession session=ssf.openSession();
try {
EmpMapper mapper=session.getMapper(EmpMapper.class);
String ctime="2017-11-01 00:00:01";
int index=0;
int changedTotal=0;
for(int i=0;i<10000;i++) {
for(int j=0;j<1000;j++) {
index++;
Employee emp=new Employee("E"+String.valueOf(index),index % 100,ctime);
int changed=mapper.singleInsert(emp.getName(), emp.getAge(), emp.getCtime());
changedTotal+=changed;
ctime=timePastOneSecond(ctime);
}
session.commit();
System.out.println("#"+i+" changed="+changedTotal);
}
}catch(Exception ex) {
session.rollback();
logger.error(ex);
}finally {
session.close();
long endTime = System.currentTimeMillis();
logger.info("Time elapsed:" + toDhmsStyle((endTime - startTime)/1000) + ".");
}
}
public static String timePastOneSecond(String otime) {
try {
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date dt=sdf.parse(otime);
Calendar newTime = Calendar.getInstance();
newTime.setTime(dt);
newTime.add(Calendar.SECOND,1);
Date dt1=newTime.getTime();
String retval = sdf.format(dt1);
return retval;
}
catch(Exception ex) {
ex.printStackTrace();
return null;
}
}
// format seconds to day hour minute seconds style
// Example 5000s will be formatted to 1h23m20s
public static String toDhmsStyle(long allSeconds) {
String DateTimes = null;
long days = allSeconds / (60 * 60 * 24);
long hours = (allSeconds % (60 * 60 * 24)) / (60 * 60);
long minutes = (allSeconds % (60 * 60)) / 60;
long seconds = allSeconds % 60;
if (days > 0) {
DateTimes = days + "d" + hours + "h" + minutes + "m" + seconds + "s";
} else if (hours > 0) {
DateTimes = hours + "h" + minutes + "m" + seconds + "s";
} else if (minutes > 0) {
DateTimes = minutes + "m" + seconds + "s";
} else {
DateTimes = seconds + "s";
}
return DateTimes;
}
}
Mapper:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.hy.mapper.EmpMapper">
<select id="selectById" resultType="com.hy.entity.Employee">
select id,name,age,cdate as ctime from emp where id=#{id}
</select>
<insert id="batchInsert">
insert into emp(name,age,cdate)
values
<foreach collection="list" item="emp" separator=",">
(#{emp.name},#{emp.age},#{emp.ctime,jdbcType=TIMESTAMP})
</foreach>
</insert>
<insert id="singleInsert">
insert into emp(name,age,cdate)
values (#{name},#{age},#{ctime,jdbcType=TIMESTAMP})
</insert>
</mapper>
与Mapper对应的接口:
package com.hy.mapper;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import com.hy.entity.Employee;
public interface EmpMapper {
Employee selectById(long id);
int batchInsert(List<Employee> emps);
// 用@Param标签指明和SQL的参数对应能避免出现org.apache.ibatis.binding.BindingException异常
int singleInsert(@Param("name")String name,@Param("age")int age,@Param("ctime")String ctime);
}
部分输出如下:
#9991 changed=9992000 #9992 changed=9993000 #9993 changed=9994000 #9994 changed=9995000 #9995 changed=9996000 #9996 changed=9997000 #9997 changed=9998000 #9998 changed=9999000 #9999 changed=10000000 INFO [main] - Time elapsed:1h16m30s.
数据库截图如下:


--END-- 2019年10月12日20:03:26
【Mybatis】向MySql数据库插入千万记录 单条插入方式,用时 1h16m30s的更多相关文章
- mybatis连接mysql数据库插入中文乱码
对于MySQL数据库的乱码问题,有两种情况: 1. mysql数据库编码问题(建库时设定). 2. 连接mysql数据库的url编码设置问题. 对于第一个问题,目前个人发现只能通过重新建库解决,建库的 ...
- Spring Boot入门(六):使用MyBatis访问MySql数据库(注解方式)
本系列博客记录自己学习Spring Boot的历程,如帮助到你,不胜荣幸,如有错误,欢迎指正! 本篇博客我们讲解下在Spring Boot中使用MyBatis访问MySql数据库的简单用法. 1.前期 ...
- 五、使用druid管理数据库,mybatis连接mysql数据库
简介: 使用 mybatis 连接 mysql 数据库, 一套简单的增删改查流程, 前台用 bootstrap, bootstrap-table 框架, 最后用 druid 监控数据库连接情况 ...
- (45). Spring Boot MyBatis连接Mysql数据库【从零开始学Spring Boot】
大家在开发的时候,会喜欢jdbcTemplate操作数据库,有喜欢JPA操作数据库的,有喜欢MyBatis操作数据库的,对于这些我个人觉得哪个使用顺手就使用哪个就好了,并没有一定要使用哪个,个人在实际 ...
- mybatis连接mysql数据库实现的jdbc功能
最近公司项目要使用myBatis,自己以前没有接触过,就在网上找到了一些资料研究了些.初步做出了基于myBatis连接mysql数据库的jdbc实现的功能. employee.java package ...
- Mybatis连接mysql数据库出现乱码
对于mysql数据库的乱码问题,有两中情况: 1. mysql数据库编码问题(建库时设定). 2. 连接mysql数据库的url编码设置问题. 对于第一个问题,目前个人发现只能通过重新建库解决,建库的 ...
- mybatis查询mysql数据库tinyint(1)变为boolean类型
mybatis查询mysql数据库对象转化为Map,tinyint(1)被转化为boolean类型,可以t通过避免使用tinyint(1)来解决.
- 如何用IDEA创建springboot(maven)并且整合mybatis连接mysql数据库和遇到的问题
一.New->Project 二.点击next 三.在Group栏输入组织名,Artifact就是项目名.选择需要的java版本,点击next 四.添加需要的依赖 在这里我们也可以添加sql方面 ...
- CentOS6.7下使用非root用户(普通用户)编译安装与配置mysql数据库并使用shell脚本定时任务方式实现mysql数据库服务随机自动启动
CentOS6.7下使用非root用户(普通用户)编译安装与配置mysql数据库并使用shell脚本定时任务方式实现mysql数据库服务随机自动启动1.关于mysql?MySQL是一个关系型数据库管理 ...
随机推荐
- SpringBoot--多环境部署配置文件
在resources 下创建 application-{profile}.properties 的配置文件,其中profile是任意名字: test:测试环境 prod:线上环境 pre-prod:预 ...
- 使用SSH命令行远程登录运行在CloudFoundry上的应用
当我试图用如下命令行采用SSH远程登录到运行在CloudFoundry环境下的应用时, cf ssh -N -T -L 9229:127.0.0.1:9229 jerry-demo-server 遇到 ...
- 记录一次ABP下载模板的坑
1.拉取ABP官网的模板的最新代码,我的代码结构是这样的 https://aspnetboilerplate.com/Templates 环境安装的部分我就不说明了.node.js npm 等等部分 ...
- Scala语言面向对象
apply1. 面向对象的基本概念: 把数据及对数据的操作方法放在一起,作为一个相互依存的整体-----对象,面向对象的三大特征:封装.多态.继承 2. scala类的定义 · class Emplo ...
- Win7系统不能拖动文件夹的问题怎么解决?
一般情况下,如果我们想要在电脑中移动文件夹,可以采用剪切复制粘贴或者按住鼠标左键拖动的方式来实现,但有些Win7系统用户反映文件夹会出现不能移动的情况,这是怎么回事呢?下面好系统U盘启动就为大家介绍一 ...
- yum list报一些error的组件
1 删除那些无效的参数配置,就不再报错了
- python-----操作路径
分离路径.文件名 org_path = r"F:\temp\test" fpath, fname = os.path.split(org_path) print(fpath,fna ...
- 异步函数Demo
private static async Task<String> IssueClientRequestAsync(string serverName, string message) { ...
- set调用add报错:
Map<String, String> goodsStandMap = new HashMap<>();goodsStandMap.put("key1", ...
- Codeforces 1148 E - Earth Wind and Fire
E - Earth Wind and Fire 思路: 栈模拟 代码: #pragma GCC optimize(2) #pragma GCC optimize(3) #pragma GCC opti ...