[Java Spring Data] JapRepository

All features of CrudRepository plus:

  • void flush();
  • saveAndFlush()
  • delteInBatch()
  • delteAllInBatch()
1
2
3
4
5
6
7
8
9
10
11
12
13
package com.example.university.repo;
 
import com.example.university.domain.Department;
import org.springframework.data.jpa.repository.JpaRepository;
 
/**
 * DataSource Management for the Departments at the University.
 *
 * Created by maryellenbowman
 */
public interface DepartmentRepository extends JpaRepository<Department, Integer> {
 
}

  

test:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package com.example.university;
 
import com.example.university.domain.Department;
import com.example.university.repo.DepartmentRepository;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
 
 
/**
 * Demonstrate JPA Repository methods with DepartmentRepository
 * <p>
 * Created by maryellenbowman
 */
 
@RunWith(SpringRunner.class)
@SpringBootTest
public class JpaRepositoryDemo {
    @Autowired
    private DepartmentRepository departmentRepository;
    /**
     * Exercise JPA Repository methods.
     */
    @Test
    public void runJpaRepositoryMethods() {
 
        departmentRepository.save(new Department("Humanities"));
        departmentRepository.flush();
 
        departmentRepository.saveAndFlush(new Department("Fine Arts"));
 
        departmentRepository.save(new Department("Social Science"));
 
        System.out.println("\n*************3 Departments*************");
        departmentRepository.findAll().forEach(System.out::println);
 
        departmentRepository.deleteInBatch(
                departmentRepository.findAll().subList(0,1));
 
        System.out.println("\n*************1 Less Departments*************");
        departmentRepository.findAll().forEach(System.out::println);
        departmentRepository.deleteAllInBatch();
        System.out.println("\n*************Zero Departments*************");
        departmentRepository.findAll().forEach(System.out::println);
 
    }
    @Before
    @After
    public void banner() {
        System.out.println("\n\n-------------------------------------------------" +
                "-------------------------------------\n");
    }
}

  

posted @   Zhentiw  阅读(111)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2019-12-17 [Algorithm] 202. Happy Number
2019-12-17 [Javascript] Creating an Iterator from an Array
2018-12-17 [Algorithm] Write a Depth First Search Algorithm for Graphs in JavaScript
2018-12-17 [Algorithms] Refactor a Loop in JavaScript to Use Recursion
2018-12-17 [Bash] Understand and Use Functions in Bash
2018-12-17 [Javascript] Await a JavaScript Promise in an async Function with the await Operator
2017-12-17 [Python] Array Attributes of Numpy lib
点击右上角即可分享
微信分享提示