继承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"));
}
}
输出结果: