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);
}