根据前两篇博文,相信大伙看完之后,应该对Spring Data JPA有了一个感性的认识。接下来,我们来看看Spring Data JPA的查询方法


UserInfo  实体:前文提到过,用myeclipse的生成JPA实体逆向生成:
package com.example.entity;

import java.sql.Timestamp;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;


/**
 * UserInfo entity. @author MyEclipse Persistence Tools
 */
@Entity
@Table(name="user_info"
    ,catalog="rbac"
, uniqueConstraints = @UniqueConstraint(columnNames="username")
)

public class UserInfo  implements java.io.Serializable {


    // Fields    

     private String id;
     private String username;
     private String password;
     private String sex;
     private Timestamp birthday;
     private String email;
     private String contact;
     private String onlineState;
     private Timestamp lastLoginTime;
     private Timestamp lastLogoutTime;
     private String lastLoginIp;
     private String userState;
     private Integer listOrder;
     private String createUser;
     private Timestamp createDatetime;
     private String modifyUser;
     private Timestamp modifyDatetime;
     private String deleteFlag;


    // Constructors

    /** default constructor */
    public UserInfo() {
    }

	/** minimal constructor */
    public UserInfo(String id, String username, String password) {
        this.id = id;
        this.username = username;
        this.password = password;
    }
    
    /** full constructor */
    public UserInfo(String id, String username, String password, String sex, Timestamp birthday, String email, String contact, String onlineState, Timestamp lastLoginTime, Timestamp lastLogoutTime, String lastLoginIp, String userState, Integer listOrder, String createUser, Timestamp createDatetime, String modifyUser, Timestamp modifyDatetime, String deleteFlag) {
        this.id = id;
        this.username = username;
        this.password = password;
        this.sex = sex;
        this.birthday = birthday;
        this.email = email;
        this.contact = contact;
        this.onlineState = onlineState;
        this.lastLoginTime = lastLoginTime;
        this.lastLogoutTime = lastLogoutTime;
        this.lastLoginIp = lastLoginIp;
        this.userState = userState;
        this.listOrder = listOrder;
        this.createUser = createUser;
        this.createDatetime = createDatetime;
        this.modifyUser = modifyUser;
        this.modifyDatetime = modifyDatetime;
        this.deleteFlag = deleteFlag;
    }

   
    // Property accessors
    @Id 
    
    @Column(name="id", unique=true, nullable=false, length=50)

    public String getId() {
        return this.id;
    }
    
    public void setId(String id) {
        this.id = id;
    }
    
    @Column(name="username", unique=true, nullable=false, length=50)

    public String getUsername() {
        return this.username;
    }
    
    public void setUsername(String username) {
        this.username = username;
    }
    
    @Column(name="password", nullable=false, length=50)

    public String getPassword() {
        return this.password;
    }
    
    public void setPassword(String password) {
        this.password = password;
    }
    
    @Column(name="sex", length=1)

    public String getSex() {
        return this.sex;
    }
    
    public void setSex(String sex) {
        this.sex = sex;
    }
    
    @Column(name="birthday", length=0)

    public Timestamp getBirthday() {
        return this.birthday;
    }
    
    public void setBirthday(Timestamp birthday) {
        this.birthday = birthday;
    }
    
    @Column(name="email", length=50)

    public String getEmail() {
        return this.email;
    }
    
    public void setEmail(String email) {
        this.email = email;
    }
    
    @Column(name="contact", length=50)

    public String getContact() {
        return this.contact;
    }
    
    public void setContact(String contact) {
        this.contact = contact;
    }
    
    @Column(name="online_state", length=1)

    public String getOnlineState() {
        return this.onlineState;
    }
    
    public void setOnlineState(String onlineState) {
        this.onlineState = onlineState;
    }
    
    @Column(name="last_login_time", length=0)

    public Timestamp getLastLoginTime() {
        return this.lastLoginTime;
    }
    
    public void setLastLoginTime(Timestamp lastLoginTime) {
        this.lastLoginTime = lastLoginTime;
    }
    
    @Column(name="last_logout_time", length=0)

    public Timestamp getLastLogoutTime() {
        return this.lastLogoutTime;
    }
    
    public void setLastLogoutTime(Timestamp lastLogoutTime) {
        this.lastLogoutTime = lastLogoutTime;
    }
    
    @Column(name="last_login_ip", length=50)

    public String getLastLoginIp() {
        return this.lastLoginIp;
    }
    
    public void setLastLoginIp(String lastLoginIp) {
        this.lastLoginIp = lastLoginIp;
    }
    
    @Column(name="user_state", length=50)

    public String getUserState() {
        return this.userState;
    }
    
    public void setUserState(String userState) {
        this.userState = userState;
    }
    
    @Column(name="list_order")

    public Integer getListOrder() {
        return this.listOrder;
    }
    
    public void setListOrder(Integer listOrder) {
        this.listOrder = listOrder;
    }
    
    @Column(name="create_user", length=20)

    public String getCreateUser() {
        return this.createUser;
    }
    
    public void setCreateUser(String createUser) {
        this.createUser = createUser;
    }
    
    @Column(name="create_datetime", length=0)

    public Timestamp getCreateDatetime() {
        return this.createDatetime;
    }
    
    public void setCreateDatetime(Timestamp createDatetime) {
        this.createDatetime = createDatetime;
    }
    
    @Column(name="modify_user", length=20)

    public String getModifyUser() {
        return this.modifyUser;
    }
    
    public void setModifyUser(String modifyUser) {
        this.modifyUser = modifyUser;
    }
    
    @Column(name="modify_datetime", length=0)

    public Timestamp getModifyDatetime() {
        return this.modifyDatetime;
    }
    
    public void setModifyDatetime(Timestamp modifyDatetime) {
        this.modifyDatetime = modifyDatetime;
    }
    
    @Column(name="delete_flag", length=1)

    public String getDeleteFlag() {
        return this.deleteFlag;
    }
    
    public void setDeleteFlag(String deleteFlag) {
        this.deleteFlag = deleteFlag;
    }
   








}


下面来看看完整的UserInfoService.java



package com.example.service.impl;

import java.util.List;

import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.data.domain.Sort;

import com.example.common.BaseService;
import com.example.entity.UserInfo;

public interface UserInfoService extends BaseService<UserInfo, String>{

	//根据username查询用户信息
	public List<UserInfo> findByUsername(String username);

	//根据用户名和密码查询用户信息
	public UserInfo findByUsernameAndPassword(String name, String password);

	//原生SQL查询,且绑定参数的方式
	//@Query(value = "SELECT * FROM USERS WHERE EMAIL_ADDRESS = ?1", nativeQuery = true)
	//@Query("select u from UserInfo u where u.email = ?1")
	List<UserInfo> findByEmail(String email);
	
	//参数查询
	// @Query("select u from User u where u.firstname = :firstname or u.lastname = :lastname")
	// UserInfo findByLastnameOrFirstname(@Param("lastname") String lastname,
	                                // @Param("firstname") String firstname);

	// Enables the distinct flag for the query
	List<UserInfo> findDistinctUserInfoByUsernameOrEmail(String name,
			String email);

	List<UserInfo> findUserInfoDistinctByUsernameOrEmail(String name,
			String email);

	List<UserInfo> findTop10ByUsername(String Username, Pageable pageable);

	// Enabling ignoring case for an individual property
	List<UserInfo> findByUsernameIgnoreCase(String name);

	// Enabling ignoring case for all suitable properties
	List<UserInfo> findByUsernameAndEmailAllIgnoreCase(String name,
			String email);

	// Enabling static ORDER BY for a query
	List<UserInfo> findByUsernameOrderByUsernameAsc(String name);

	List<UserInfo> findByUsernameOrderByUsernameDesc(String name);
	
	//Between, LessThan, GreaterThan, Like
	
	
	UserInfo findFirstByOrderByUsernameAsc();

	//UserInfo findTopByOrderByAgeDesc();

	//Page<UserInfo> queryFirst10ByUsername(String Username, Pageable pageable);

	Slice<UserInfo> findTop3ByUsername(String Username, Pageable pageable);

	List<UserInfo> findFirst10ByUsername(String Username, Sort sort);
	
}


上面的命名具有一定的规范,总结下来就是:

find+全局修饰+By+实体属性名+Keyword+ .(其它实体属性)+OrderBy+
排序实体属性+排序方式

例如:
findDistnctByUsernameIgnoreCaseAndPasswordOrderByAgeDsc(String 
firstName,String lastName){.}
其中:Distnct是全局修饰(非必须),Username和Password是实体的属性名,
And、IgnoreCase都是关键字,Age是排序属性,Desc是排序方式

常用词如下:
全局修饰:Distnct,Top,First
keyword:如图1,2所示
IgnoreCase,Betwen,Equals,LesThan,GreaterThan,After,Before.
排序方式:Asc,Desc
连接词:And,Or


关键字列表:


wKioL1eZXl-hbdKrAAE0yBiA4xk262.jpg-wh_50

wKiom1eZXoChQlSoAAEXRCaPnsY155.jpg-wh_50

图1. 查询的关键字


上面的查询方式,详细大伙都已经非常的清楚了,毕竟上述查询有些也不能满足我们的需求,因此下一篇文章将讲述如何进行自定义的查询方式。