When it is difficult to use method names to achieve the desired query results, we can use @Query to query.@QueryIt is a convenient way to add custom queries.
Using @QueryAnnotation, there are two ways to use annotation, one is JPQL SQL language way, one is native SQL language.
1.User.java Entity class
-
package com.niugang.entity;
-
import javax.persistence.Column;
-
import javax.persistence.Entity;
-
import javax.persistence.GeneratedValue;
-
import javax.persistence.GenerationType;
-
import javax.persistence.Id;
-
import javax.persistence.Table;
-
//javax.persistence.Entity
-
-
-
public class User {
-
/**
-
*mysql Self generated primary key
-
*Indicates that the persistence provider must allocate the primary key of the entity using the database identification column.
-
*/
-
-
-
-
private Integer id;
-
/**
-
* @ColumnThe name in the default is the same as the default field name.
-
*/
-
-
private String name;
-
-
private Integer age;
-
-
private String phone;
-
-
private String password;
-
public String getPassword() {
-
return password;
-
}
-
public void setPassword(String password) {
-
this.password = password;
-
}
-
public Integer getId() {
-
return id;
-
}
-
public void setId(Integer 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 String getPhone() {
-
return phone;
-
}
-
public void setPhone(String phone) {
-
this.phone = phone;
-
}
-
-
public String toString() {
-
return “User [id=” + id + “, name=” + name + “, age=” + age + “, phone=” + phone + “, password=” + password
-
+ “]”;
-
}
-
}
2.UserDao.java
-
package com.niugang.dao;
-
import java.util.List;
-
import org.springframework.data.domain.Page;
-
import org.springframework.data.domain.Pageable;
-
import org.springframework.data.jpa.repository.JpaRepository;
-
import org.springframework.data.jpa.repository.Query;
-
import org.springframework.data.repository.query.Param;
-
import org.springframework.stereotype.Repository;
-
import com.niugang.entity.User;
-
/**
-
* JpaRepository springbootIt has been configured automatically, and has also been injected into the data source.
-
*
-
* @author niugang
-
*
-
*/
-
-
public interface UserDao extends JpaRepository<User, Integer> {
-
/**
-
* ?Add numbers to indicate placeholders? 1 represents the first parameter in the method parameter, which is different from other index, starting from 1.
-
*/
-
// select * from User where name = ?1 Be careful not to write *
-
-
User findUserByName1(String name);
-
/**
-
* =:Add the variable name, which is in the method parameter.@ParamThe value matching is not the same as the actual one, but is similar to the first one above.
-
*/
-
-
User findUserByName2(@Param(“name”) String username);
-
/**
-
* Query through native SQL
-
* To open nativeQuery=true, you can use the native SQL statement to complete the query in value.
-
*/
-
-
User findUserByNativeSQL(@Param(“name”) String username);
-
/**
-
* The number of fuzzy queries here can only be placed in front of the placeholder.
-
*/
-
-
List<User> findUserLike(String name);
-
/**
-
* Conditional paging query Pageable:spring-data-jpa self contained interface
-
*
-
* @param name
-
* :Query condition name
-
* @param page:Paging object
-
* @return
-
*/
-
-
Page<User> findUserLikeByPage(String name, Pageable page);
-
/**
-
* @QueryQuery is not supported, sort and pagination.
-
* Source code analysis
-
* if (hasParameterOfType(method, Pageable.class)) {
-
if (!isStreamQuery()) {
-
assertReturnTypeAssignable(method, Slice.class, Page.class, List.class);
-
}
-
if (hasParameterOfType(method, Sort.class)) {
-
throw new IllegalStateException(String.format(“Method must not have Pageable *and* Sort parameter. “
-
+ “Use sorting capabilities on Pageble instead! Offending method: %s”, method.toString()));
-
}
-
}
-
*
-
* @param sort :Sorting object org.springframework.data.domain.Sort
-
* @return
-
*/
-
-
List<User> findUserLikeBySort(Sort sort);
-
}
3. findUserLikeByPage(String name, Pageable page)Interface explanation:
-
public Page<User> findUserByPage(String name) {
-
PageRequest pageRequest = new PageRequest(0, 4);
-
return userDao.findUserLikeByPage(name, pageRequest);
-
}
4.Sorting test code
-
/**
-
* Single conditional sort
-
*/
-
public List<User> findListSortSingleCondition(){
-
//idAscending query
-
Sort sort = new Sort(Sort.Direction.ASC,“id”);
-
return userDao.findUserLikeBySort(sort);
-
}
-
/**
-
* Multi condition ranking
-
*/
-
public List<User> findListSortMultiCondition(){
-
List<Order> orders=new ArrayList<Order>();
-
Order orderId = new Sort.Order(Sort.Direction.DESC,“id”);
-
Order orderAge = new Sort.Order(Sort.Direction.DESC,“age”);
-
orders.add(orderId);
-
orders.add(orderAge);
-
Sort sort = new Sort(orders);
-
return userDao.findUserLikeBySort(sort);
-
}