还在用 RedisTemplate?试试 Redis 官方 ORM 框架吧,用起来够优雅

之前在SpringBoot项目中,我一直使用RedisTemplate来操作Redis中的数据,这也是Spring官方支持的方式。对比Spring Data对MongoDB和ES的支持,这种使用Template的方式确实不够优雅!最近发现Redis官方新推出了Redis的专属ORM框架RedisOM,用起来够优雅,推荐给大家!

RedisOM简介

RedisOM是Redis官方推出的ORM框架,是对Spring Data Redis的扩展。由于Redis目前已经支持原生JSON对象的存储,之前使用RedisTemplate直接用字符串来存储JOSN对象的方式明显不够优雅。通过RedisOM我们不仅能够以对象的形式来操作Redis中的数据,而且可以实现搜索功能!

JDK 11安装

由于目前RedisOM仅支持JDK 11以上版本,我们在使用前得先安装好它。

还在用 RedisTemplate?试试 Redis 官方 ORM 框架吧,用起来够优雅

还在用 RedisTemplate?试试 Redis 官方 ORM 框架吧,用起来够优雅

还在用 RedisTemplate?试试 Redis 官方 ORM 框架吧,用起来够优雅

使用

接下来我们以管理存储在Redis中的商品信息为例,实现商品搜索功能。注意安装Redis的完全体版本RedisMod,具体可以参考RediSearch 使用教程 。



    com.redis.om
    redis-om-spring
    0.3.0-SNAPSHOT


    
        snapshots-repo
        https://s01.oss.sonatype.org/content/repositories/snapshots/
    

spring:
  redis:
    host: 192.168.3.105 # Redis服务器地址
    database: 0 # Redis数据库索引(默认为0)
    port: 6379 # Redis服务器连接端口
    password: # Redis服务器连接密码(默认为空)
    timeout: 3000ms # 连接超时时间
@SpringBootApplication
@EnableRedisDocumentRepositories(basePackages = "com.macro.mall.tiny.*")
public class MallTinyApplication {

    public static void main(String[] args) {
        SpringApplication.run(MallTinyApplication.class, args);
    }

}
/**
 * 商品实体类
 * 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 {
}
/**
 * 使用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("导入商品")
    @PostMapping("/import")
    public CommonResult importList() {
        productRepository.deleteAll();
        List productList = LocalJsonUtil.getListFromJson("json/products.json", Product.class);
        for (Product product : productList) {
            productRepository.save(product);
        }
        return CommonResult.success(null);
    }

    @ApiOperation("创建商品")
    @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 detail(@PathVariable Long id) {
        Optional result = productRepository.findById(id);
        return CommonResult.success(result.orElse(null));
    }

    @ApiOperation("分页查询")
    @GetMapping("/page")
    public CommonResult> page(@RequestParam(defaultValue = "1") Integer pageNum,
                                            @RequestParam(defaultValue = "5") Integer pageSize) {
        Pageable pageable = PageRequest.of(pageNum - 1, pageSize);
        Page pageResult = productRepository.findAll(pageable);
        return CommonResult.success(pageResult.getContent());
    }

}
还在用 RedisTemplate?试试 Redis 官方 ORM 框架吧,用起来够优雅

还在用 RedisTemplate?试试 Redis 官方 ORM 框架吧,用起来够优雅

还在用 RedisTemplate?试试 Redis 官方 ORM 框架吧,用起来够优雅

还在用 RedisTemplate?试试 Redis 官方 ORM 框架吧,用起来够优雅

/**
 * 商品管理Repository
 * Created by macro on 2022/3/1.
 */
public interface ProductRepository extends RedisDocumentRepository {
    /**
     * 根据品牌名称查询
     */
    List findByBrandName(String brandName);

    /**
     * 根据名称或副标题搜索
     */
    List findByNameOrSubTitle(String name, String subTitle);
}
/**
 * 使用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("根据品牌查询")
    @GetMapping("/getByBrandName")
    public CommonResult> getByBrandName(String brandName) {
        List resultList = productRepository.findByBrandName(brandName);
        return CommonResult.success(resultList);
    }

    @ApiOperation("根据名称或副标题搜索")
    @GetMapping("/search")
    public CommonResult> search(String keyword) {
        List resultList = productRepository.findByNameOrSubTitle(keyword, keyword);
        return CommonResult.success(resultList);
    }

}
还在用 RedisTemplate?试试 Redis 官方 ORM 框架吧,用起来够优雅

还在用 RedisTemplate?试试 Redis 官方 ORM 框架吧,用起来够优雅

还在用 RedisTemplate?试试 Redis 官方 ORM 框架吧,用起来够优雅

总结

今天体验了一把RedisOM,用起来确实够优雅,和使用Spring Data来操作MongoDB和ES的方式差不多。不过目前RedisOM只发布了快照版本,期待Release版本的发布,而且Release版本据说会支持JDK 8的!

来源:https://mp.weixin.qq.com/s/s2MoZuapTiAUesYXpYSbhw

作者:梦想de星空

展开阅读全文

页面更新:2024-03-04

标签:副标题   快照   注解   仓库   框架   优雅   对象   名称   版本   文档   官方   商品   数据

1 2 3 4 5

上滑加载更多 ↓
推荐阅读:
友情链接:
更多:

本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828  

© CopyRight 2008-2024 All Rights Reserved. Powered By bs178.com 闽ICP备11008920号-3
闽公网安备35020302034844号

Top