Springboot+spring-data系列之(二)redis整合示例

1、项目结构

使用ideaj快速生成springboot项目,结构如下

Springboot+spring-data系列之(二)redis整合示例_第1张图片

 

2、pom.xml依赖



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.3.RELEASE
         
    
    com.leao.springdata
    redis
    0.0.1-SNAPSHOT
    redis
    Demo project for Spring Boot

    
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter-data-redis
        
        
            org.springframework.boot
            spring-boot-starter-data-redis-reactive
        
        
            org.springframework.boot
            spring-boot-starter-webflux
        

        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            io.projectreactor
            reactor-test
            test
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


2、application.xml

spring.data.redis.repositories.enabled=true
spring.redis.host=192.168.127.128
spring.redis.port=6379
spring.redis.database=0
#spring.redis.password=

3、编写保存实体Order

package com.leao.springdata.redis;

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

import java.io.Serializable;
import java.util.Date;

@Data
@RedisHash
public class Order implements Serializable {
    @Id
    @Indexed
    private String id;
    private String orderId;
    private String content;
    private Date date;
    private Product product;

}

4、两种不同形式的使用方式 OrderDao (一种用Repository,一种tempalate)

@Repository
public interface OrderDao extends PagingAndSortingRepository {
}
package com.leao.springdata.redis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.hash.Jackson2HashMapper;
import org.springframework.stereotype.Repository;

import java.util.Map;
import java.util.Set;

@Repository
public class OrderDao2 {

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 创建对象
     */
    public void saveorder(Order order) {


        HashOperations hashOperations = redisTemplate.opsForHash();
        hashOperations.put("leihao",order.getId(),order);
    }


    /**
     * 根据用户名查询对象
     * @return
     */
    public Order findTestByName(String name,String id) {
        HashOperations hashOperations= redisTemplate.opsForHash();
        Order order = (Order)hashOperations.get(name, id);
        return order;
    }

    /**
     * 更新对象
     */
    public void updateTest(String name,String id,Order order) {
        redisTemplate.opsForHash().put(name,id,order);
    }

    /**
     * 删除对象
     * @param id
     */
    public void deleteTestById(String name,Integer id) {
        redisTemplate.opsForHash().delete(name,id);
    }

    public void findAllByName(String name) {
        Set keys = redisTemplate.opsForHash().keys(name);
        System.out.println(keys);
    }
}

5、springboot测试类单元测试

package com.leao.springdata.redis;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.Date;
import java.util.Optional;
import java.util.UUID;

@RunWith(SpringRunner.class)
@SpringBootTest
public class RedisApplicationTests {

    @Test
    public void contextLoads() {
//        test1();
        test3();
        test2();
        test4();
        test2();

    }

    @Autowired
    private OrderDao orderDao;

    @Autowired
    private OrderDao2 orderDao2;

    public void test1(){
        Order order = new Order();
        order.setContent("leao的redis测试");
        order.setDate(new Date());
        order.setId("123");
        order.setOrderId(UUID.randomUUID().toString());
        order.setProduct(new Product("纸尿片",UUID.randomUUID().toString()));
        orderDao.save(order);
        System.out.println("执行保存成功");
    }

    public void test2(){
        Optional byId = orderDao.findById("124");
        Order order = byId.orElse(null);
        System.out.println(order);
    }


    public void test2Delet(){
        orderDao.deleteById("123");
    }

    public void test3(){
        Order order = new Order();
        order.setContent("leao的redis测试2");
        order.setDate(new Date());
        order.setId("124");
        order.setOrderId(UUID.randomUUID().toString());
        order.setProduct(new Product("纸尿裤",UUID.randomUUID().toString()));
        orderDao2.saveorder(order);
    }

    public void test4(){
       //在dao里写死了key
        orderDao2.deleteTestById("leihao","124");
        orderDao2.findAllByName("leihao");
    }
}

最后,使用redismanger查看结果

 

Springboot+spring-data系列之(二)redis整合示例_第2张图片

相关代码已经上传至:https://github.com/anshitou/springboot-springdata-compose/tree/master

你可能感兴趣的:(Springboot+spring-data系列之(二)redis整合示例)