有句话这样说 欲练神功 挥刀自宫 请亲们先回到第一个 从Spring data 介绍 开始看 搭好环境 跟着步伐一块走java
Spring Data 的方法必须严格按照它的规范进行编写,若是写错了就不行spring
下面是网上找的一张图:仔细看 dom
我们先拿几个方法来作个示例测试
在这以前 先往数据表插入一些数据 spa
insert into employee(name,age) values('wangwu',12); ..... 大家本身插写数据code
先贴下个人数据xml
继续 基于原先的代码进行修改 EmployeeRepository.java 第二个方法 咱们所演示的blog
本按理接口对应的方法------>findByNameIsStartingWithAndAgeLessThan接口
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);
}
测试类仍是基于原先的 修改 本案例测试类方法---------->testfindByNameIsStartingWithAndAgeLessThan 查询name已wang开始的而且年龄小于50岁的get
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()); } } }
执行下测试 看下答应结果
是否是达到效果啦