Spring Data Redis Repositories

本文介绍如何使用Spring Data Redis结合CrudRepository接口实现基本的增删改查操作,并通过自定义方法完成根据属性名查询的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

继承CrudRepository接口,不能实现根据属性名查询(目前没发现可以这样操作可以得到想要的结果)

实体

package cn.hjw.btmanage.entity;

import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.redis.core.RedisHash;

/**
 * @author
 * @date 2019/7/14 16:49
 */
@RedisHash("Business")
@Data
public class Business {
    @Id
    private String bizCode;

    private String bizName;
}

数据访问层

package cn.hjw.btmanage.repository;

import cn.hjw.btmanage.entity.Business;
import org.springframework.data.repository.CrudRepository;

/**
 * @author 
 * @date 2019/7/14 16:51
 */
public interface BusinessRepository extends CrudRepository<Business,String> {
    public Business getByBizCode(String bizCode);
}

测试代码

package cn.hjw.btmanage.service;

import cn.hjw.btmanage.config.ApplicationConfig;
import cn.hjw.btmanage.entity.Business;
import cn.hjw.btmanage.test.BizTableServiceImplTest;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import static org.junit.Assert.*;

/**
 * @author
 * @date 2019/7/14 16:55
 */

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {ApplicationConfig.class, BizTableServiceImplTest.TestConfig.class})
public class BusinessServiceImplTest {

    @Autowired
    BusinessService businessService;
    @Test
    public void addBusiness() {
        String[] businessCode = {"CRE_ORD","QUE_ORD","THIRD_CAB","PAGE_CAB"};
        String[] businessName = {"预下单","交易查询","第三方回调","页面回显"};
        for (int i = 0; i < businessCode.length; i++) {
            Business business = new Business();
            business.setBizName(businessName[i]);
            business.setBizCode(businessCode[i]);
            businessService.addBusiness(business);
        }
        System.out.println(businessService.getByBizCode("CRE_ORD"));
        System.out.println(businessService.findOne("CRE_ORD"));
    }
}

 

输出结果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值