zoukankan      html  css  js  c++  java
  • [Java Spring Data] JapRepository

    All features of CrudRepository plus:

    • void flush();
    • saveAndFlush()
    • delteInBatch()
    • delteAllInBatch()
    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:

    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("
    *************3 Departments*************");
            departmentRepository.findAll().forEach(System.out::println);
    
            departmentRepository.deleteInBatch(
                    departmentRepository.findAll().subList(0,1));
    
            System.out.println("
    *************1 Less Departments*************");
            departmentRepository.findAll().forEach(System.out::println);
            departmentRepository.deleteAllInBatch();
            System.out.println("
    *************Zero Departments*************");
            departmentRepository.findAll().forEach(System.out::println);
    
        }
        @Before
        @After
        public void banner() {
            System.out.println("
    
    -------------------------------------------------" +
                    "-------------------------------------
    ");
        }
    }
    

      

  • 相关阅读:
    Bean管理学习笔记
    Spring核心概念学习笔记
    Spring主要用到两种设计模式
    C# 值类型和引用类型等值判断
    嵌入式Linux之gdb配置和使用
    嵌入式Linux之telnet
    构建嵌入式Linux交叉编译工具链
    Linux动态库和静态库
    VxWorks BSP开发入门
    buildroot
  • 原文地址:https://www.cnblogs.com/Answer1215/p/14147638.html
Copyright © 2011-2022 走看看