09)SpringBoot 数据操作03-> JPA查询方法的规则定义

1 按照方法命名来进行查询

 

package cn.xiangxu.springboot.repository;

import cn.xiangxu.springboot.entity.dataObject.Girl;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface GirlRepository extends JpaRepository<Girl, Integer> {
    // where name like ?% and age < ?
    public List<Girl> findByNameStartingWithAndAgeLessThan(String name, Integer age);
}

 

  弊端:方法名很长而且对于复杂的查询很难实现

 

2 利用@Query注解实现复杂查询

  可以利用实体对象进行查询,也可以利用原生的SQL语句进行查询;利用原生的SQL语句进行查询时需要设置nativeQuery的值为True

  2.1 利用实体类进行查询

@Query("select g from Girl g where id = (select max(id) from Girl g1)") // 利用实体类进行查询,可以用别名代替*
Girl findByMaxId();

  2.2 利用原生SQL语句进行查询

    @Query(nativeQuery = true, value = "select * from girl g") // 利用原生的SQL进行查询,不能用别名代替*
    List<Girl> findAllGirl();

  2.3 利用索引参数进行查询

    @Query(nativeQuery = true, value = "select * from girl o where o.girl_id=?1 ") // 索引参数
    Girl findOneById(Integer id);

  2.4 利用命名参数进行查询

    @Query(value = "select o from Girl o where o.id=:id") // 命名参数
    Girl findGirlById02(@Param("id") Integer girlId);

 

 

09)SpringBoot 数据操作03-> JPA查询方法的规则定义

 

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package org.springframework.data.jpa.repository;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.data.annotation.QueryAnnotation;

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})
@QueryAnnotation
@Documented
public @interface Query {
    String value() default "";

    String countQuery() default "";

    String countProjection() default "";

    boolean nativeQuery() default false;

    String name() default "";

    String countName() default "";
}

 

  知识点总汇:点击前往

 

3 利用@Modifying实现更新操作

  在进行更新操作时必须添加这个注解

  3.1 不添加该注解时的报错信息

 

  3.2 添加了该注解后还是报错

    原因:更新操作需要事务支持

 09)SpringBoot 数据操作03-> JPA查询方法的规则定义

 

  3.3 添加@Transactional注解实现事务支持

    注意:@Transactional一般都是放在服务层的相关方法中的

    @Transactional
    @Modifying
    @Query(value = "update Girl o set o.age = ?2 where o.id = ?1")
    void updateAgeById(Integer id, Integer age);

  

 

 

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package org.springframework.data.jpa.repository;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})
@Documented
public @interface Modifying {
    boolean clearAutomatically() default false;
}

 

 

 

 

package cn.xiangxu.springboot.repository;

import cn.xiangxu.springboot.entity.dataObject.Girl;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

public interface GirlRepository extends JpaRepository<Girl, Integer> {
    // where name like ?% and age < ?
    List<Girl> findByNameStartingWithAndAgeLessThan(String name, Integer age);

    @Query("select g from Girl g where id = (select max(id) from Girl g1)") // 利用实体类进行查询,可以用别名代替*
    Girl findByMaxId();

    @Query(nativeQuery = true, value = "select * from girl g") // 利用原生的SQL进行查询,不能用别名代替*
    List<Girl> findAllGirl();

    @Query(nativeQuery = true, value = "select * from girl o where o.girl_id=?1 ") // 索引参数
    Girl findOneById(Integer id);

    @Query(value = "select o from Girl o where o.id=:id") // 命名参数
    Girl findGirlById02(@Param("id") Integer girlId);

    @Transactional
    @Modifying
    @Query(value = "update Girl o set o.age = ?2 where o.id = ?1")
    void updateAgeById(Integer id, Integer age);
}

 

原文链接https://www.cnblogs.com/NeverCtrl-C/p/7905199.html

 

关注公众号,将会有更多精彩每天送达:

公众号内有精彩内容,可以提现

                                                  09)SpringBoot 数据操作03-> JPA查询方法的规则定义