从淘宝的支付功能学习到什么
昨天我们写了优惠卷功能和商品展示的功能
有人就说写的不怎么好,今天就来改进了,哈哈,谢谢大家呀,思路还是不改变,将枚举代替成redis就可以
思考一下:
当时的枚举用来干什么了,mybaits读取数据库获取用户的优惠卷,进行判断中国优惠卷,满多少减多少,通过读取数据库优惠卷的名字,进行枚举判断获取当前总钱数,进行判断减多少,再减这个减以后的钱返回给前端,就实现
淘宝这个效果图
这个提供只是带大家去看图写代码,可能真实的代码不是这种的,正常,但是,主要的目的,对于这种图,我们怎么去分析问题,去写代码,去设计数库和redis才是这几篇博客要告诉你的。思考不用枚举,用redis怎么实现优惠卷的功能?
redis有几大数据类型
这里就使用set集合的类型
每一个店铺都是一个set集合这样的话,保证优惠卷不会重复
分析问题1通过去店铺名字进行加密,作为set集合的key
value就是店铺所有商品和通用的优惠卷,保证设计的合理性
2当用户点击的时候,前端传递一个商品的id和店铺的id,到后端,通过商品和店铺的进行一对一,进行查询获取店铺的名字和商品的名字和商品的信息,通过用户操作,判断用户的优惠卷是不是存在这个商品的,再进一步,获取用户购买商品的总钱,进行判断,默认使用用户的优惠卷
3我们就拿这些优惠卷去查询这个店铺所有的优惠卷,再通过获取用户的优惠卷,优先这个商品的优惠卷,第一个使用
实战配置redis
ConfigurationpublicclassRedisCacheConfig{BeanpublicRedisTemplateString,ObjectredisTemplate(RedisConnectionFactory
redisConnectionFactory){
RedisTemplateString,Objecttemplate=newRedisTemplate();//配置序列化模式Jackson2JsonRedisSerializerjackson2JsonRedisSerializer=newJacks
on2JsonRedisSerializer(Object.class);
template.setConnectionFactory(redisConnectionFactory);ObjectMapperobjectMapper=newObjectMapper();objectMapper.setVisibility(PropertyAccessor.ALL,JsonAutoDetect.Visibility.ANY);objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);jackson2JsonRedisSerializer.setObjectMapper(objectMapper);//String序列化StringRedisSerializerstringRedisSerializer=newStringRedisSerializer();
//key采用String序列化
template.setKeySerializer(stringRedisSerializer);//posh的key的采用s
tring的序列化
template.setHashKeySerializer(stringRedisSerializer);//value的序
列化采用json
template.setValueSerializer(jackson2JsonRedisSerializer);//hash的序列化采用jsontemplate.setHashKeySerializer(jackson2JsonRedisSerializer);template.afterPropertiesSet();returntemplate;}}由于上次我们好像没有建店铺这个表,我们去可以去设计一下,思考一下
店铺这个表,需要什么,店铺id,店铺名字,店铺类别
设计好以后,我们进行思考怎么进行修改可能遇到的问题字符串加密,不知道怎么加密
不知道怎么改,不知道要改什么地方
进行实战dependencygroupIdorg.apache.shiro/groupIdartifactIdshiro-spring/artifactIdversion1.3.2/version/dependency我们使用是shrio提供的加密的方法ComponentpublicclassEncryptiontoolsutil{//加密工具类publicstaticStringGetencryption(Stringpassword){//进行加密注册和登入
Md5Hashhash=newMd5Hash(password,"zhuddewjdwekdh
ewubidfbewiufdyuiwebnuji");
returnhash.toHex();}}修改优惠卷的数据库修改完,进行店铺名字加密加入redis里面这个就是上一篇的枚举里面
错误的理解记住商品的id是不重复的,所有不用再根据店铺id进行获取相应的优惠卷
这个也是你回答面试一个,你遇到的问题,哈哈,但是思考一下,这个的通用的优惠卷,是一个问题,哈哈,怎么解决这个问题,这里的设计是通用优惠卷不属于任何一家店铺的,字段为0,所以不用担心
这样写好以后,进行写业务
有发现问题了
进行修改店铺表,刚开始的思考的时候,我们思考将,通用的优惠卷放入每一个店铺的redis里面,但是发现,sql查询出现问题,我设计为字段为0,但是这样不好查询,
更改思路:
将通用的优惠卷放入一个单个的redis里面,店铺表增加一个通用优惠卷的店铺,进行这样就可以了
发现成功了,哈哈上面的设计不合理,但是这个功能,你以后可以跟面试官说
ComponentpublicclassDiscountvolumetools{AutowiredprivateRedisTemplateredisTemplate;//优惠卷的工具类publicCouponenumerationgetCoupCouponenumeration(Stringshopingname,Stringvalues){Stringgetencryption=Encryptiontoolsutil.Getencryption(shopingname);SetCouponenumerationset=redisTemplate.opsForSet().members(getencryption);for(Couponenumerationcouponenumeration:set){//map.keys()if(couponenumeration.getCoupnename().equals(values)){returncouponenumeration;}}returnnull;}}工具类
ServiceTransactionalpublicclassCouponenumertionserverimpimplementsCouponenumertionserver{AutowiredprivateCuuponuserdaocuuponuserdao;AutowiredDiscountvolumetoolsdiscountvolumetools;OverridepublicdoubleAccordingtotheIDoftheproductpassedbytheuserthepref
erentialvolumeoftheproductcanbefound(doublesum,Integer
转载请注明:http://www.sonphie.com/lcbx/14365.html