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

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

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

其实很简单 只需要使用默认的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字句
And findByNameAndPwd where name= ? and pwd =?
Or findByNameOrSex where name= ? or sex=?
Is,Equals findById,findByIdEquals where id= ?
Between findByIdBetween where id between ? and ?
LessThan findByIdLessThan where id < ?
LessThanEquals findByIdLessThanEquals where id <= ?
GreaterThan findByIdGreaterThan where id > ?
GreaterThanEquals findByIdGreaterThanEquals where id > = ?
After findByIdAfter where id > ?
Before findByIdBefore where id < ?
IsNull findByNameIsNull where name is null
isNotNull,NotNull findByNameNotNull where name is not null
Like findByNameLike where name like ?
NotLike findByNameNotLike where name not like ?

StartingWith

findByNameStartingWith where name like '?%'
EndingWith findByNameEndingWith where name like '%?'
Containing findByNameContaining where name like '%?%'
OrderBy findByIdOrderByXDesc where id=? order by x desc
Not findByNameNot where name <> ?
In findByIdIn(Collection c) where id in (?)
NotIn findByIdNotIn(Collection c) where id not  in (?)
True

findByAaaTue

where aaa = true
False findByAaaFalse where aaa = false
IgnoreCase findByNameIgnoreCase where UPPER(name)=UPPER(?)

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

List findByAgeIN(Coollection<> c);


你可能感兴趣的:(java,springboot,JPA)