电子商务网站营销的方法郑州哪有做网站的公司
- 作者: 五速梦信息网
- 时间: 2026年03月21日 11:22
当前位置: 首页 > news >正文
电子商务网站营销的方法,郑州哪有做网站的公司,怎样建自己的网站,昆明哪家网站做得好文章目录 一、SpringBoot整合二、RedisAutoConfiguration自动配置类1、整合测试一下 三、自定义RedisTemplete1、在测试test中使用自定义的RedisTemplete2、自定义RedisTemplete后测试 四、在企业开发中#xff0c;大部分情况下都不会使用原生方式编写redis1、编写RedisUtils代… 文章目录 一、SpringBoot整合二、RedisAutoConfiguration自动配置类1、整合测试一下 三、自定义RedisTemplete1、在测试test中使用自定义的RedisTemplete2、自定义RedisTemplete后测试 四、在企业开发中大部分情况下都不会使用原生方式编写redis1、编写RedisUtils代替原生操作RedisTemplate的方式2、测试使用redisUtils操作redis 一、SpringBoot整合 SpringBoot 操作数据spring-data jpa jdbc mongodb redis SpringData 也是和 SpringBoot 齐名的项目 说明 在 SpringBoot2.x 之后原来使用的jedis 被替换为了 lettuce? jedis : 采用直连多个线程操作的话是不安全的如果想要避免不安全的使用 jedis pool 连接 池 更像 BIO 模式 lettuce : 采用netty实例可以在多个线程中进行共享不存在线程不安全的情况可以减少线程数据 了更像 NIO 模式 二、RedisAutoConfiguration自动配置类 自动配置类springboot已经帮我们写好来看看源码 源码分析下面是springboot中 RedisAutoConfiguration自动配置类 的源码中的方法 Bean ConditionalOnMissingBean(name redisTemplate) // 我们可以自己定义一个 redisTemplate来替换这个默认的 public RedisTemplateObject, ObjectredisTemplate(RedisConnectionFactory redisConnectionFactory)throws UnknownHostException {// 默认的 RedisTemplate 没有过多的设置redis 对象都是需要序列化// 两个泛型都是 Object, Object 的类型我们后使用需要强制转换 String, ObjectRedisTemplateObject, Object template new RedisTemplate();template.setConnectionFactory(redisConnectionFactory);return template; } Bean ConditionalOnMissingBean // 由于 String 是redis中最常使用的类型所以说单独提出来了一 个bean public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory)throws UnknownHostException {StringRedisTemplate template new StringRedisTemplate();template.setConnectionFactory(redisConnectionFactory);return template; }1、整合测试一下 导入springboot-redis的 依赖 !– 操作redis – dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-data-redis/artifactId /dependency配置连接在application.properties中
配置redis
spring.redis.host127.0.0.1
spring.redis.port6379测试在测试类中自动装配RedisTemplate因为springboot已经通过RedisAutoConfiguration配置类帮我们注入了RedisTemplate然后通过RedisTemplate 去操作 Redis
SpringBootTest
class Redis02SpringbootApplicationTests {Autowiredprivate RedisTemplate redisTemplate;Testvoid contextLoads() {// redisTemplate 操作不同的数据类型api和我们的指令是一样的// opsForValue 操作字符串 类似String// opsForList 操作List 类似List// opsForSet// opsForHash// opsForZSet// opsForGeo// opsForHyperLogLog// 除了进本的操作我们常用的方法都可以直接通过redisTemplate操作比如事务和基本的 CRUD// 获取redis的连接对象// RedisConnection connection redisTemplate.getConnectionFactory().getConnection();// connection.flushDb();// connection.flushAll();redisTemplate.opsForValue().set(mykey,关注狂神说公众号);System.out.println(redisTemplate.opsForValue().get(mykey));}
}关于对象的保存 对象没有序列化传输保存会报错 序列化对象才能在redis的value中正常保存并获取对象 public class User implements Serializable {private String name;private int age;
}三、自定义RedisTemplete
默认序列化是jdk的序列化方式自定义RedisTemplete配置config可以自定义序列化方式 可以自己配置具体的序列化方式代码如下所示我们来编写一个自己的 RedisTemplete下面是一个固定的模板可以直接用
自己写一个配置类注入自己定义的RedisTemplate
package com.kuang.config;
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.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
Configuration
public class RedisConfig {// 这是我给大家写好的一个固定模板大家在企业中拿去就可以直接使用// 自己定义了一个 RedisTemplateBeanSuppressWarnings(all)public RedisTemplateString, Object redisTemplate(RedisConnectionFactory factory) {// 我们为了自己开发方便一般直接使用 String,ObjectRedisTemplateString, Object template new RedisTemplateString,Object();template.setConnectionFactory(factory);//Json序列化配置,创建序列化方式的对象Jackson2JsonRedisSerializer jackson2JsonRedisSerializer newJackson2JsonRedisSerializer(Object.class);ObjectMapper om new ObjectMapper();om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);jackson2JsonRedisSerializer.setObjectMapper(om);// String 的序列化StringRedisSerializer stringRedisSerializer new StringRedisSerializer();// key采用String的序列化方式template.setKeySerializer(stringRedisSerializer);// hash的key也采用String的序列化方式template.setHashKeySerializer(stringRedisSerializer);// value序列化方式采用jacksontemplate.setValueSerializer(jackson2JsonRedisSerializer);// hash的value序列化方式采用jacksontemplate.setHashValueSerializer(jackson2JsonRedisSerializer);template.afterPropertiesSet();return template;}
}所有的redis操作其实对于java开发人员来说十分的简单更重要是要去理解redis的思想和每一种数据结构的用处和作用场景
1、在测试test中使用自定义的RedisTemplete 首先自动装配因为在配置类中注入过了 装配时要选择自定义的RedisTemplate而不是源码中的 2、自定义RedisTemplete后测试 //保存对象到redisTestpublic void test() throws JsonProcessingException {User user new User(hhb, 20);//String jsonUser new ObjectMapper().writeValueAsString(user); //转换为json字符串redisTemplate.opsForValue().set(user,user);System.out.println(redisTemplate.opsForValue().get(user));}结果 因为key 已经采用 String序列化方式 自定义RedisTemplate并为其设置序列化方式之前key是乱码 四、在企业开发中大部分情况下都不会使用原生方式编写redis
不会像下面这样
1、编写RedisUtils代替原生操作RedisTemplate的方式
在真实开发中或者公司一般都会看到一个封装的 RedisUtil工具类文件在文章标题处给出RedisUtil工具类附件因为文件代码太长所以提供文件附件百度csdn中应该也有很多容易搜到
示例部分代码如下都是封装了一些redis的操作把redis的操作封装为了RedisUtils的方法。
Component
SuppressWarnings({unchecked, all})
public class RedisUtils {private static final Logger log LoggerFactory.getLogger(RedisUtils.class);private RedisTemplateObject, Object redisTemplate;// Value(${jwt.online-key})// private String onlineKey;public RedisUtils(RedisTemplateObject, Object redisTemplate) {this.redisTemplate redisTemplate;this.redisTemplate.setHashKeySerializer(new StringRedisSerializer());this.redisTemplate.setKeySerializer(new StringRedisSerializer());this.redisTemplate.setStringSerializer(new StringRedisSerializer());}/*** 指定缓存失效时间** param key 键* param time 时间(秒)/public boolean expire(String key, long time) {try {if (time 0) {redisTemplate.expire(key, time, TimeUnit.SECONDS);}} catch (Exception e) {log.error(e.getMessage(), e);return false;}return true;}/** 指定缓存失效时间** param key 键* param time 时间(秒)* param timeUnit 单位/public boolean expire(String key, long time, TimeUnit timeUnit) {try {if (time 0) {redisTemplate.expire(key, time, timeUnit);}} catch (Exception e) {log.error(e.getMessage(), e);return false;}return true;}/** 根据 key 获取过期时间** param key 键 不能为null* return 时间(秒) 返回0代表为永久有效*/public long getExpire(Object key) {return redisTemplate.getExpire(key, TimeUnit.SECONDS);}
}2、测试使用redisUtils操作redis
因为RedisUtils被注册成了组件所以可以Autowired自动装配 输出结果如下
- 上一篇: 电子商务网站系统开发十堰论坛网站
- 下一篇: 电子商务网站硬件建设的核心前几年做那个网站致富
相关文章
-
电子商务网站系统开发十堰论坛网站
电子商务网站系统开发十堰论坛网站
- 技术栈
- 2026年03月21日
-
电子商务网站推广案例wordpress apple pro
电子商务网站推广案例wordpress apple pro
- 技术栈
- 2026年03月21日
-
电子商务网站体系结构有哪些?大数据精准营销
电子商务网站体系结构有哪些?大数据精准营销
- 技术栈
- 2026年03月21日
-
电子商务网站硬件建设的核心前几年做那个网站致富
电子商务网站硬件建设的核心前几年做那个网站致富
- 技术栈
- 2026年03月21日
-
电子商务网站怎么建设北京到广州航班时刻表
电子商务网站怎么建设北京到广州航班时刻表
- 技术栈
- 2026年03月21日
-
电子商务网站中的信息技术阿里巴巴微分销系统定制开发
电子商务网站中的信息技术阿里巴巴微分销系统定制开发
- 技术栈
- 2026年03月21日






