有句话这样说  欲练神功  挥刀自宫  请亲们先回到第一个  从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());
        }
    }
}

执行下测试  看下答应结果

  

是不是达到效果啦

Spring Data 查询方法的规则定义(五)的更多相关文章

  1. SpringBoot05 数据操作03 -> JPA查询方法的规则定义

    请参见<springboot详解>springjpa部分知识 1 按照方法命名来进行查询 待更新... package cn.xiangxu.springboot.repository; ...

  2. spring data jpa方法命名规则

    关键字 方法命名 sql where字句 And findByNameAndPwd where name= ? and pwd =? Or findByNameOrSex where name= ? ...

  3. 74. Spring Data JPA方法定义规范【从零开始学Spring Boot】

    [从零开始学习Spirng Boot-常见异常汇总] 事情的起因:有人问过我们这个这个问题:为什么我利用Spring data jpa写的方法没有按照我想要的情况进行执行呢?我记得当时只是告诉他你你先 ...

  4. Spring Data JPA方法定义规范

    Spring Data Jpa方法定义的规则: (1)简单条件查询 简单条件查询:查询某一个实体类或者集合. 按照Spring Data的规范的规定,查询方法以find | read | get开头, ...

  5. Spring JdbcTemplate 查询方法中的RowMapper实现汇总

    实现一.在内部建立内联类实现RowMapper接口 package hysteria.contact.dao.impl; import java.sql.ResultSet; import java. ...

  6. Spring Data JPA 简单查询--方法定义规则

    一.常用规则速查 1  And 并且2  Or  或3  Is,Equals 等于4  Between  两者之间5  LessThan 小于6  LessThanEqual   小于等于7  Gre ...

  7. Spring Data JPA 简单查询

    一.常用规则速查 1  And 并且2  Or  或3  Is,Equals 等于4  Between  两者之间5  LessThan 小于6  LessThanEqual   小于等于7  Gre ...

  8. SpringBoot学习笔记:Spring Data Jpa的使用

    更多请关注公众号 Spring Data Jpa 简介 JPA JPA(Java Persistence API)意即Java持久化API,是Sun官方在JDK5.0后提出的Java持久化规范(JSR ...

  9. Spring Data(一)概念和仓库的定义

    Spring Data(一)概念和仓库的定义 Spring Data的主要任务是为数据访问提供一个相似的.一致的.基于Spring的编程模型,同时又保留着下面各个数据存储的特征.它使得使用数据访问技术 ...

随机推荐

  1. 关闭socket连接最好的方法

    最好关闭连接的方法 `C S` `shutdown-WR ` `发送FIN` ` read-0发送ACK` ` ......` ` close` ` 发送FIN` `read-0` `close` ` ...

  2. [ExtJS5学习笔记]第二十七节 CMD打包错误 Error C2009: YUI Parse Error (identifier is a reserved word =&gt; debugger;)

    本文地址:http://blog.csdn.net/sushengmiyan/article/details/41242993 本文作者:sushengmiyan ------------------ ...

  3. git clone 故障 fatal could not create work tree dir

    问题如上图,原因是openWRT目录权限的问题,该目录是新创建的查看目录权限后发现该目录只对root有读写权限,对所有者及其他用户无读写权限.最简单的chmod 777 openWRT即可解决问题.

  4. EasyUI DataGrid合并单元

    <table id="tt"></table> $('#tt').datagrid({     title:'Merge Cells',     iconC ...

  5. 编译内核出现"mkimage" command not found - U-Boot images will not be built

    参考链接: http://spyker729.blogspot.com/2010/07/mkimage-command-not-found-u-boot-images.html 制作uImage的工具 ...

  6. &lt;LeetCode OJ&gt; 217./219. Contains Duplicate (I / II)

    Given an array of integers, find if the array contains any duplicates. Your function should return t ...

  7. 探索PowerShell----函数

    http://marui.blog.51cto.com/1034148/294775/

  8. android classloader双亲托付模式

    概述 ClassLoader的双亲托付模式:classloader 按级别分为三个级别:最上级 : bootstrap classLoader(根类载入器) : 中间级:extension class ...

  9. MySQL------如何卸载与安装

    1.安装 转载:http://wenda.so.com/q/1471475177723102?src=140 2.卸载 转载:http://jingyan.baidu.com/article/3d69 ...

  10. Struts2_day02--Action获取表单提交数据

    Action获取表单提交数据 1 之前web阶段,提交表单到servlet里面,在servlet里面使用request对象里面的方法获取,getParameter,getParameterMap 2 ...