環境Spring boot
1、JDBC(JdbcTemplate
)
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
Service
@Autowired
private JdbcTemplate jdbcTemplate;
public void jdbc(List<String> list){
int[] updatedCountArray=jdbcTemplate.batchUpdate("INSERT INTO customer (name) VALUES (?);", new BatchPreparedStatementSetter() {
@Override
public void setValues(PreparedStatement preparedStatement, int i) throws SQLException {
preparedStatement.setString(1,list.get(i));
}
@Override
public int getBatchSize() {
return list.size();
}
});
}
2、網上最常見的JPA----entityManager批量操作方法
Entity
package net.xjdsz.model;
import javax.persistence.*;
/**
* Created by dingshuo on 2017/6/23.
*/
@Entity
@Table(name = "customer", schema = "test", catalog = "")
public class CustomerEntity {
private int id;
private String name;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", nullable = false)
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Basic
@Column(name = "name", nullable = true, length = 100)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
CustomerEntity that = (CustomerEntity) o;
if (id != that.id) return false;
if (name != null ? !name.equals(that.name) : that.name != null) return false;
return true;
}
@Override
public int hashCode() {
int result = id;
result = 31 * result + (name != null ? name.hashCode() : 0);
return result;
}
}
Service
private EntityManager em;
@PersistenceContext(name = "EntityManagerFactory")
public void SetEntityManager(EntityManager em) {
this.em = em;
}
@Transactional
public void saveBatch(List<CustomerEntity> list) {
for (int i = 0; i < 10000; i++) {
em.persist(list.get(i));
if (i % 1000 == 0) {
em.flush();
em.clear();
}
}
}
3、Jpa---Repository循環寫入
Repository
package net.xjdsz.dao;
import net.xjdsz.model.CustomerEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
/**
* Created by dingshuo on 2017/6/23.
*/
@Repository
public interface CustomerRepository extends JpaRepository<CustomerEntity,Integer> {
}
4、Jpa--
Repository批量寫入
Service
@Transactional
public void saveBatchJpa(List<CustomerEntity> list) {
repository.save(list);
}
實驗代碼:
用的是Spring boot,所以開了一個RestController去做實驗
package net.xjdsz;
import net.xjdsz.dao.CustomerRepository;
import net.xjdsz.dao.TestService;
import net.xjdsz.model.CustomerEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List;
/**
* Created by dingshuo on 2017/6/23.
*/
@RestController
public class TestController {
@Autowired
TestService service;//測試用的Service類
@Autowired
CustomerRepository repository; //實體Repository接口
@GetMapping(value = "/test")
public void test(){
List<String> list=new ArrayList<>(); //給jdbctemplate用的集合
List<CustomerEntity> customerEntityList=new ArrayList<>();//給jpa用的集合
for(int i=0;i<10000;i++){
list.add("學生"+i);
CustomerEntity customerEntity=new CustomerEntity();
customerEntity.setName("學生"+i);
customerEntityList.add(customerEntity);
}
//1.jdbc
long startTime=System.currentTimeMillis(); //獲取開始時間
service.jdbc(list);
long endTime=System.currentTimeMillis(); //獲取結束時間
System.out.println("jdbc程序運行時間: "+(endTime-startTime)+"ms");
//2.jpa-em
long startTime1=System.currentTimeMillis(); //獲取開始時間
service.saveBatch(customerEntityList);
long endTime1=System.currentTimeMillis(); //獲取結束時間
System.out.println("JPA-EM程序運行時間: "+(endTime1-startTime1)+"ms");
//3.jpa-循環
long startTime2=System.currentTimeMillis(); //獲取開始時間
for(int i=0;i<customerEntityList.size();i++){
repository.save(customerEntityList.get(i));
}
long endTime2=System.currentTimeMillis(); //獲取結束時間
System.out.println("JPA-循環程序運行時間: "+(endTime2-startTime2)+"ms");
//4.jpa-集合
long startTime3=System.currentTimeMillis(); //獲取開始時間
repository.save(customerEntityList);
long endTime3=System.currentTimeMillis(); //獲取結束時間
System.out.println("JPA-集合程序運行時間: "+(endTime3-startTime3)+"ms");
}
}
實驗結果
jdbc程序運行時間: 878ms
JPA-EM程序運行時間: 2018ms
JPA-循環程序運行時間: 21915ms
JPA-集合程序運行時間: 2373ms
結論就是如果追求極致的性能(批量操作速度),優選JDBC。EM和JPA直接操作集合沒有太大的性能區別,這對於新接觸Spring JPA(比如我)的人來說,不比糾結有時候沒法注入EM,直接使用JPA操作集合即可。
JPA的循環造作相當於對單條insert重復了10000遍,自然最慢,也不推薦了。
如下是JPA操作集合的代碼,可以看出起始內部也是用的em,所以可以放心大膽的用了。
@Transactional
public <S extends T> List<S> save(Iterable<S> entities) {
List<S> result = new ArrayList<S>();
if (entities == null) {
return result;
}
for (S entity : entities) {
result.add(save(entity));
}
return result;
}
/*
* (non-Javadoc)
* @see org.springframework.data.repository.CrudRepository#save(java.lang.Object)
*/
@Transactional
public <S extends T> S save(S entity) {
if (entityInformation.isNew(entity)) {
em.persist(entity);
return entity;
} else {
return em.merge(entity);
}
}