Spring data jpa 的批量查询和批量插入及一些常用操作

本文介绍了一种通过批量插入数据来提高数据库操作效率的方法,并演示了如何利用Java的JPA实现这一过程。此外,还提供了批量查询数据的具体示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

有时候我们需要向数据库插入大量数据,如果一条一条插入会非常慢

所以我们可以考虑批量插入

其实很简单 只需要使用默认的save()方法就可以了

假设现在有一个student实体类  我们需要一次插入整个学区5000名学生的信息

 
package com.chunying.boke.bean;

/**
 * @author chunying
 */
public class Student {

    private Integer studentID;
    private String name;
    private Integer age;

    public Integer getStudentID() {
        return studentID;
    }

    public void setStudentID(Integer studentID) {
        this.studentID = studentID;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}
我实体类什么的没有加注解,实际使用时自己注意下哈 ,我只是写个demo没有运行。

package com.chunying.boke.dao;

import com.chunying.boke.bean.Student;
import org.springframework.data.jpa.repository.JpaRepository;

/**
 * @author chunying
 */
public interface StudentDao extends JpaRepository<Student, Integer> {
}

package com.chunying.boke.JpaTest;

import com.chunying.boke.bean.Student;
import com.chunying.boke.dao.StudentDao;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

/**
 * @author chunying
 */
public class JpaTest {

    @Autowired
    private StudentDao studentDao;

    @Test
    public void fun1() {

        //模拟5000学生数据
        List<Student> originData = new ArrayList<>();

        //批量存储的集合
        List<Student> data = new ArrayList<>();

        //批量存储
        for(Student student : originData) {
            if(data.size() == 300) {
                studentDao.save(data);
                data.clear();
            }

            data.add(student);
        }

        if(!data.isEmpty()) {
            studentDao.save(data);
        }

    }

}

至于批量查询 ,我这里列一个通用JPA的操作对应的方法名

关键字方法命名sql where字句
AndfindByNameAndPwdwhere name= ? and pwd =?
OrfindByNameOrSexwhere name= ? or sex=?
Is,EqualsfindById,findByIdEqualswhere id= ?
BetweenfindByIdBetweenwhere id between ? and ?
LessThanfindByIdLessThanwhere id < ?
LessThanEqualsfindByIdLessThanEqualswhere id <= ?
GreaterThanfindByIdGreaterThanwhere id > ?
GreaterThanEqualsfindByIdGreaterThanEqualswhere id > = ?
AfterfindByIdAfterwhere id > ?
BeforefindByIdBeforewhere id < ?
IsNullfindByNameIsNullwhere name is null
isNotNull,NotNullfindByNameNotNullwhere name is not null
LikefindByNameLikewhere name like ?
NotLikefindByNameNotLikewhere name not like ?

StartingWith

findByNameStartingWithwhere name like '?%'
EndingWithfindByNameEndingWithwhere name like '%?'
ContainingfindByNameContainingwhere name like '%?%'
OrderByfindByIdOrderByXDescwhere id=? order by x desc
NotfindByNameNotwhere name <> ?
InfindByIdIn(Collection<?> c)where id in (?)
NotInfindByIdNotIn(Collection<?> c)where id not  in (?)
True

findByAaaTue

where aaa = true
FalsefindByAaaFalsewhere aaa = false
IgnoreCasefindByNameIgnoreCasewhere UPPER(name)=UPPER(?)

例:比如要批量查询某些年纪的学生

List<Student> findByAgeIN(Coollection<> c);


评论 23
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值