一、项目搭建
- 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