您好,登錄后才能下訂單哦!
這篇文章主要介紹了Spring Boot2發布與調用REST服務過程解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
Redis是一個key-value數據庫,支持存儲的value類型包括string(字符串)、list(鏈表)、set(集合)、zset(sorted set --有序集合)和hash(哈希類型)。在Java中,使用較為廣泛的客戶端有Redisson、Jedis。Spring Data Redis模塊默認使用Jedis。
開發環境:IntelliJ IDEA 2019.2.2
Spring Boot版本:2.1.8
一、項目構建
1、新建一個名稱為demo的Spring Boot項目。
2、pom.xml
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency>
3、application.yml
配置Redis的相關屬性。
spring: redis: port: 6379 password: 123456 host: localhost
二、實體類
1、User.java
映射Redis的數據:
(1) 類使用@RedisHash注解表示User對象會以hash類型保存;
(2) 字段id使用@Id注解表示自動生成guid;
(3) 字段使用@Indexed表示建立索引,在方法名查詢時需要設置此注解;
package com.example.demo.entity; import org.springframework.data.annotation.Id; import org.springframework.data.redis.core.RedisHash; import org.springframework.data.redis.core.index.Indexed; @RedisHash("user") public class User { @Id private String id; @Indexed private String name; @Indexed private Integer age; public String getId() { return id; } public void setId(String 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; } }
三、數據訪問層
1、數據訪問層 UserRepository.java
繼承CrudRepository可以不需要任何實現,擁有基本的CRUD操作。也可以手動添加代碼實現方法名查詢。
package com.example.demo.repository; import com.example.demo.entity.User; import org.springframework.data.repository.CrudRepository; import java.util.List; public interface UserRepository extends CrudRepository<User,String> { //方法名查詢:根據字段name查詢 List<User> findByName(String name); //方法名查詢:根據字段name和age的And查詢 List<User> findByNameAndAge(String name, Integer age); //方法名查詢:根據字段name和age的Or查詢 List<User> findByNameOrAge(String name, Integer age); }
四、業務層
1、UserRepository.java
package com.example.demo.service; import com.example.demo.entity.User; import com.example.demo.repository.UserRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.ArrayList; import java.util.Iterator; import java.util.List; @Service public class UserService { @Autowired UserRepository userRepository; public List<User> getUsers(){ Iterable<User> users = userRepository.findAll(); List<User> datas = new ArrayList<User>(); for(Iterator<User> it = users.iterator(); it.hasNext();){ User u = it.next(); datas.add(u); } return datas; } public void save(User user) { userRepository.save(user); } public List<User> findByName(String name){ return userRepository.findByName(name); } public List<User> findByNameAndAge(String name, Integer age){ return userRepository.findByNameAndAge(name, age); } public List<User> findByNameOrAge(String name, Integer age){ return userRepository.findByNameOrAge(name, age); } }
五、控制器
1、UserController.java
package com.example.demo; import com.example.demo.entity.User; import com.example.demo.service.UserService; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; import java.util.List; @RestController public class UserController { @Resource UserService userService; @RequestMapping("/save") public String save(){ for(int i=1;i<=20;i++){ User user = new User(); user.setName("a" + i); user.setAge(i); userService.save(user); } return "添加成功"; } @RequestMapping("/getUsers") public List<User> getUsers(){ List<User> users = userService.getUsers(); return users; } @RequestMapping("/findByName") public List<User> findByName(String name){ return userService.findByName(name); } @RequestMapping("/findByNameAndAge") public List<User> findByNameAndAge(String name, Integer age){ return userService.findByNameAndAge(name, age); } @RequestMapping("/findByNameOrAge") public List<User> findByNameOrAge(String name, Integer age){ return userService.findByNameOrAge(name, age); } }
啟動服務器,瀏覽器訪問:http://localhost:8080/save 生成20條測試數據,訪問http://localhost:8080/getUsers,頁面顯示如下:
[{"id":"8da6f5ad-b24c-4725-9c69-4d709369eac7","name":"a14","age":14},{"id":"b62b2028-4d41-41be-aeff-e2b56776dd87","name":"a10","age":10},{"id":"c8330a2a-902b-4db9-b262-65d182027eac","name":"a6","age":6},{"id":"cc7b5a91-0ca6-4577-891a-835a1cebef6f","name":"a11","age":11},{"id":"09f8f2e3-2cf8-48f7-a535-1897b7cc0f35","name":"a1","age":1},{"id":"3344413f-8128-42ad-8343-304005c87e5f","name":"a7","age":7},{"id":"db74f020-f3e3-4422-8013-c515b9d8551d","name":"a5","age":5},{"id":"7fc46621-ecaf-47c6-bae3-f2061104d802","name":"a2","age":2},{"id":"dcdcdda0-bab0-450a-b6e6-fdce410b01fd","name":"a8","age":8},{"id":"b456f4fd-158f-43e9-86cf-0340b809ffab","name":"a12","age":12},{"id":"4ffb6019-21b1-4880-8ec3-2703f561d028","name":"a15","age":15},{"id":"7e45d738-7a31-4b24-928e-a9da3583ef6d","name":"a4","age":4},{"id":"7992e502-4049-4d40-bc35-aab72adbe335","name":"a19","age":19},{"id":"3df7b2ac-6bb0-496d-91d2-05ac763e5f68","name":"a13","age":13},{"id":"7c94edba-f91e-42fe-b8b4-7c6a9ba82747","name":"a3","age":3},{"id":"39402099-17ba-4368-ac23-dd5e9fe1d8ae","name":"a17","age":17},{"id":"e49750d6-ce80-4764-87d3-1566d9263360","name":"a18","age":18},{"id":"499e1a9a-b947-4eec-8d37-d1cdf7ac1179","name":"a16","age":16},{"id":"04f0cb4e-c17b-4b4e-b135-e6c222aace21","name":"a9","age":9},{"id":"ac48ce35-5a38-48b8-90b9-6b999c3fd242","name":"a20","age":20}]
其它控制器方法略。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。