多线程+JPA批量insert实现三分钟造100w测试数据多线程+JPA批量insert实现三分钟造100w测试数据
⼀、实现环境、场景
⼆、实现步骤
三、注意事项与实践⼼得
四、遇到的问题
五、参考⽂献
⼀、实现环境、场景
1.⼯程环境
SpringBoot -- 1.5.9.RELEASE
JDK -- 1.8
数据源 -- Druid
数据库 -- mysql
2.实现场景
短时间内批量造数据,⽐如100w条
⼆、实现步骤
1.pom⽂件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="/POM/4.0.0" xmlns:xsi="/2001/XMLSchema-instance" xsi:schemaLocation="maven.apach
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath/>
</parent>
<groupId&le</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.31</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.数据源的配置
2.1 建表语句 -- 此外还要设置⾃增主键
CREATE TABLE `t_emp` (
`Id` int(11) NOT NULL,
`Name` varchar(255) DEFAULT NULL,
`JoinTime` datetime DEFAULT NULL,
PRIMARY KEY (`Id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
2.l配置
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource # 当前数据源操作类型
jpa:
hibernate:
naming:
physical-strategy: org.del.naming.PhysicalNamingStrategyStandardImpl # show-sql: true
properties:
hibernate:
jdbc:
batch_size: 800
batch_versioned_data: true
order_inserts: true
2.3 db.properties配置
druid.driver-class-name=sql.Driver
druid.url=jdbc:mysql://localhost:33306/clouddb01?characterEncoding=utf-8&useSSL=false druid.username=root
druid.password=xxxx
druid.minIdle=10
druid.initialSize=300
druid.maxWait=200
druid.maxActive=300
2.4 数据源配置类
fig;
import com.alibaba.druid.pool.DruidDataSource;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.t.properties.ConfigurationProperties; import t.annotation.Bean;
import t.annotation.Configuration;
mysql下载add produceimport t.annotation.PropertySource;
import org.springframework.stereotype.Component;
import javax.sql.DataSource;
@Configuration
public class DatasourceConfig {
@Autowired
private DataSourceProperties dbProperties;
@Bean
public DataSource druid() {
DruidDataSource dataSource = new DruidDataSource();
dataSource.Username());
dataSource.Password());
dataSource.DriverClassName());
dataSource.Url());
dataSource.MinIdle());
dataSource.InitialSize());
dataSource.MaxWait());
dataSource.setTestWhileIdle(dbProperties.isTestWhileIdle());
dataSource.MaxActive());
return dataSource;
}
}
@Data
@Component
@PropertySource(value = "classpath:db.properties")
@ConfigurationProperties(prefix = "druid")
class DataSourceProperties {
private String driverClassName;
private String url;
private String username;
private String password;
private Integer minIdle;
private Integer initialSize;
private Integer maxWait;
private boolean testWhileIdle;
private Integer maxActive;
}
3.实体类和业务代码
3.1实体类
/**
* @ClassName Employee
* @Description 批量插⼊的员⼯数据
* @Date 2020/2/20 12:30
**/
@Data
@Entity
@ToString
@AllArgsConstructor
@NoArgsConstructor
@Table(name = "t_emp")
public class Employee {
@Id
@Column(name = "Id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column(name = "Name")
private String name;
// 配合JPA hibernate注解, 见l
/
/ org.del.naming.PhysicalNamingStrategyStandardImpl
@Column(name = "JoinTime")
private Date joinTime;
}
3.2 repository接⼝
pository;
ities.Employee;
import org.springframework.pository.JpaRepository;
public interface EmpRepository extends JpaRepository<Employee, Integer> {} 3.3 随机⼯具类
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论