JPA批量操作及性能比对
假设需要批量插入10000条实体数据至数据库。如下是各个操作方法及耗时
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jdbc</artifactId></dependency>
@Autowiredprivate JdbcTemplate jdbcTemplate;public void jdbc(List<String> list){int[] updatedCountArray=jdbcTemplate.batchUpdate("INSERT INTO customer (name) VALUES (?);", new BatchPreparedStatementSetter() {@Overridepublic void setValues(PreparedStatement preparedStatement, int i) throws SQLException {preparedStatement.setString(1,list.get(i));}@Overridepublic int getBatchSize() {return list.size();}});}
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;}@Overridepublic 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;}@Overridepublic int hashCode() {int result = id;result = 31 * result + (name != null ? name.hashCode() : 0);return result;}}
private EntityManager em;@PersistenceContext(name = "EntityManagerFactory")public void SetEntityManager(EntityManager em) {this.em = em;}@Transactionalpublic 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();}}}
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.*/@Repositorypublic interface CustomerRepository extends JpaRepository<CustomerEntity,Integer> {}
@Transactionalpublic void saveBatchJpa(List<CustomerEntity> list) {repository.save(list);}
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.*/@RestControllerpublic class TestController {@AutowiredTestService service;//测试用的Service类@AutowiredCustomerRepository 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.jdbclong startTime=System.currentTimeMillis(); //获取开始时间service.jdbc(list);long endTime=System.currentTimeMillis(); //获取结束时间System.out.println("jdbc程序运行时间: "+(endTime-startTime)+"ms");//2.jpa-emlong 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程序运行时间: 878msJPA-EM程序运行时间: 2018msJPA-循环程序运行时间: 21915msJPA-集合程序运行时间: 2373ms
@Transactionalpublic <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)*/@Transactionalpublic <S extends T> S save(S entity) {if (entityInformation.isNew(entity)) {em.persist(entity);return entity;} else {return em.merge(entity);}}
JPA批量操作及性能比对的更多相关文章
- jpa batch批量操作save和persist比较
1.网上最常见的JPA----entityManager批量操作方法 private EntityManager em; @PersistenceContext(name = "Entity ...
- 深入浅出学Spring Data JPA
第一章:Spring Data JPA入门 Spring Data是什么 Spring Data是一个用于简化数据库访问,并支持云服务的开源框架.其主要目标是使得对数据的访问变得方便快捷,并支持map ...
- JPA基础
目录 目录 1 一.JPA基础 2 1.1 JPA基础 2 1.2JPA开发过程 3 1.3 实体的生命周期及实体管理器常用方法 4 二.环境搭建 5 2.1 添加JPA支持 6 2.2 添加配置文件 ...
- Spring Data Jpa配置
Spring Data JPA提供的接口,也是Spring Data JPA的核心概念: 1:Repository:最顶层的接口,是一个空的接口,目的是为了统一所有Repository的类型,且能让组 ...
- JPA && Spring Data && Spring Data JPA
1.JPA Java Persistence API,用于对象持久化的一组API,JPA本身是一组规范,让开发者用同一种方式访问不同的ORM框架.其实也就是java实体对象和关系型数据库建立起映射关 ...
- JPA学习笔记
一.JPA基础1.1 JPA基础JPA: java persistence api 支持XML.JDK5.0注解俩种元数据的形式,是SUN公司引入的JPA ORM规范 元数据:对象和表之间的映射关系 ...
- Spring Boot 系列教程2-Data JPA
Spring Data JPA 用来简化创建 JPA 数据访问层和跨存储的持久层功能. 官网文档连接 http://docs.spring.io/spring-data/jpa/docs/curren ...
- Spring Data JPA 入门Demo
什么是JPA呢? 其实JPA可以说是一种规范,是java5.0之后提出来的用于持久化的一套规范:它不是任何一种ORM框架,在我看来,是现有ORM框架在这个规范下去实现持久层. 它的出现是为了简化现有的 ...
- Spring Data JPA 初体验
一,JPA相关的概念 JPA概述 全称是:JavaPersistence API.是SUN公司推出的一套基于ORM的规范. Hibernate框架中提供了JPA的实现. JPA通过JDK 5.0注解或 ...
随机推荐
- java 4对象群体的组织
两个接口 collecion接口 元素构成的元素的群体 map接口 键值对组成的群体 Array类 Vector ArrayList 在数组上构建的类 Java集合框架介绍 集成过得数据结构 查询方法 ...
- VisualTreeHelper使用——使用BFS实现高效率的视觉对象搜索
BFS,即广度优先搜索,是一种典型的图论算法.BFS算法与DFS(深度优先搜索)算法相对应,都是寻找图论中寻路的常用算法,两者的实现各有优点. 其中DFS算法常用递归实现,也就是常见的一条路找到黑再找 ...
- SDUT-3398_数据结构实验之排序一:一趟快排
数据结构实验之排序一:一趟快排 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 给定N个长整型范围内的整数,要求输出以给 ...
- MSSQL2008 数据压缩方法
数据压缩功能使得SOL Server 2008允许在表.索引和分区中执行数据压缩,这样不仅可以节省磁盘空间,而且允许更多数据置入RAM中,从而提升数据库查询的性能. 1.启用行压缩 如果我们要在指定的 ...
- JavaScript--函数对象的属性caller与callee
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 纯CSS3绘制的黑色图标按钮组合
在线演示 本地下载
- php刷新当前页面,js刷新页面
echo "<script language=JavaScript> location.replace(location.href);</script>"; ...
- Mac终端打开AndroidStudio已创建模拟器
目的 偶尔我们只是想运行模拟器,并不想打开AndroidStudio,这时我们可以从终端找到emulator,通过emulator来启动指定名称的模拟器 步骤 1.找到emulator所在位置 fin ...
- Java练习 SDUT-3848_Shift Dot
Shift Dot Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 给出平面直角坐标系中的一点,并顺序给出n个向量,求该 ...
- vue插件大全
一.UI组件及框架 element - 饿了么出品的Vue2的web UI工具套件 mint-ui - Vue 2的移动UI元素 iview - 基于 Vuejs 的开源 UI 组件库 Keen-UI ...