一、pom文件中
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.1.8.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
二、application.yml文件
spring:
application:
name: redis-demo
redis:
host: 127.0.0.1
port: 6379
timeout: 3000 # 连接超时时间(毫秒)
password: root1234
三、RedisConfig文件,配置Redis
package com.test.demo;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
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.Jackson2JsonRedisSerializer;
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {
StringRedisTemplate template = new StringRedisTemplate(factory);
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
template.setValueSerializer(jackson2JsonRedisSerializer);
template.afterPropertiesSet();
return template;
}
}
四、生成序列号类,PrimaryKeyService
package com.test.demo.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import java.util.Calendar;
import java.util.Date;
@Service
public class PrimaryKeyService {
@Autowired
private RedisTemplate redisTemplate;
/**
* 获取年的后两位加上一年多少天+当前小时数作为前缀
*
* @param date
* @return
*/
public String getOrderIdPrefix(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);
int hour = calendar.get(Calendar.HOUR_OF_DAY);
//补两位,因为一年最多三位数
String monthFormat = String.format("%1$02d", month + 1);
//补两位,因为日最多两位数
String dayFormat = String.format("%1$02d", day);
//补两位,因为小时最多两位数
String hourFormat = String.format("%1$02d", hour);
return year + monthFormat + dayFormat + hourFormat;
}
/**
* 生成订单
*
* @param prefix
* @return
*/
public String orderId(String prefix) {
String key = "DEMO_ORDER_ID_" + prefix;
String orderId = null;
try {
Long increment = redisTemplate.opsForValue().increment(key, 1);
//往前补6位
orderId = prefix + String.format("%1$06d", increment);
} catch (Exception e) {
System.out.println("生成订单号失败");
e.printStackTrace();
}
return orderId;
}
}
五、测试类
@Test
public void testRedis(){
long startMillis = System.currentTimeMillis();
String orderIdPrefix = primaryKeyService.getOrderIdPrefix(new Date());
// for (int i = 0; i < 10; i++) {
String aLong = primaryKeyService.orderId(orderIdPrefix);
System.out.println(aLong);
// }
long endMillis = System.currentTimeMillis();
System.out.println("生成速度:"+(endMillis-startMillis)+",单位毫秒");
}
完毕!!!