SpringDataJPA(二)SpringDataJPA自定义关键字查询

源码

GitHub: https://github.com/291685399/springboot-learning/tree/master/springboot-springdatajpa02

什么是自定义关键字查询

SpringDataJPA支持自定义关键字查询,在方法中含有关键字,SpringDataJPA会为你自动生成SQL

SpringDataJPA提供的关键字

关键字 方法名举例 对应的SQL
And findByNameAndAge where name = ? and age = ?
Or findByNameOrAge where name = ? or age = ?
Is findByNameIs where name = ?
Equals findByNameEquals where name = ?
Between findByAgeBetween where age between ? and ?
LessThan findByAgeLessThan where age < ?
LessThanEquals findByAgeLessThanEqual where age <= ?
GreatorThan findByAgeGreaterThan where age > ?
GreatorThanEquals findByAgeGreaterThanEqual where age >= ?
After findByAgeAfter where age > ?
Before findByAgeBefore where age < ?
IsNull findByNameIsNull where name is null
IsNotNull,NotNull findByNameIsNotNull,findByNameNotNull where name is not null
Not findByNameNot where name <>?
In findByAgeIn where age in (?)
NotIn findByAgeNotIn where age not in (?)
NotLike findByNameNotLike where name not like ?
Like findByNameLike where name like ?
StartingWith findByNameStartingWith where name like ‘?%’
EndingWith findByNameEndingWith where name like ‘%?’
Containing,Contains findByNameContaining,findByNameContains where name like ‘%?%’
OrderBy findByOrderByAgeDesc order by age desc
True findByBossTrue where boss = true
False findByBossFalse where boss = false
IgnoreCase findByNameIgnoreCase where UPPER(name) = UPPER(?)

自定义关键字查询

pom.xml:

<dependencies>
    
    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-webartifactId>
    dependency>
    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-testartifactId>
        <scope>testscope>
    dependency>

    
    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-data-jpaartifactId>
    dependency>

    
    <dependency>
        <groupId>mysqlgroupId>
        <artifactId>mysql-connector-javaartifactId>
        <scope>runtimescope>
    dependency>

    
    <dependency>
        <groupId>org.projectlombokgroupId>
        <artifactId>lombokartifactId>
        <optional>trueoptional>
    dependency>
dependencies>

application.properties:

# datasource
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/springboot-springdatajpa02?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=root
# springdatajpa
#打印出自动生产的SQL,方便调试的时候查看
spring.jpa.show-sql=true
#更新数据库表结构
spring.jpa.hibernate.ddl-auto=update
#对打印的sql进行格式化,方便查看
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.properties.hibernate.use_sql_comments=true
#指定生成表名的存储引擎为InneoDB
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect

User:

@Entity
@Data
public class User {
    @Id
    @GeneratedValue
    private Long id;
    private String name;
    private Integer age;
    private String sex;
    private String address;
}
  • 继承Repository或者Repository子接口

UserRepository:

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

    @Autowired
    private UserRepository userRepository;

    @Test
    public void findByNameAndAge() {
        List<User> userList = userRepository.findByNameAndAge("ljk", 18);
        System.out.println(userList);
    }

    @Test
    public void findByNameOrAge() {
        List<User> userList = userRepository.findByNameOrAge("ljk", 18);
        System.out.println(userList);
    }

    @Test
    public void findByNameIs() {
        String str = "wyj";
        List<User> userList = userRepository.findByNameIs(str);
        System.out.println(userList);
    }

    @Test
    public void findByNameEquals() {
        String str = "wyj";
        List<User> userList = userRepository.findByNameEquals(str);
        System.out.println(userList);
    }

    @Test
    public void findByAgeBetween() {
        int littleAge = 17;
        int bigAge = 18;
        List<User> userList = userRepository.findByAgeBetween(littleAge, bigAge);
        System.out.println(userList);
    }

    @Test
    public void findByAgeLessThan() {
        int age = 19;
        List<User> userList = userRepository.findByAgeLessThan(age);
        System.out.println(userList);
    }

    @Test
    public void findByAgeLessThanEqual() {
        int age = 18;
        List<User> userList = userRepository.findByAgeLessThanEqual(age);
        System.out.println(userList);
    }

    @Test
    public void findByAgeGreaterThan() {
        int age = 17;
        List<User> userList = userRepository.findByAgeGreaterThan(age);
        System.out.println(userList);
    }

    @Test
    public void findByAgeGreaterThanEqual() {
        int age = 18;
        List<User> userList = userRepository.findByAgeGreaterThanEqual(age);
        System.out.println(userList);
    }

    @Test
    public void findByOrderByAgeDesc() {
        List<User> userList = userRepository.findByOrderByAgeDesc();
        System.out.println(userList);
    }

    @Test
    public void findByNameIgnoreCase() {
        String name = "Wyj";
        List<User> userList = userRepository.findByNameIgnoreCase(name);
        System.out.println(userList);
    }
}

你可能感兴趣的:(SpringDataJPA)