JPA addition, deletion, modification and query class summary

Query category:

One two five:

Object-oriented search method, using the method provided by the bottom layer of JPA (no need to write SQL, JPQL)
    JPA provides limited support for methods 1 and 2, and can only implement some simple CRUD; if you want to implement complex functions, use method 5
    and method 5. The idea is similar to mybatis reverse engineering. Design concept: Allow people who do not understand SQL to write complex SQL statements

Three four: 

You need to write SQL, JPQL

One way to use jpa:

  There is no need to write any methods in the dao layer, just call the methods in the parent interface.
  Disadvantage: The methods provided in the parent interface are limited

The second way to use jpa:

You can write your own methods without writing SQL. The premise is to write the method name according to the convention

Three ways to use jpa:

Object-oriented SQL statement: JPQL

  @Query("select p form People p where id=?1 and age=?2") 
  publish List<People> findByIdAndAge(int id, int age);

Method 3 Basic plus pagination function:

  Pageable(PageRequest)

Four ways to use jpa:

SQL-oriented (native SQL statement), recommended

原生sql增删改
 @Modifying
 @Query(nativeQuery=true, value="insert into tb_address(id,name) values(?,?) ")
  publish List<People> addAddress(Integer id, String name);

 @Modifying
 @Query(nativeQuery=true, value="delete from tb_address where id=? ")
  publish List<People> delAddress(Integer id);

 @Modifying
 @Query(nativeQuery=true, value="update tb_address a set a.name=? where a.id=?")
  publish List<People> updateAddress(String name, Integer id);

native sql query

 @Query(nativeQuery=true, value="select * from tb_people where name=? and age=?")
  publish List<People> findByNameAndAge(string name, int age);

  @Query(nativeQuery=true, value="select * from tb_people where age in (:ages)")
  publish List<People> findByAges(@Param("ages") int age);
  :age is a named parameter, indicating that it is a variable rather than an ordinary field

Method 4 Basic plus pagination function:

Pageable(PageRequest)

JPA usage five: object-oriented search method

Method 5 implementation steps:
    similar to method 1, you don’t need to customize any method in dao (you need to write dao, but you don’t need to write the method in dao)
        such as findAll (implement the Specification interface) use Predicate to construct query conditions
    to realize service, controller

There are detailed descriptions in the dynamic query Specifications

Dynamic query:

QueryByExample  

    Matches that can only match at the beginning/include/end/regular of the string
    do not support nested conditions, such as or

Specifications

//Conditional paging query
public R list(TGtSampleDto dto) {         Page<TGtSample> list = dao.findAll(new Specification<TGtSample>() {             @Override             public Predicate toPredicate(Root<TGtSample> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) { //                 query Condition                 List<Predicate> predicates = new ArrayList<>();                 if(dto.getType() != null){ // Whether there is an id in the where clause predicates.add(                     criteriaBuilder.equal(root.get("type").as(Integer.class), dto.getType())); }                 if                 (dto.getRailwayBureau()!=null && dto .getRailwayBureau()!=""){ // Whether there is a railwayBureau in the where clause









                    predicates.add(criteriaBuilder.like(root.get("railwayBureau").as(String.class), "%"+dto.getRailwayBureau()+"%"));
                }
                if(dto.getMinUploadTime()!=null){ // 是否where子句中有uploadTime
                    predicates.add(criteriaBuilder.greaterThanOrEqualTo(root.get("uploadTime").as(LocalDateTime.class), dto.getMinUploadTime()));
                }
                if(dto.getMaxUploadTime()!=null){ // 是否where子句中有uploadTime
                    predicates.add(criteriaBuilder.lessThanOrEqualTo(root.get("uploadTime").as(LocalDateTime.class), dto.getMaxUploadTime()));
                }
                criteriaQuery.orderBy(criteriaBuilder.desc(root.get("uploadTime").as(Date.class)));

                return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
            }
        }, PageRequest.of(dto.getCurrentPage(), dto.getSize()));
        return R.ok(list);
    }

Querydsl

    Third-party general query framework

SQL knowledge:

1. When using group by, the field after select is either an aggregate function or after group by

2. To be updated

Guess you like

Origin blog.csdn.net/m0_69057918/article/details/131083140