有句话这样说  欲练神功  挥刀自宫  请亲们先回到第一个  从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. iOS图片压缩上传

    本文实例为大家分享了iOS实现压缩图片上传功能,供大家参考,具体内容如下 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 2 ...

  2. vue-router介绍

    vue-router学习 转自:https://my.oschina.net/u/1416844/blog/849971 1. vue-router介绍 vue-router把react-router ...

  3. msyql的内存计算

    本文将讨论MySQL内存相关的一些选项,包括: 单位都是b,不是kb,即1B=1/(1024*1024*1024)G 1)全局的buffer,如innodb_buffer_pool_size: 2)线 ...

  4. 常见的装包的三种宝,包 bao-devel bao-utils bao-agent ,包 开发包 工具包 客户端

    常见的装包的三种宝,包  bao-devel    bao-utils   bao-agent  ,包    开发包   工具包  客户端

  5. node.js发http请求

    标准库中默认的HTTP模块 const https = require('https'); https.get('https://api.nasa.gov/planetary/apod?api_key ...

  6. openresty 视频

    http://v.163.com/paike/V8H1BIE6U/V949ER8RD.html#from=search

  7. EasyUI DataGrid合并单元

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

  8. oracle和SQLserver数据库中select into 的区别

    在Oracle中是这样的 在SQLserver中是这样的

  9. iOS 基于OpenCV图像比较的常见方法

    先列个大纲 1.模板匹配 2.直方图比较 3.感知哈希算法 4.特征点匹配

  10. mysql查询各种类型的前N条记录

    mysql查询各种类型的前N条记录,将3改为N(需查询条数)即可  (select * from event_info where event_type = 1  limit 3)union all( ...