当前位置: 首页 > 工具软件 > jpa-spec > 使用案例 >

Spring data JPARepository的一些方法&&jpa-spec插件

习华灿
2023-12-01

普通的CRUD

public class EmployeeRopositoryTest extends BaseTest {

    @Autowired
    private EmployeeRepository employeeRepository;

    /**
     * 查询所有
     */
    @Test
    public void testfindAll(){
        List<Employee> all = employeeRepository.findAll();
        for (Employee employee : all) {
            System.out.println(employee);
        }
    }

    /**
     * 查询单个
     */
    @Test
    public void testfindOne(){
        Employee one = employeeRepository.findOne(2L);
        System.out.println(one);
    }

    /**
     * 根据id删除数据
     */
    @Test
    public void testdel(){
        employeeRepository.delete(272L);
    }

    /**
     * 保存数据
     */
    @Test
    public void testSave(){
        Employee employee = new Employee();
        employee.setUsername("弋弋");
        employee.setPassword("123456");
        employee.setEmail("2385@qq.com");
        employee.setAge(22);
        employeeRepository.save(employee);

    }

    /**
     * 修改数据 用的都是save 如果有id就是修改 没有就是新增
     */
    @Test
    public void testUpdate(){
        Employee employee = new Employee();
        employee.setUsername("这是修改后的值");
        employee.setPassword("1323");
        employee.setEmail("123");
        employee.setAge(0);
        employee.setId(273L);
        employeeRepository.save(employee);

    }

    /**
     * 批量删除
     * JpaRepository中的批量删除与CRUDRepository中的批量删除的区别
     */
    @Test
    public void testBathchRemove(){
        List<Employee> list = new ArrayList<>();
        Employee one = employeeRepository.findOne(265L);
        Employee one1 = employeeRepository.findOne(264L);

        list.add(one);
        list.add(one1);

        //JPARepository
        //产生的sql语句 delete from employee where id=? or id=?
//        employeeRepository.deleteInBatch(list);

        //CRUDRepository
        //产生的sql语句
        /**
         * select employee0_.id as id1_0_0_, employee0_.age as age2_0_0_, employee0_.email as email3_0_0_, employee0_.password as password4_0_0_, employee0_.username as username5_0_0_ from employee employee0_ where employee0_.id=?
         * select employee0_.id as id1_0_0_, employee0_.age as age2_0_0_, employee0_.email as email3_0_0_, employee0_.password as password4_0_0_, employee0_.username as username5_0_0_ from employee employee0_ where employee0_.id=?
         * delete from employee where id=?
         * delete from employee where id=?
         * 会产生4条sql语句,2条循环查询语句,2条循环删除语句,性能超级低,建议使用子类的方法
         */
        employeeRepository.delete(list);
    }

分页排序功能

    /**
     * 查询并排序
     */
    @Test
    public void testfindAllAndSort(){
        Sort sort = new Sort(Sort.Direction.DESC,"age","username");
        List<Employee> all = employeeRepository.findAll(sort);
        for (Employee employee : all) {
            System.out.println(employee);
        }
    }


    /**
     * 分页查询
     */
    @Test
    public void testPage(){
        Pageable page = new PageRequest(1, 10);
        Page<Employee> all = employeeRepository.findAll(page);

        for (Employee employee : all) {
            System.out.println(employee);
        }

        System.out.println(all.getTotalElements());
        System.out.println(all.getTotalPages());
        System.out.println(all.getContent());
        System.out.println(all.getSize());
        System.out.println(all.getNumber());
        System.out.println(all.getNumberOfElements());

    }

    /**
     * 分页且排序
     */
    @Test
    public void testPageAndOrder(){
        Sort age = new Sort(Sort.Direction.DESC, "age");
        Pageable able = new PageRequest(0, 5, age);
        Page<Employee> all = employeeRepository.findAll(able);
        for (Employee employee : all) {
            System.out.println(employee);

        }
    }

根据条件进行查询

    /**
     * 条件查询 根据名字查询
     */
    @Test
    public void queryByName(){
        List<Employee> admin = employeeRepository.findByUsername("admin");
        for (Employee employee : admin) {
            System.out.println(employee);
        }
    }

    /**
     * 根据名字 模糊查询
     */
    @Test
    public void queryByNameLike(){
        List<Employee> admin = employeeRepository.findByUsernameLike("%admin%");
        for (Employee employee : admin) {
            System.out.println(employee);
        }
    }


    //Query 注解查询
    @Test
    public void testQuery01(){
        List<Employee> employees = employeeRepository.loadByUsername1("%admin%", 26);
        for (Employee employee : employees) {
            System.out.println(employee);
        }
    }

    @Test
    public void testQuery02(){
        List<Employee> employees = employeeRepository.loadByUsername2("%admin%", 26);
        for (Employee employee : employees) {
            System.out.println(employee);
        }
    }

    @Test
    public void testQuery03(){
        Long count = employeeRepository.getCount();
        System.out.println(count);
    }

JpaSpecificationExecutor

    /**
     * JpaSpecificationExecutor(了解)
     */
    @Test
    public void testJpaSpecificationExecutor1(){
        List<Employee> all = employeeRepository.findAll(new Specification<Employee>() {

            @Override
            public Predicate toPredicate(Root<Employee> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                Path username = root.get("username");
                Path age = root.get("age");

                Predicate like = criteriaBuilder.like(username, "%admin%");
                Predicate ge = criteriaBuilder.ge(age, 26);
                Predicate and = criteriaBuilder.or(like, ge);

                return and;
            }
        });

        for (Employee employee : all) {
            System.out.println(employee);
        }
    }

jpa-spec插件

    @Test
    public void testSpec(){
        Specification<Employee> build = Specifications.<Employee>and()
                .like("username", "%admin%")
                .ge("age", 26)
                .build();
        List<Employee> all = employeeRepository.findAll(build);
        for (Employee employee : all) {
            System.out.println(employee);
        }
    }

    /**
     * 同时有and 和 or
     */
    @Test
    public void testSpec02(){
        Specification<Employee> build = Specifications.<Employee>and()
                .like("username", "%admin%")
                .ge("age", 26)
                .build();

        Specification<Employee> email = Specifications.<Employee>or()
                .eq("email", "amdin1@itsource.cn")
                .predicate(build)
                .build();

        List<Employee> all = employeeRepository.findAll(email);
        for (Employee employee : all) {
            System.out.println(employee);
        }
    }

    /**
     * 分页排序条件查询
     */
    @Test
    public void testSpec03(){
        Specification<Employee> build = Specifications.<Employee>and()
                .like("username", "%admin%")
                .ge("age", 26)
                .build();

        Specification<Employee> email = Specifications.<Employee>or()
                .eq("email", "amdin1@itsource.cn")
                .predicate(build)
                .build();

        Sort age = new Sort(Sort.Direction.DESC, "age");
        Pageable pageable = new PageRequest(0, 5,age);
        Page<Employee> all = employeeRepository.findAll(email, pageable);
        for (Employee employee : all) {
            System.out.println(employee);
        }
    }

    /**
     * jpa-spec插件 高级查询
     */
    @Test
    public void testJpaSpecQuery1(){
        EmployeeQuery employeeQuery = new EmployeeQuery();
        employeeQuery.setUsername("admin");
        employeeQuery.setEmail("admin@itsource.com");

        List<Employee> all = employeeRepository.findAll(employeeQuery.createSpec());

        for (Employee employee : all) {
            System.out.println(employee);
        }
    }

    @Test
    public void testJpaSpecPage1(){
        EmployeeQuery employeeQuery = new EmployeeQuery();
        employeeQuery.setUsername("admin");
        employeeQuery.setEmail("admin@itsource.com");
        employeeQuery.setOrderByName("age");
        employeeQuery.setOrder("desc");

        Pageable pageable = new PageRequest(employeeQuery.getBegin(), employeeQuery.getRows(),
                employeeQuery.createSort());
        Page<Employee> all = employeeRepository.findAll(employeeQuery.createSpec(), pageable);
        System.out.println(all);
        for (Employee employee : all) {
            System.out.println(employee);
        }
    }
}

 类似资料: