Spring Data 关于Repository的介绍 和 查询方法的规则定义

Repository类的定义:

public interface Repository<T, ID extends Serializable> {

}

 

1)Repository是一个空接口,标记接口
没有包含方法声明的接口

2)如果我们定义的接口EmployeeRepository extends Repository

如果我们自己的接口没有extends Repository,运行时会报错:
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springdata.repository.EmployeeRepository' available

 

3) 添加注解能到达到不用extends Repository的功能
@RepositoryDefinition(domainClass = Employee.class, idClass = Integer.class)

我们写个例子

  

Spring Data 关于Repository的介绍 和 查询方法的规则定义

package org.springdata.repository;

import org.springdata.domain.Employee;
import org.springframework.data.repository.Repository;
import org.springframework.data.repository.RepositoryDefinition;

/***
 * domainClass  表示哪个实体类
 * idClass 标识id
 */
@RepositoryDefinition(domainClass = Employee.class, idClass = Integer.class)
public interface EmployeeRepository /*extends Repository<Employee,Integer>*/ {
    /**
     * 根据名字找员工
     * desc  
     * @param name
     * @return
     */
    public Employee findByName(String name);
}

Spring Data 关于Repository的介绍 和 查询方法的规则定义

以上 咱们通过一个注解 来什么接口 RepositoryDefinition

 

 

 

 

 

 

 

 

 

有句话这样说  欲练神功  挥刀自宫  请亲们先回到第一个  从Spring data 介绍 开始看  搭好环境 跟着步伐一块走

  Spring Data 的方法必须严格按照它的规范进行编写,如果写错了就不行

下面是网上找的一张图:仔细看  

  Spring Data 关于Repository的介绍 和 查询方法的规则定义

 

咱们先拿几个方法来做个示例

  在这之前  先往数据表插入一些数据 

    insert into employee(name,age) values('wangwu',12); ..... 你们自己插写数据

Spring Data 关于Repository的介绍 和 查询方法的规则定义 先贴下我的数据

 

继续 基于原先的代码进行修改  EmployeeRepository.java   第二个方法  我们所演示的

本按理接口对应的方法------>findByNameIsStartingWithAndAgeLessThan

Spring Data 关于Repository的介绍 和 查询方法的规则定义

package org.springdata.repository;

import org.springdata.domain.Employee;
import org.springframework.data.repository.Repository;
import org.springframework.data.repository.RepositoryDefinition;

import java.util.List;

/***
 *
 */
@RepositoryDefinition(domainClass = Employee.class, idClass = Integer.class)
public interface EmployeeRepository /*extends Repository<Employee,Integer>*/ {
    /**
     * 根据名字找员工
     * desc  
     * @param name
     * @return
     */
    public Employee findByName(String name);


    // name 以什么开始 IsStartingWith 并且 年龄<多少岁的员工   还有很多方法  我就不一一演示了  比如已wang结尾的 就是findByNameIsEndingWith  
    public List<Employee> findByNameIsStartingWithAndAgeLessThan(String name, Integer gae);
  
}

Spring Data 关于Repository的介绍 和 查询方法的规则定义

测试类还是基于原先的 修改   本案例测试类方法---------->testfindByNameIsStartingWithAndAgeLessThan   查询name已wang开始的并且年龄小于50岁的

Spring Data 关于Repository的介绍 和 查询方法的规则定义

package org.springdata;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springdata.domain.Employee;
import org.springdata.repository.EmployeeRepository;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.List;

/**
 * 测试类
 */
public class SpringDataTest {

    private ApplicationContext ctx = null;

    private EmployeeRepository employeeRepository = null;

    @Before
    public void setup(){
        ctx = new ClassPathXmlApplicationContext("beans.xml");
        employeeRepository = ctx.getBean(EmployeeRepository.class);
        System.out.println("setup");
    }

    @After
    public void tearDown(){
        ctx = null;
        System.out.println("tearDown");
    }

    @Test
    public void testEntityManagerFactory(){

    }

    @Test
    public void testFindByName(){
        System.out.println(employeeRepository);
        Employee employee = employeeRepository.findByName("zhangsan");
        System.out.println("id:" + employee.getId()
                + " , name:" + employee.getName()
                + " ,age:" + employee.getAge());
    }

    @Test
    public void testfindByNameIsStartingWithAndAgeLessThan(){
        System.out.println(employeeRepository);
        List<Employee> employees = employeeRepository.findByNameIsStartingWithAndAgeLessThan("wang",50);
        for (Employee employee: employees) {
            System.out.println("id:" + employee.getId()
                    + " , name:" + employee.getName()
                    + " ,age:" + employee.getAge());
        }
    }
}

Spring Data 关于Repository的介绍 和 查询方法的规则定义

执行下测试  看下答应结果

  Spring Data 关于Repository的介绍 和 查询方法的规则定义

是不是达到效果啦