标签:hand log interface tar ima art beans star setup
有句话这样说 欲练神功 挥刀自宫 请亲们先回到第一个 从Spring data 介绍 开始看 搭好环境 跟着步伐一块走
Spring Data 的方法必须严格按照它的规范进行编写,如果写错了就不行
下面是网上找的一张图:仔细看
咱们先拿几个方法来做个示例
在这之前 先往数据表插入一些数据
insert into employee(name,age) values(‘wangwu‘,12); ..... 你们自己插写数据
先贴下我的数据
继续 基于原先的代码进行修改 EmployeeRepository.java 第二个方法 我们所演示的
本按理接口对应的方法------>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岁的
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()); } } }
执行下测试 看下答应结果
是不是达到效果啦
标签:hand log interface tar ima art beans star setup
原文地址:http://www.cnblogs.com/fzng/p/7253189.html