1. 程式人生 > >spring data jpa 通過方法名稱查詢、限制查詢結果查詢

spring data jpa 通過方法名稱查詢、限制查詢結果查詢

2個javabean

package com.example.demo.entity;

import javax.persistence.*;
import java.io.Serializable;
@Entity
@Table(name = "class_room")
public class ClassRoom implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String name;
    private String address;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "["+this.name+"---"+this.address+"]";
    }
}
package com.example.demo.entity;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;

@Entity
@Table(name = "student")
public class Student implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String name;
    private Integer age;
    private Date birthday;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    @Override
    public String toString() {
        return "["+this.name+"--"+this.age+"---"+this.birthday+"]";
    }
}

2個repository

package com.example.demo.repository.base;

import com.example.demo.entity.ClassRoom;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface ClassRoomRepository extends JpaRepository<ClassRoom, Long> {

    ClassRoom findByName(String name);
    
    List<ClassRoom> findByNameLike(String name);

    List<ClassRoom> findByAddressIsNull();

    List<ClassRoom> findByNameStartingWith(String name);

    List<ClassRoom> findByNameContaining(String name);
}
package com.example.demo.repository.base;

import com.example.demo.entity.ClassRoom;
import com.example.demo.entity.Student;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.Collection;
import java.util.Date;
import java.util.List;

public interface StudentRepository extends JpaRepository<Student, Long> {

    Student findByNameAndAge(String name,Integer age);

    List<Student> findByNameOrAge(String name,Integer age);

    List<Student> findByAgeBetween(Integer age1,Integer age2);

    List<Student> findByAgeLessThan(Integer age);

    List<Student> findByAgeLessThanEqual(Integer age);

    List<Student> findByBirthdayBefore(Date date);

    List<Student> findByOrderByAgeDesc();

    List<Student> findByNameNot(String name);

    List<Student> findByIdIn(Long[] ids);

    Student findByNameIgnoreCase(String name);
}

測試及說明

package com.example.demo;

import com.example.demo.entity.ClassRoom;
import com.example.demo.entity.Student;
import com.example.demo.repository.base.ClassRoomRepository;
import com.example.demo.repository.base.StudentRepository;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoApplicationTests {

    @Autowired
    private ClassRoomRepository classRoomRepository;
    @Autowired
    private StudentRepository studentRepository;

    /**
     * 從方法名建立查詢語句,sql語句在註釋中
     */

    /**
     * 根據name查詢
     * select classroom0_.id as id1_0_, classroom0_.address as address2_0_, classroom0_.name as name3_0_ from
     * class_room classroom0_ where classroom0_.name=?
     */
    @Test
    public void findByName(){
        String name = "教室01";
        ClassRoom classRooms = classRoomRepository.findByName(name);
        System.out.println(classRooms);
    }

    /**
     * like查詢
     * select classroom0_.id as id1_0_, classroom0_.address as address2_0_, classroom0_.name as name3_0_ from
     * class_room classroom0_ where classroom0_.name like ?
     */
    @Test
    public void findByNameLike(){
        String name = "%01%";
        List<ClassRoom> classRooms = classRoomRepository.findByNameLike(name);
        System.out.println(classRooms);

    }

    /**
     * and查詢
     * select student0_.id as id1_1_, student0_.age as age2_1_, student0_.birthday as birthday3_1_, student0_.name as name4_1_ from
     * student student0_ where student0_.name=? and student0_.age=?
     */
    @Test
    public void findByNameAndAge(){
        String name = "mock";
        Integer age = 10;
        Student student = studentRepository.findByNameAndAge(name,age);
        System.out.println(student);
    }

    /**
     * or查詢
     * select student0_.id as id1_1_, student0_.age as age2_1_, student0_.birthday as birthday3_1_, student0_.name as name4_1_ from
     * student student0_ where student0_.name=? or student0_.age=?
     */
    @Test
    public void findByNameOrAge(){
        String name = "mock";
        Integer age = 6;
        List<Student> student = studentRepository.findByNameOrAge(name,age);
        System.out.println(student);
    }

    /**
     * Between查詢(between 查詢包括6和10 也就是 >=6 <=10)
     *select student0_.id as id1_1_, student0_.age as age2_1_, student0_.birthday as birthday3_1_, student0_.name as name4_1_ from
     *student student0_ where student0_.age between ? and ?
     */
    @Test
    public void findByAgeBetween(){
        Integer age1 = 6;
        Integer age2 = 10;
        List<Student> student = studentRepository.findByAgeBetween(age1,age2);
        System.out.println(student);
    }

    /**
     * lessThan查詢(GreaterThan同理)
     * select student0_.id as id1_1_, student0_.age as age2_1_, student0_.birthday as birthday3_1_, student0_.name as name4_1_ from
     * student student0_ where student0_.age<?
     */
    @Test
    public void findByAgeLessThan(){
        Integer age = 10;
        List<Student> student = studentRepository.findByAgeLessThan(age);
        System.out.println(student);
    }

    /**
     * LessThanEqual查詢(GreaterThanEqual同理)
     * select student0_.id as id1_1_, student0_.age as age2_1_, student0_.birthday as birthday3_1_, student0_.name as name4_1_ from
     * student student0_ where student0_.age<=?
     */
    @Test
    public void findByAgeLessThanEqual(){
        Integer age = 10;
        List<Student> student = studentRepository.findByAgeLessThanEqual(age);
        System.out.println(student);
    }

    /**
     * Before查詢(經過測試Before與LessThan不是一個東東,後者處理不了時間的比較,前者也處理不了數字,只能處理日期)After同理
     * select student0_.id as id1_1_, student0_.age as age2_1_, student0_.birthday as birthday3_1_, student0_.name as name4_1_
     * from student student0_ where student0_.birthday<?
     */
    @Test
    public void findByBirthdayBefore(){
        String s = "2018-05-06";
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = format.parse(s);
        } catch (ParseException e) {
            System.out.println(e.getMessage());
        }
        List<Student> student = studentRepository.findByBirthdayBefore(date);
        System.out.println(student);
    }

    /**
     * isNUll查詢(isNotNull同理)
     * select classroom0_.id as id1_0_, classroom0_.address as address2_0_, classroom0_.name as name3_0_
     * from class_room classroom0_ where classroom0_.address is null
     */
    @Test
    public void findByAddressIsNull(){
        List<ClassRoom> classRooms = classRoomRepository.findByAddressIsNull();
        System.out.println(classRooms);
    }

    /**
     * StartingWith(相當於like中的  ‘%室’) EndingWith 相當於‘室’%
     * select classroom0_.id as id1_0_, classroom0_.address as address2_0_, classroom0_.name as name3_0_
     * from class_room classroom0_ where classroom0_.name like ?
     */
    @Test
    public void findByNameStartingWith(){
        String name = "室";
        List<ClassRoom> classRooms = classRoomRepository.findByNameStartingWith(name);
        System.out.println(classRooms);
    }

    /**
     * Containing相當於like(%2%)
     * select classroom0_.id as id1_0_, classroom0_.address as address2_0_, classroom0_.name as name3_0_
     * from class_room classroom0_ where classroom0_.name like ?
     */
    @Test
    public void findByNameContaining(){
        String name = "2";
        List<ClassRoom> classRooms = classRoomRepository.findByNameContaining(name);
        System.out.println(classRooms);
    }

    /**
     * orderBy
     * select student0_.id as id1_1_, student0_.age as age2_1_, student0_.birthday as birthday3_1_, student0_.name as name4_1_
     * from student student0_ order by student0_.age desc
     */
    @Test
    public void findByOrderByAgeDesc(){
        List<Student> student = studentRepository.findByOrderByAgeDesc();
        System.out.println(student);
    }

    /**Not
     * select student0_.id as id1_1_, student0_.age as age2_1_, student0_.birthday as birthday3_1_, student0_.name as name4_1_
     * from student student0_ where student0_.name<>?
     */
    @Test
    public void findByNameNot(){
        String name = "mock";
        List<Student> student = studentRepository.findByNameNot(name);
        System.out.println(student);
    }

    /**
     * in
     * select student0_.id as id1_1_, student0_.age as age2_1_, student0_.birthday as birthday3_1_, student0_.name as name4_1_
     * from student student0_ where student0_.id in (? , ?)
     */
    @Test
    public void findByIdIn(){
        Long [] ids = new Long[2];
        ids[0]=1L;
        ids[1]=2L;
        List<Student> student = studentRepository.findByIdIn(ids);
        System.out.println(student);
    }

    /**
     * IgnoreCase
     * select student0_.id as id1_1_, student0_.age as age2_1_, student0_.birthday as birthday3_1_, student0_.name
     * as name4_1_ from student student0_ where upper(student0_.name)=upper(?)
     */
    @Test
    public void findByNameIgnoreCase(){
        String name = "Mock";
        Student student = studentRepository.findByNameIgnoreCase(name);
        System.out.println(student);
    }
}

限制查詢結果

Student findFirstByOrderByAgeDesc();
List<Student> findFirst3ByOrderByAgeDesc();
/**
     * 查詢第一條
     *select student0_.id as id1_1_, student0_.age as age2_1_, student0_.birthday as birthday3_1_, student0_.class_id as class_id6_1_,
     *student0_.name as name4_1_, student0_.sex as sex5_1_ from student student0_ order by student0_.age desc limit ?
     */
    @Test
    public void findFirstByOrOrderByAgeDesc(){
        Student student = studentRepository.findFirstByOrderByAgeDesc();
        System.out.println(student.getName());
    }

    /**
     * 查詢1到3條
     * select student0_.id as id1_1_, student0_.age as age2_1_, student0_.birthday as birthday3_1_, student0_.class_id as class_id6_1_, student0_.name
     * as name4_1_, student0_.sex as sex5_1_ from student student0_ order by student0_.age desc limit ?
     */
    @Test
    public void findFirst3ByOrderByAgeDesc(){
        List<Student> student = studentRepository.findFirst3ByOrderByAgeDesc();
        System.out.println(student);
    }