spring data rest 入門(三)自定義查詢

通過簡單的增刪改查知道了spring data rest 的基本語法,那麼帶入一些真實環境來實現一些用例。

一、密碼一欄的信息不想讓用戶看到。

在user實體類中增加

    @JsonIgnore
    private String password;

http://localhost:8081/api/user/2

返回結果:

{
    "name": "李四",
    "age": 25,
    "sex": "男",
    "datatime": "2020-02-20 01:52:26",
    "_links": {
        "self": {
            "href": "http://localhost:8081/api/user/2"
        },
        "user": {
            "href": "http://localhost:8081/api/user/2"
        }
    }
}

二、組合輸出用戶姓名、性別、年齡。

新增一個接口:

import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.rest.core.config.Projection;

import test.entity.User;

@Projection(name="list",types=User.class)
public interface userList {

	@Value("#{target.name},年齡:#{target.age},性別:#{target.sex}")
	public String getFullInfo();
}

輸入 http://localhost:8081/api/user?projection=list

將該接口配置在UserRepository上。

package test.dao;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;

import test.entity.User;

/**
 * path="user": 映射路由
 * excerptProjection 映射接口
 * User:實體類
 * Long:主鍵
 * @author white
 *
 */
@RepositoryRestResource(path="user",excerptProjection=userList.class)
public interface UserRepository extends JpaRepository<User, Integer>{

}

輸入http://localhost:8081/api/user

返回內容:(如果輸入http://localhost:8081/api/user/2,顯示不出這個組合字段)

{
    "_embedded": {
        "users": [
            {
                "fullInfo": "張三,年齡:16,性別:男",
                "_links": {
                    "self": {
                        "href": "http://localhost:8081/api/user/1"
                    },
                    "user": {
                        "href": "http://localhost:8081/api/user/1{?projection}",
                        "templated": true
                    }
                }
            },
            {
                "fullInfo": "李四,年齡:25,性別:男",
                "_links": {
                    "self": {
                        "href": "http://localhost:8081/api/user/2"
                    },
                    "user": {
                        "href": "http://localhost:8081/api/user/2{?projection}",
                        "templated": true
                    }
                }
            },
            {
                "fullInfo": "王五,年齡:32,性別:女",
                "_links": {
                    "self": {
                        "href": "http://localhost:8081/api/user/3"
                    },
                    "user": {
                        "href": "http://localhost:8081/api/user/3{?projection}",
                        "templated": true
                    }
                }
            }
        ]
    },
    "_links": {
        "self": {
            "href": "http://localhost:8081/api/user{?page,size,sort,projection}",
            "templated": true
        },
        "profile": {
            "href": "http://localhost:8081/api/profile/user"
        }
    },
    "page": {
        "size": 20,
        "totalElements": 3,
        "totalPages": 1,
        "number": 0
    }
}

三、查找姓名爲張三的信息

在UserRepository配置如下內容:


	@RestResource(path="name",rel="name")
	public String findByName(@Param("name") int name);

輸入http://localhost:8081/api/user/search/name?name=張三

返回結果:

{
    "name": "張三",
    "age": 16,
    "sex": "男",
    "datatime": "2020-02-03 01:52:14",
    "_links": {
        "self": {
            "href": "http://localhost:8081/api/user/1"
        },
        "user": {
            "href": "http://localhost:8081/api/user/1{?projection}",
            "templated": true
        }
    }
}

這裏有兩個問題:1.爲什麼請求的url要加上search?2.爲什麼方法名必須是findByName?

按照Spring Data的規範的規定,查詢方法以find | read | get開頭(比如 find、findBy、read、readBy、get、getBy),涉及查詢條件時,條件的屬性用條件關鍵字連接,要注意的是:條件屬性以首字母大寫。框架在進行方法名解析時,會先把方法名多餘的前綴截取掉,然後對剩下部分進行解析。

jpa命名規範請參考:點擊這裏

四、查詢一個姓張的信息。

在UserRepository配置如下內容:

@RestResource(path="like",rel="nameStartsWith")
	public List<User> findByNameStartsWith(@Param("name") String name);

輸入:http://localhost:8081/api/user/search/like?name=張

返回結果:

{
  "_embedded": {
    "users": [
      {
        "fullInfo": "張三,年齡:16,性別:男",
        "_links": {
          "self": {
            "href": "http://localhost:8081/api/user/1"
          },
          "user": {
            "href": "http://localhost:8081/api/user/1{?projection}",
            "templated": true
          }
        }
      }
    ]
  },
  "_links": {
    "self": {
      "href": "http://localhost:8081/api/user/search/like?name=%E5%BC%A0"
    }
  }
}

五、查詢一個姓名爲張三,性別爲男的信息。

在UserRepository配置如下內容:

@RestResource(path="more",rel="nameAndSex")
	public List<User> findByNameAndSex(@Param("name") String name,@Param("sex") String sex);

輸入:http://localhost:8081/api/user/search/more?name=張三&sex=男

返回結果:

{
  "_embedded": {
    "users": [
      {
        "fullInfo": "張三,年齡:16,性別:男",
        "_links": {
          "self": {
            "href": "http://localhost:8081/api/user/1"
          },
          "user": {
            "href": "http://localhost:8081/api/user/1{?projection}",
            "templated": true
          }
        }
      }
    ]
  },
  "_links": {
    "self": {
      "href": "http://localhost:8081/api/user/search/more?sex=%E7%94%B7&name=%E5%BC%A0%E4%B8%89"
    }
  }
}

六、將性別爲男的信息按年齡排序輸出。

在UserRepository配置如下內容:

@RestResource(path="ageDesc",rel="ageDesc")
	public List<User> findBySexOrderByAgeDesc(@Param("sex") String sex);

輸入:http://localhost:8081/api/user/search/ageDesc?sex=男

返回結果:

{
  "_embedded": {
    "users": [
      {
        "fullInfo": "李四,年齡:25,性別:男",
        "_links": {
          "self": {
            "href": "http://localhost:8081/api/user/2"
          },
          "user": {
            "href": "http://localhost:8081/api/user/2{?projection}",
            "templated": true
          }
        }
      },
      {
        "fullInfo": "張三,年齡:16,性別:男",
        "_links": {
          "self": {
            "href": "http://localhost:8081/api/user/1"
          },
          "user": {
            "href": "http://localhost:8081/api/user/1{?projection}",
            "templated": true
          }
        }
      }
    ]
  },
  "_links": {
    "self": {
      "href": "http://localhost:8081/api/user/search/ageDesc?sex=%E7%94%B7&%E5%BC%A0%E4%B8%89"
    }
  }
}

 

發佈了159 篇原創文章 · 獲贊 109 · 訪問量 12萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章