我使用以下代码在数据库中插入大量数据。
问题
1)有没有什么方法可以优化插入查询,以便可以像
一样应用查询插入x值(id,描述,位置),(id,description,location)....
其中id,description和location可以动态获取
2)虽然我尝试使用@Query Annotation但无法应用插入查询并在上述查询中获取动态输入
任何人都可以指导我如何解决这个问题,或者是否存在更好的方法。提前谢谢。
我实施的解决方案
1)在这个例子中,我无法实现插入查询,我尝试了不同的插入方式来插入。
项目实体
Entity
@Table(name = "Item")
public class Item {
private static AtomicLong lastId = new AtomicLong();
@Id
private Long id=lastId.incrementAndGet();
@Column(name = "description")
private String description;
@Column(name = "location")
private String location;
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
BulkInsert EndPoint
@PostMapping("/bulkInsert")
ApiResponse<List<Item>> post() {
List<Item> itemList = new ArrayList<>();
for (int i = 0; i < 2000; i++) {
Item item = new Item();
item.setDescription("JalajDesc" + i);
item.setLocation("Jalaj Location" + i);
itemList.add(item);
}
return ApiResponse.success().object(bulkImporterRepository1.bulkSave(itemList));
}
BulkImporterRepository1
@Repository
public class BulkImporterRepository1 extends SimpleJpaRepository<Item, Long> {
@PersistenceContext
private EntityManager entityManager;
@Value("${spring.jpa.properties.hibernate.jdbc.batch_size}")
private int batchSize;
public BulkImporterRepository1(EntityManager entityManager) {
super(Item.class, entityManager);
this.entityManager = entityManager;
}
@Transactional
public <T extends Item> List<Item> bulkSave(List<Item> entities) {
final List<Item> savedEntities = new ArrayList<Item>(entities.size());
int i = 0;
for (Item t : entities) {
savedEntities.add(persistOrMerge(t));
i++;
if (i % batchSize == 0) {
// Flush a batch of inserts and release memory.
entityManager.flush();
entityManager.clear();
}
}
return savedEntities;
}
Hibernate日志
2018-03-04 18:20:58.839 DEBUG 10703 --- [nio-6005-exec-1] org.hibernate.SQL
: select item0_.id as id1_0_0_, item0_.description as descript2_0_0_, item0_.location as location3_0_0_ from item item0_ where item0_.id=?
2018-03-04 18:20:58.839 DEBUG 10703 --- [nio-6005-exec-1] org.hibernate.SQL : select item0_.id as id1_0_0_, item0_.description as descript2_0_0_, item0_.location as location3_0_0_ from item item0_ where item0_.id=?
2018-03-04 18:20:58.839 DEBUG 10703 --- [nio-6005-exec-1] org.hibernate.SQL : select item0_.id as id1_0_0_, item0_.description as descript2_0_0_, item0_.location as location3_0_0_ from item item0_ where item0_.id=?
2018-03-04 18:20:58.839 DEBUG 10703 --- [nio-6005-exec-1] org.hibernate.SQL : select item0_.id as id1_0_0_, item0_.description as descript2_0_0_, item0_.location as location3_0_0_ from item item0_ where item0_.id=?
2018-03-04 18:20:58.839 DEBUG 10703 --- [nio-6005-exec-1] org.hibernate.SQL : select item0_.id as id1_0_0_, item0_.description as descript2_0_0_, item0_.location as location3_0_0_ from item item0_ where item0_.id=?
2018-03-04 18:20:58.840 DEBUG 10703 --- [nio-6005-exec-1] org.hibernate.SQL : insert into item (description, location, id) values (?, ?, ?)
2018-03-04 18:20:58.840 DEBUG 10703 --- [nio-6005-exec-1] org.hibernate.SQL : insert into item (description, location, id) values (?, ?, ?)
2018-03-04 18:20:58.840 DEBUG 10703 --- [nio-6005-exec-1] org.hibernate.SQL : insert into item (description, location, id) values (?, ?, ?)
2018-03-04 18:20:58.840 DEBUG 10703 --- [nio-6005-exec-1] org.hibernate.SQL : insert into item (description, location, id) values (?, ?, ?)
2018-03-04 18:20:58.840 DEBUG 10703 --- [nio-6005-exec-1] org.hibernate.SQL : insert into item (description, location, id) values (?, ?, ?)
会话指标
2018-03-04 18:20:58.918 INFO 10703 --- [nio-6005-exec-1] i.StatisticalLoggingSessionEventListener : Session Metrics {
447529 nanoseconds spent acquiring 1 JDBC connections;
0 nanoseconds spent releasing 0 JDBC connections;
72032184 nanoseconds spent preparing 2040 JDBC statements;
241834982 nanoseconds spent executing 2000 JDBC statements;
185823486 nanoseconds spent executing 40 JDBC batches;
0 nanoseconds spent performing 0 L2C puts;
0 nanoseconds spent performing 0 L2C hits;
0 nanoseconds spent performing 0 L2C misses;
307745133 nanoseconds spent executing 40 flushes (flushing a total of 2000 entities and 0 collections);
0 nanoseconds spent executing 0 partial-flushes (flushing a total of 0 entities and 0 collections)
** Application.propeeties **
休眠
spring.jpa.hibernate.naming-strategy=org.hibernate.cfg.DefaultNamingStrategy
spring.jpa.hibernate.ddl-auto=update
连接
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=x
spring.datasource.password=x
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
spring.datasource.name=test
池
spring.datasource.initial-size=10
spring.datasource.max-active=50
spring.datasource.min-idle=5
spring.datasource.max-idle=5
spring.datasource.test-on-borrow=true
spring.datasource.validation-query=select 1;
spring.datasource.time-between-eviction-runs-millis=60000
spring.datasource.min-evictable-idle-time-millis=300000
spring.jpa.properties.hibernate.jdbc.batch_size=50
spring.jpa.properties.hibernate.order_inserts=true
spring.jpa.properties.hibernate.order_updates=true
spring.jpa.properties.hibernate.generate_statistics=true