JpaRepository(JPA)

JpaRepository

JpaRepository是SpringBoot Data JPA 提供的基础接口,具备基本的CRUD以及分页功能

save

save(保存一个实体类)
1 /*<S extends T> S save(S entity);*/ 2 3 @GetMapping("/Student/save") 4 public Student CRUD_save(){ 5 //保存一个Student 6 Student student = new Student("妲己","66"); 7 Student res = StudentDo.save(Student); 8 return res; 9 }

saveAll

 saveAll(保存提供的所有实体类)
1 /*<S extents T> Iterable<S> saveAll(Iterable<S> entities)*/ 2 @GetMapping("/Student/saveAll") 3 public List<Student> Student_saveAll() { 4 5 // 保存指定集合的实体 6 List<Student> studentList = new ArrayList<>(); 7 studentList .add(new Customer("幻波", 21)); 8 studentList .add(new Customer("琳妮", 21)); 9 List<Student> res = StudentDo.saveAll(StudentList); 10 return res; 11 }

findbyid

findbyid(根据id查询对应实体类)

/*optional<T> findById(ID id)*/ @GetMapping("/Student/findByid") public Student Student_findById(){ //根据id查对应实体类 Optional<Student> student = StudentDo.findById(); if(student.isPeresent()){ return Student.get(); } return null; }

exeistsById

exeistsById(根据id查询对应实体类是否存在)

/*Boolean existsById(ID id)*/ @GetMapping("/Student/existsByid") public Blooean Student_findById(){ //根据id查对应实体类 return StudentDo.existsById(); }

findAll

findAll(查询所有实体类)

/*Iterable<T> findAll*/ @GetMapping(/Student/findAll) public List<Student> Student_findAll(){ List<Student> list = StudentDo.findAll(); return list; }

findAllById

findAllById(根据给的id查询实体类集合)

/*Iterable<T> findAllById(Iterable<ID> ids)*/
@GetMapping(/Student/findAllById) public List<Student> findAllById(String ids){ List idList=ids.split(",").asList(); List<Student> studentList = idList.findAllById(idlist); return stduentList; }

count

count(统计现存实体类个数)

/*long count()*/ @GetMapping("/Student/Count") public Long getCount(){ //统计现存的实体个数 return studentDo.count(); }

deleteById

/*根据id删除对用实体*/
@GetMapping("/Studebt/deleteById")
public void deleteStudent(Sring id){
  StudentDo.deleteById(666);  
}

delete  

/*void delete(T entity)删除给定实体类*/

@GetMapping("/Student/deleteByEntity")
public void deleteByEntity(){

  Student student = new Student("laogao","23");
  StudentDo.delete(student)    
}

deleteAll

deleteAll(删除给定实体类集合)

/*deleteAll(Iterable<? extends T> entities)*/ @GetMapping("/Student/deleteAll") public void Student_deleteAll(){ List<Student> studentList = new ArrayList<>(); Student Student01=new Student("A01","20"); Student Student02=new Student("A02","30"); studentList.add(student01); studentList.add(student02); StudentDo.deleteAll(studentList) }

findAll(Sort sort) 

findAll(查询All并根据sort排序)
/*Iterable<T> findAll(Short short) */
@GetMapping("/student/studentFindAll")
Public List<Student> findAllStudent(){
  Sory sort= new Sort(Sott.Dirextion.DESC,"age");
  List<Student> list = StduentDo.fidnAll(sort);
    returun list;
}

findAll(Pageanle pageable)

findAll(过滤查询)

  @GetMapping("/Student/findAll)
    public void findAll_pageable() {

        // 分页查询
        // PageRequest.of 的第一个参数表示第几页(注意:第一页从序号0开始),第二个参数表示每页的大小
        Pageable pageable = PageRequest.of(1, 5); //查第二页
        Page<Customer> page = customerRepository.findAll(pageable);

        System.out.println("查询总页数:" + page.getTotalPages());
        System.out.println("查询总记录数:" + page.getTotalElements());
        System.out.println("查询当前第几页:" + (page.getNumber() + 1));
        System.out.println("查询当前页面的集合:" + page.getContent());
        System.out.println("查询当前页面的记录数:" + page.getNumberOfElements());
    }

  

*编辑删除注意

-- @Transactional 开启事务
-- @Modifying 识别执行更新操作
-- @Query() SQL语句

原生查询

@Query(value = "select name,author,price from Book b where b.name like %:name%")
List<Book> findByNameMatch(@Param("name") String name);

编辑

@Transactional
@Modifying
@Query("update ShopCoupon sc set sc.deleted = true where sc.id in :ids")
public void deleteByIds(@Param(value = "ids") List<String> ids);

  


三个主注解缺一不可!!

 

 

posted @ 2022-08-30 15:39  这里那里  阅读(815)  评论(0)    收藏  举报
Live2D