之前在SpringBoot項(xiàng)目中,我一直使用RedisTemplate來操作Redis中的數(shù)據(jù),這也是Spring官方支持的方式。對比Spring Data對MongoDB和ES的支持,這種使用Template的方式確實(shí)不夠優(yōu)雅!最近發(fā)現(xiàn)Redis官方新推出了Redis的專屬ORM框架RedisOM,用起來夠優(yōu)雅,推薦給大家!
RedisOM簡介 RedisOM是Redis官方推出的ORM框架,是對Spring Data Redis的擴(kuò)展。由于Redis目前已經(jīng)支持原生JSON對象的存儲,之前使用RedisTemplate直接用字符串來存儲JOSN對象的方式明顯不夠優(yōu)雅。通過RedisOM我們不僅能夠以對象的形式來操作Redis中的數(shù)據(jù),而且可以實(shí)現(xiàn)搜索功能!
JDK 11安裝 由于目前RedisOM僅支持JDK 11以上版本,我們在使用前得先安裝好它。
首先下載JDK 11,這里推薦去清華大學(xué)開源軟件鏡像站下載,下載地址:https://mirrors.tuna./AdoptOpenJDK/11/jdk/x64/ 然后在IDEA的項(xiàng)目配置中,將對應(yīng)模塊的JDK依賴版本設(shè)置為JDK 11即可。 使用 接下來我們以管理存儲在Redis中的商品信息為例,實(shí)現(xiàn)商品搜索功能。注意安裝Redis的完全體版本RedisMod,具體可以參考Red iSearch 使用教程 。
<> <>中添加RedisOM相關(guān)依賴;pom.xmlction style="margin-top: 5px;margin-bottom: 5px;line-height: 26px;color: rgb(1, 1, 1);">首先在< font=""><>i> <!--Redis OM 相關(guān)依賴--> <dependency > <groupId > com.redis.om</groupId > <artifactId > redis-om-spring</artifactId > <version > 0.3.0-SNAPSHOT</version > </dependency > <> <>ction style="margin-top: 5px;margin-bottom: 5px;line-height: 26px;color: rgb(1, 1, 1);">由于RedisOM目前只有快照版本,還需添加快照倉庫;< font=""><>i> <repositories > <repository > <id > snapshots-repo</id > <url > https://s01.oss./content/repositories/snapshots/</url > </repository > </repositories > <> <>中添加Redis連接配置;application.ymlction style="margin-top: 5px;margin-bottom: 5px;line-height: 26px;color: rgb(1, 1, 1);">然后在配置文件< font=""><>i> spring: redis: host: 192.168 .3 .105 # Redis服務(wù)器地址 database: 0 # Redis數(shù)據(jù)庫索引(默認(rèn)為0) port: 6379 # Redis服務(wù)器連接端口 password: # Redis服務(wù)器連接密碼(默認(rèn)為空) timeout: 3000ms # 連接超時時間 <> <>注解啟用RedisOM的文檔倉庫功能,并配置好文檔倉庫所在路徑;@EnableRedisDocumentRepositoriesction style="margin-top: 5px;margin-bottom: 5px;line-height: 26px;color: rgb(1, 1, 1);">之后在啟動類上添加< font=""><>i> @SpringBootApplication @EnableRedisDocumentRepositories (basePackages = "com.macro.mall.tiny.*" )public class MallTinyApplication { public static void main (String[] args) { SpringApplication.run(MallTinyApplication.class , args ) ; } }<> <>;chinese注解標(biāo)識其為文檔對象,由于我們的搜索信息中包含中文,我們需要設(shè)置語言為@Documentction style="margin-top: 5px;margin-bottom: 5px;line-height: 26px;color: rgb(1, 1, 1);">然后創(chuàng)建商品的文檔對象,使用< font=""><>i> /** * 商品實(shí)體類 * Created by macro on 2021/10/12. */ @Data @EqualsAndHashCode (callSuper = false )@Document (language = "chinese" )public class Product { @Id private Long id; @Indexed private String productSn; @Searchable private String name; @Searchable private String subTitle; @Indexed private String brandName; @Indexed private Integer price; @Indexed private Integer count; }/** * 商品管理Repository * Created by macro on 2022/3/1. */ public interface ProductRepository extends RedisDocumentRepository <Product , Long > { }<> <>實(shí)現(xiàn)對Redis中數(shù)據(jù)的創(chuàng)建、刪除、查詢及分頁功能;Repositoryction style="margin-top: 5px;margin-bottom: 5px;line-height: 26px;color: rgb(1, 1, 1);">創(chuàng)建測試用的Controller,通過< font=""><>i> /** * 使用Redis OM管理商品 * Created by macro on 2022/3/1. */ @RestController @Api (tags = "ProductController" , description = "使用Redis OM管理商品" )@RequestMapping ("/product" )public class ProductController { @Autowired private ProductRepository productRepository; @ApiOperation ("導(dǎo)入商品" ) @PostMapping ("/import" ) public CommonResult importList () { productRepository.deleteAll(); List<Product> productList = LocalJsonUtil.getListFromJson("json/products.json" , Product.class ) ; for (Product product : productList) { productRepository.save(product); } return CommonResult.success(null ); } @ApiOperation ("創(chuàng)建商品" ) @PostMapping ("/create" ) public CommonResult create (@RequestBody Product entity) { productRepository.save(entity); return CommonResult.success(null ); } @ApiOperation ("刪除" ) @PostMapping ("/delete/{id}" ) public CommonResult delete (@PathVariable Long id) { productRepository.deleteById(id); return CommonResult.success(null ); } @ApiOperation ("查詢單個" ) @GetMapping ("/detail/{id}" ) public CommonResult<Product> detail (@PathVariable Long id) { Optional<Product> result = productRepository.findById(id); return CommonResult.success(result.orElse(null )); } @ApiOperation ("分頁查詢" ) @GetMapping ("/page" ) public CommonResult<List<Product>> page(@RequestParam (defaultValue = "1" ) Integer pageNum, @RequestParam (defaultValue = "5" ) Integer pageSize) { Pageable pageable = PageRequest.of(pageNum - 1 , pageSize); Page<Product> pageResult = productRepository.findAll(pageable); return CommonResult.success(pageResult.getContent()); } }<> <>ction style="margin-top: 5px;margin-bottom: 5px;line-height: 26px;color: rgb(1, 1, 1);">當(dāng)我們啟動項(xiàng)目時,可以發(fā)現(xiàn)RedisOM會自動為文檔建立索引;< font=""><>i> <> ction><>i/接口導(dǎo)入數(shù)據(jù),訪問地址:http://localhost:8088/swagger-u導(dǎo)入商品ction style="margin-top: 5px;margin-bottom: 5px;line-height: 26px;color: rgb(1, 1, 1);">接下來我們訪問Swagger進(jìn)行測試,先使用< font=""><>i> <>的形式命名了鍵,同時將全部的ID存儲到了一個SET集合中去了;全類名:IDction style="margin-top: 5px;margin-bottom: 5px;line-height: 26px;color: rgb(1, 1, 1);">導(dǎo)入成功后我們可以發(fā)現(xiàn)RedisOM已經(jīng)向Redis中插入了原生JSON數(shù)據(jù),以< font=""><>i><> <>ction style="margin-top: 5px;margin-bottom: 5px;line-height: 26px;color: rgb(1, 1, 1);">我們可以通過ID來查詢商品信息;< font=""><>i><> <>ction style="margin-top: 5px;margin-bottom: 5px;line-height: 26px;color: rgb(1, 1, 1);">當(dāng)然RedisOM也是支持衍生查詢的,通過我們創(chuàng)建的方法名稱就可以自動實(shí)現(xiàn)查詢邏輯,比如根據(jù)品牌名稱查詢商品,根據(jù)名稱和副標(biāo)題關(guān)鍵字來搜索商品;< font=""><>i><>/** * 商品管理Repository * Created by macro on 2022/3/1. */ public interface ProductRepository extends RedisDocumentRepository <Product , Long > { /** * 根據(jù)品牌名稱查詢 */ List<Product> findByBrandName (String brandName) ; /** * 根據(jù)名稱或副標(biāo)題搜索 */ List<Product> findByNameOrSubTitle (String name, String subTitle) ; } <>ction style="margin-top: 5px;margin-bottom: 5px;line-height: 26px;color: rgb(1, 1, 1);">在Controller中可以添加如下接口進(jìn)行測試;< font=""><>i><>/** * 使用Redis OM管理商品 * Created by macro on 2022/3/1. */ @RestController @Api (tags = "ProductController" , description = "使用Redis OM管理商品" )@RequestMapping ("/product" )public class ProductController { @Autowired private ProductRepository productRepository; @ApiOperation ("根據(jù)品牌查詢" ) @GetMapping ("/getByBrandName" ) public CommonResult<List<Product>> getByBrandName(String brandName) { List<Product> resultList = productRepository.findByBrandName(brandName); return CommonResult.success(resultList); } @ApiOperation ("根據(jù)名稱或副標(biāo)題搜索" ) @GetMapping ("/search" ) public CommonResult<List<Product>> search(String keyword) { List<Product> resultList = productRepository.findByNameOrSubTitle(keyword, keyword); return CommonResult.success(resultList); } } <>ction style="margin-top: 5px;margin-bottom: 5px;line-height: 26px;color: rgb(1, 1, 1);">我們可以通過品牌名稱來查詢商品;< font=""><>i><> <>ction style="margin-top: 5px;margin-bottom: 5px;line-height: 26px;color: rgb(1, 1, 1);">也可以通過關(guān)鍵字來搜索商品;< font=""><>i><> <>ction style="margin-top: 5px;margin-bottom: 5px;line-height: 26px;color: rgb(1, 1, 1);">這類根據(jù)方法名稱自動實(shí)現(xiàn)查詢邏輯的衍生查詢有什么規(guī)則呢,具體可以參考下表。< font=""><>i><> 總結(jié) 的!JDK 8今天體驗(yàn)了一把RedisOM,用起來確實(shí)夠優(yōu)雅,和使用Spring Data來操作MongoDB和ES的方式差不多。不過目前RedisOM只發(fā)布了快照版本,期待Release版本的發(fā)布,而且Release版本據(jù)說會支持
參考資料 <> <>ction style="margin-top: 5px;margin-bottom: 5px;line-height: 26px;color: rgb(1, 1, 1);">官方文檔:https://developer./develop/java/spring/redis-om/redis-om-spring-json< font=""><>i><>ction style="margin-top: 5px;margin-bottom: 5px;line-height: 26px;color: rgb(1, 1, 1);">項(xiàng)目地址:https://github.com/redis/redis-om-spring< font=""><>i><> 項(xiàng)目源碼地址 https://github.com/macrozheng/mall-learning/tree/master/mall-tiny-redis-om