SpringBoot集成redis实现每天序列号自增--简单方案

一、项目搭建

  • pox.xml文件依赖
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.3.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-redis</artifactId>
            <version>1.3.3.RELEASE</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
  • springboot启动类
package com.wb;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

  • application.yml
server:
  port: 8001

#配置数据源
spring:
  redis:
    database: 0
    port: 6379
    host: 127.0.0.1
    jedis:
      pool:
        max-active: 500
        max-idle: 300
        min-idle: 100
  • RedisConfig配置类
package com.wb.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 *  redis配置类
 *  @EnableCaching:开启缓存注解
 **/
@Configuration
@EnableCaching
public class RedisConfig {

    @Autowired
    private RedisConnectionFactory connectionFactory;

    @Bean
    public RedisTemplate redisTemplate(){
        RedisTemplate<String,Object> redisTemplate=new RedisTemplate<>();
        redisTemplate.setConnectionFactory(connectionFactory);
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }

    @Bean
    public StringRedisTemplate stringRedisTemplate(){
        StringRedisTemplate stringRedisTemplate=new StringRedisTemplate();
        stringRedisTemplate.setConnectionFactory(connectionFactory);
        return stringRedisTemplate;
    }
}
  • controller
package com.wb.controller;


import com.wb.service.ProduceImerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/ProduceImeiAdd")
public class ProduceImeiController {

    @Autowired
    ProduceImerService produceImerService;

    @PostMapping("/Produce")
    public String produceSerialNumber() {
        String s = produceImerService.produceSerialNumber();
        return s;
    }
}

  • service
package com.wb.service;

public interface ProduceImerService {
    public String produceSerialNumber();
}

  • Impl
package com.wb.service.impl;


import com.wb.service.ProduceImerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;

@Service

public class ProduceImeiServiceImpl implements ProduceImerService {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    //生成一个序列号,每天从0开始自增,
    public String produceSerialNumber() {

        SimpleDateFormat yyMMdd = new SimpleDateFormat("yyMMdd");
        String firstToSixthNumber = yyMMdd.format(new Date());
        if (stringRedisTemplate.hasKey(firstToSixthNumber)) {
            stringRedisTemplate.opsForValue().increment(firstToSixthNumber, 1);
            stringRedisTemplate.expire(firstToSixthNumber, 1000*60*24, TimeUnit.MILLISECONDS);
        } else {
            stringRedisTemplate.opsForValue().set(firstToSixthNumber, String.valueOf(0));
        }
        String tenthToFifteenthNumber = String.format("%06d",Integer.parseInt(stringRedisTemplate.opsForValue().get(firstToSixthNumber)));
        return firstToSixthNumber + tenthToFifteenthNumber;
    }
}

二、项目结构图

在这里插入图片描述

三、postman测试

项目启动时先启动redis
在这里插入图片描述
在这里插入图片描述

测试路径:
localhost:8001/ProduceImeiAdd/Produce
在这里插入图片描述

四、项目下载地址

Demo:easy-rdeis-demo.zip

  • 21
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值