你瞭解Spring Data JPA的批量插入嗎?

實現場景

最近在寫一個有關拼團旅遊的業務,裏面有這樣一個邏輯實現:一個拼團活動表Activity對應多個Travel旅遊表,travel旅遊表要分段存儲前端傳來的List<TravelSchedulingVO>,Travel表需將每一條數據保存入庫,這時候就須要用到批量插入來實現了,因爲項目的orm框架使用的是Spring Data JPA,全部咱們來討論一下JPA的批量插入的實現和原理。css

Spring Data JPA中保存方法已經幫咱們封裝好了,有save(),saveAll(),而saveAll()是用來實現數據的批量插入的,咱們先看看save和savaAll()這兩個方法的源碼:html

save()前端

saveAll()java

從源碼能夠看到,saveAll()批量插入就是循環調用save()方法啊,咱們來寫個簡單的測試插入數據方法試一下:spring

項目信息:sql

UserController

@Controller
public class UserController {

    @Autowired
    UserRepository userRepository;

    @PostMapping("/saveall")
    @ResponseBody
    public String saveAll() {
        long start = System.currentTimeMillis();
        List<User> list = new ArrayList<>();
        for (int i = 10000; i < 10005; i++) {
            User user = new User();
            user.setUsername("這是用戶名測試數據" + i);
            list.add(user);
        }
        userRepository.saveAll(list);
        long end = System.currentTimeMillis();
        long time = end - start;
        System.out.println(LocalDateTime.now() + " 共用時 " + time);
        return "success";
    }
}

DruidConfiguration

/**
 * @Author 林必昭
 * @Date 2019/11/21 11:10
 */

@Configuration
@PropertySource(value = "application.properties")
public class DruidConfiguration {

    @Bean
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource druidDataSource() {
        DruidDataSource druidDataSource = new DruidDataSource();
        return druidDataSource;
    }

    /**
     * 註冊一個StatViewServlet
     * @return
     */
    @Bean
    public ServletRegistrationBean druidStatViewServlet(){
        //org.springframework.boot.context.embedded.ServletRegistrationBean提供類的進行註冊.
        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(),"/druid/*");

        //添加初始化參數:initParams
        //白名單:
        servletRegistrationBean.addInitParameter("allow","127.0.0.1");
        //IP黑名單 (存在共同時,deny優先於allow) : 若是知足deny的話提示:Sorry, you are not permitted to view this page.
        servletRegistrationBean.addInitParameter("deny","192.168.1.73");
        //登陸查看信息的帳號密碼.
        servletRegistrationBean.addInitParameter("loginUsername","root");
        servletRegistrationBean.addInitParameter("loginPassword","admin");
        //是否可以重置數據.
        servletRegistrationBean.addInitParameter("resetEnable","false");
        return servletRegistrationBean;
    }

    /**
     * 註冊一個:filterRegistrationBean
     * @return
     */
    @Bean
    public FilterRegistrationBean druidStatFilter(){

        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter());

        //添加過濾規則.
        filterRegistrationBean.addUrlPatterns("/*");

        //添加不須要忽略的格式信息.
        filterRegistrationBean.addInitParameter("exclusions","*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
        return filterRegistrationBean;
    }
}

運行Appliaction,使用IDEA Http Client測試一下saveAll方法,配置打開了Hibernate顯示sql日誌,看一下輸出結果::數據庫

看日誌,JPA是先把全部的數據全查出來了,若是數據庫有就更新,沒有就新增。咱們訪問 http://localhost:8080/druid/login.html 輸入帳戶名root,密碼admin登陸,看一下阿里Druid的SQL監控:app

能夠看到咱們插入了5條數據,執行了7次sql操做,由於咱們spring.jpa.hibernate.ddl-auto=create-drop,JPA會幫咱們自動建立和刪除庫,好像沒有什麼問題。。。框架

相關文章
相關標籤/搜索