精华内容
下载资源
问答
  • springboot项目**中国加油,武汉加油!**篇幅较长,配合目录观看案例准备1. springboot整合Redis1.1 shop-temp新建springboot-redis1.2 编写yml1.3 Test1.3 Test第二种2. ... 基于springboot电商项目 六 整合Rabbi...

    中国加油,武汉加油!

    篇幅较长,配合目录观看

    案例准备

    1. 本案例基于springboot篇】二十一. 基于springboot电商项目 六 整合RabbitMQ

    1. springboot整合Redis

    1.1 shop-temp新建springboot-redis

    1. Spring Boot DevTools
    2. Lombok
    3. Spring Data Redis (Access+Driver)

    1.2 编写yml

    spring:
      redis:
        host: 192.168.59.100
        password: admin
    

    1.3 Test

    package com.wpj;
    
    import org.junit.jupiter.api.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.data.redis.core.RedisTemplate;
    import org.springframework.test.context.junit4.SpringRunner;
    
    @SpringBootTest
    @RunWith(SpringRunner.class)
    class SpringbootRedisApplicationTests {
    
        @Autowired
        private RedisTemplate redisTemplate;
    
        @Test
        void contextLoads() {
            redisTemplate.opsForValue().set("age", 20);
            System.out.println(redisTemplate.opsForValue().get("age"));
        }
    }
    

    1.3 Test第二种

    package com.wpj;
    
    import org.junit.jupiter.api.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.data.redis.core.RedisTemplate;
    import org.springframework.data.redis.core.StringRedisTemplate;
    import org.springframework.test.context.junit4.SpringRunner;
    
    @SpringBootTest
    @RunWith(SpringRunner.class)
    class SpringbootRedisApplicationTests {
    
        @Autowired
        private StringRedisTemplate stringRedisTemplate;
    
        @Test
        void contextLoads2() {
            stringRedisTemplate.opsForValue().set("email", "jiekami@qq.com");
            System.out.println(stringRedisTemplate.opsForValue().get("email"));
        }
    
    }
    

    在这里插入图片描述

    2. springboot操纵redis中的事务

    @Test
    public void testTransaction(){
        redisTemplate.execute(new SessionCallback() {
            @Override
            public Object execute(RedisOperations redisOperations) throws DataAccessException {
                // 开启事务
                redisOperations.multi();
                try{
                    // 操作
                    redisOperations.opsForValue().set("email","admin@qq.com");
                    // 事务提交
                    redisOperations.exec();
                } catch (Exception e) {
                    redisOperations.discard();
                    e.printStackTrace();
                }
                return null;
            }
        });
    }
    

    3. redis设置超时

    @Test
    public void testTimeout() throws InterruptedException {
        // 添加一个键值对
        stringRedisTemplate.opsForValue().set("email","jiekami@qq.com",3, TimeUnit.SECONDS);
        // 给整个键值对设置超时时间 10s
    //		stringRedisTemplate.expire("email",10, TimeUnit.SECONDS);
    
        Thread.sleep(1000);
        Long time1= stringRedisTemplate.getExpire("email");
        System.out.println(time1);
    
        Thread.sleep(4000);
        Long time2= stringRedisTemplate.getExpire("email");
        System.out.println(time2);
    }
    

    在这里插入图片描述

    展开全文
  • springboot 电商项目.txt

    2019-09-26 11:25:19
    简单的springboot项目,适合刚学完springboot想练手的同学,里面视频代码文档都很齐全
  • 今天我们继续Springboot电商项目实战系列文章。到目前为止,整个项目的架构和基础服务已经全部实现,分布式锁也已经讲过了。那么,现在应该到数据库设计及代码实现阶段,我们要注意或准备什么呢?今天先说说商品的...

    最近事情有点多,所以系列文章已停止好多天了。今天我们继续Springboot电商项目实战系列文章。到目前为止,整个项目的架构和基础服务已经全部实现,分布式锁也已经讲过了。那么,现在应该到数据库设计及代码实现阶段,我们要注意或准备什么呢?今天先说说商品的数据库表设计问题吧。

    image

    来看看上面的图片,这个商品的数据库表怎么设计呢?是不是有人会说,4张表搞定:商品分类表、商品信息表、价格表、属性表。

    image

    没错,这样是可以实现,但存在很多弊端,比如就针对上面的商品(手机),不同的颜色,不同的版本都有不同的价格。按传统的设计思路,我们把版本作为属性,不同的属性对应不同的价格,看似不错。那颜色又怎么处理呢?是不是不同的版本不同的价格,颜色你就可以随便选呢?但现实中并不是这样,同一个版本配置,不同颜色往往都是不同的价格。这又怎么办呢?你可能又会说我把颜色再设置成一个属性,颜色和版本两个属性组合设置一条价格信息。那如果还有套餐呢?我们继续这样搞,是的,完全没问题。那库存呢?是否还要放在商品信息表呢?明显这样是有问题的。比如荣耀20 版本6G 128G 蓝水翡翠 已经卖完了,但这个版本的其他颜色还有货呀。那数据库到底怎么设计呢?这就是今天要讲的话题。目前的主流解决方案SPU、SKU。

    商品的SPU, SKU实现

    首先,什么是SPU,SKU呢?

    SPU(Standard Product Unit)即标准化产品单元 SPU是商品信息聚合的最小单位,是一组可复用标准化信息的集合。

    SKU(Stock Keeping Unit)即库存量单位 SKU即库存进出计量的单位,可以是以件、盒、托盘等为单位。

    对于电商而言,SPU有一个唯一编码,一个SPU代表一个产品;SKU为一个产品不同属性、规格之间的编码。也就是说,SPU代表产品,SKU代表属性与规格;一个产品,可以是单属性产品,也可以是多属性产品,也就是说一个产品可以有一个SKU,也可以有多个SKU。

    那么,针对上面的商品,数据库表究竟怎么设计呢?

    image

    这里是简单的商品SPU、SKU表设计实现,一共包含六个表。

    1,商品分类表

    此表添加了parent_id字段,可实现无限层级的树状数据结构,parent_id=0表明当前为根节点,否则可使用递归算法来遍历分类下的所有子分类。这里我根据上面图片中的商品添加了他的基础分类数据如下:

    image

    2,商品品牌表

    此表的结构比较简单,就是品牌的基础信息。如图片中的荣耀手机,那么荣耀作为一个手机品牌,添加基础数据如下:

    image

    3,商品表(SPU)

    这个表里的每一条数据就是一个标准的产品单元,也就是所谓的SPU。比如荣耀20手机,这就是一个标准的商品,所以我们把它作为一条商品数据存储。此表必须要包含的字段:商品分类ID,商品品牌ID。注:此表里的商品详情信息字段,不要直接保存商品的图文化信息,可以把图文化信息转成html静态文件存储在文件服务器,然后将存储的路径url保存在数据库此字段里。这样会在很大程度上节约数据库开销。

    image

    4,规格表(SKU)

    规则表就是这里所谓的商品SKU实现,也就是说这里实现的是商品的存储单元。针对每一个多属性的商品,几个不同属性的组合将有自己独立的库存和价格信息。如上面的荣耀20手机,通过颜色和版本这两个属性,组成了以下6条SKU信息。

    image

    5,商品属性key和属性value

    这两个表作为商品分类的不同属性存储,在系统开始运营就需要做数据的初始化。日后运营人员如果要新增某一商品的SKU信息,就可直接根据数据库的这些基础数据选取,然后将属性再以json的形式存储到对应的规格表。

    属性key:

    image

    属性value:

    image

    好了,到此为止商品的SPU、SKU已经说完了。今天我讲的这个设计方案,可完全适用于商品类别差异化不大的项目或系统中。但针对差异化较大的情况,那就需要根据自己的业务情况去优化处理。

    注:此SpringBoot电商实战项目的源码已上传到github上并已开源,有需要的可以扫码关注公众号,然后发送“Springboot”获取github地址。

    扫码关注公众号,发送关键词获取相关资料:

    1. 发送“Springboot”领取电商项目实战源码;
    2. 发送“SpringCloud”领取cloud学习实战资料;

    展开全文
  • springboot项目**中国加油,武汉加油!**篇幅较长,配合目录观看案例准备1. springboot整合rabbitmq1.1 shop-temp新建springboot-rabbitmq-provider(module-springboot)1.2 导相关依赖1.3 编写yml1.4 编写...

    中国加油,武汉加油!

    篇幅较长,配合目录观看

    案例准备

    1. 本案例基于springboot篇】二十一. 基于springboot电商项目 五 FreeMarker生成商品页面
    2. gitee地址 https://gitee.com/springboot-dubbo/nz1904-springboot-shop-05

    1. springboot整合rabbitmq

    1.1 shop-temp新建springboot-rabbitmq-provider(module-springboot)

    1. Spring Boot DevTools
    2. Lombok
    3. Spring Web

    1.2 导相关依赖

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-amqp</artifactId>
    </dependency>
    

    1.3 编写yml

    spring:
      rabbitmq:
        host: 192.168.59.100
        virtual-host: /
    

    1.4 编写RabbitMQConfig

    package com.wpj.config;
    
    import org.springframework.amqp.core.*;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    @Configuration
    public class RabbitMQConfig {
    
        // 创建交换机
        @Bean
        public FanoutExchange fanoutExchange(){
            return new FanoutExchange("myExchange");
        }
        // 创建队列
        @Bean
        public Queue queue1(){
            return new Queue("queue1");
        }
        @Bean
        public Queue queue2(){
            return new Queue("queue2");
        }
        // 绑定
        @Bean
        public Binding queue1ToExchange(){
            return BindingBuilder.bind(queue1()).to(fanoutExchange());
        }
        @Bean
        public Binding queue2ToExchange(){
            return BindingBuilder.bind(queue2()).to(fanoutExchange());
        }
    }
    

    1.5 修改程序入口

    package com.wpj;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication(scanBasePackages = "com.wpj")
    public class SpringbootRabbitmqProviderApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(SpringbootRabbitmqProviderApplication.class, args);
        }
    
    }
    

    1.6 Test

    package com.wpj;
    
    import org.junit.jupiter.api.Test;
    import org.junit.runner.RunWith;
    import org.springframework.amqp.rabbit.core.RabbitTemplate;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.test.context.junit4.SpringRunner;
    
    @SpringBootTest
    @RunWith(SpringRunner.class)
    class SpringbootRabbitmqProviderApplicationTests {
    
        @Autowired
        private RabbitTemplate rabbitTemplate;
    
        @Test
        public void testRabbitMQ() {
            String str = "hello";
            rabbitTemplate.convertAndSend("myExchange","",str);
        }
    }
    

    1.7 shop-temp新建springboot-rabbitmq-consumer(module-springboot)

    1. Spring Web

    1.8 导包

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-amqp</artifactId>
    </dependency>
    

    1.9 编写yml

    spring:
      rabbitmq:
        host: 192.168.59.100
        virtual-host: /
    

    1.10 编写QueueConsumer

    package com.wpj.listener;
    
    import org.springframework.amqp.rabbit.annotation.RabbitListener;
    import org.springframework.context.annotation.Configuration;
    
    @Configuration
    public class QueueConsumer {
        @RabbitListener(queues = "queue1")
        public void queue1(String msg){
            System.out.println("queue1" + msg);
        }
        @RabbitListener(queues = "queue2")
        public void queue12(String msg){
            System.out.println("queue2" + msg);
        }
    }
    

    1.11 启动程序入口

    package com.wpj;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication(scanBasePackages = "com.wpj")
    public class SpringbootRabbitmqConsumerApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(SpringbootRabbitmqConsumerApplication.class, args);
        }
    }
    

    在这里插入图片描述

    2. 整合RabbitMQ到项目中

    2.1 good-service

    2.1.1 添加依赖

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-amqp</artifactId>
    </dependency>
    

    2.1.2 配置yml

    spring:
      datasource:
        url: jdbc:mysql://localhost:3306/nz1904-springboot-shop?characterEncoding=UTF8
        username: root
        password: 123456
        driver-class-name: com.mysql.jdbc.Driver
      rabbitmq:
        host: 192.168.59.100
        virtual-host: /
        
    mybatis-plus:
      type-aliases-package: com.wpj.entity
      mapper-locations: classpath:/mapper/*.xml
    
    dubbo:
      application:
        name: goods-service
      registry:
        address: zookeeper://192.168.59.100:2181
      protocol:
        port: -1
    

    2.1.3 编写RabbitMQConfig

    package com.wpj.config;
    
    import org.springframework.amqp.core.Binding;
    import org.springframework.amqp.core.BindingBuilder;
    import org.springframework.amqp.core.FanoutExchange;
    import org.springframework.amqp.core.Queue;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    @Configuration
    public class RabbitMQConfig {
    
        @Bean
        public FanoutExchange fanoutExchange(){
            return new FanoutExchange("goods-exchange");
        }
        @Bean
        public Queue solrQueue(){
            return new Queue("solrQueue");
        }
        @Bean
        public Queue itemQueue(){
            return new Queue("itemQueue");
        }
        @Bean
        public Binding solrQueueToExchange(){
            return BindingBuilder.bind(solrQueue()).to(fanoutExchange());
        }
        @Bean
        public Binding itemQueueToExchange(){
            return BindingBuilder.bind(itemQueue()).to(fanoutExchange());
        }
    }
    

    2.1.4 修改GoodServiceImpl

    package com.wpj.service.impl;
    
    import com.alibaba.dubbo.config.annotation.Service;
    import com.baomidou.mybatisplus.mapper.BaseMapper;
    import com.wpj.entity.Goods;
    import com.wpj.mapper.IGoodsMapper;
    import com.wpj.service.IGoodService;
    import org.springframework.amqp.rabbit.core.RabbitTemplate;
    import org.springframework.beans.factory.annotation.Autowired;
    
    @Service
    public class GoodsServiceImpl extends BaseServiceImpl<Goods> implements IGoodService {
    
        @Autowired
        private IGoodsMapper iGoodsMapper;
    
        @Override
        public BaseMapper<Goods> getMapper() {
            return iGoodsMapper;
        }
    
        @Autowired
        private RabbitTemplate rabbitTemplate;
    
        @Override
        public int insert(Goods goods) {
            int insert = super.insert(goods);
            // 给交换机添加消息
            rabbitTemplate.convertAndSend("goods-exchange","", goods);
            return insert;
        }
    }
    

    2.2 search-service

    2.2.1 添加依赖

    <dependency>
       <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-amqp</artifactId>
    </dependency>
    

    2.2.2 配置yml

    dubbo:
      registry:
        address: zookeeper://192.168.59.100:2181
      application:
        name: search-service
      protocol:
        port: -1
    
    spring:
      data:
        solr:
          host: http://192.168.59.100:8983/solr/mycollection
      rabbitmq:
        host: 192.168.59.100
        virtual-host: /
    

    2.2.3 编写RabbitMQConfig

    package com.wpj.config;
    
    import org.springframework.amqp.core.Binding;
    import org.springframework.amqp.core.BindingBuilder;
    import org.springframework.amqp.core.FanoutExchange;
    import org.springframework.amqp.core.Queue;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    @Configuration
    public class RabbitMQConfig {
        @Bean
        public FanoutExchange fanoutExchange(){
            return new FanoutExchange("goods-exchange");
        }
        @Bean
        public Queue solrQueue(){
            return new Queue("solrQueue");
        }
        @Bean
        public Binding solrQueueToExchange(){
            return BindingBuilder.bind(solrQueue()).to(fanoutExchange());
        }
    }
    

    2.2.4 编写SolrQueueListener

    package com.wpj.listener;
    
    import com.wpj.entity.Goods;
    import org.apache.solr.client.solrj.SolrClient;
    import org.apache.solr.client.solrj.SolrServerException;
    import org.apache.solr.common.SolrInputDocument;
    import org.springframework.amqp.rabbit.annotation.RabbitListener;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Configuration;
    
    import java.io.IOException;
    
    @Configuration
    public class SolrQueueListener {
    
        @Autowired
        private SolrClient solrClient;
    
        @RabbitListener(queues = "solrQueue")
        public void solrQueue(Goods goods){
            SolrInputDocument document = new SolrInputDocument();
            document.addField("id",goods.getId());
            document.setField("gname", goods.getGname());
            document.setField("gdesc", goods.getGdesc());
            document.setField("gpic", goods.getGpic());
            document.setField("gprice", goods.getGprice().toString());
            try {
                solrClient.add(document);
                solrClient.commit();
            } catch (SolrServerException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    

    2.3 shop-item

    2.3.1 添加依赖

    <dependency>
       <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-amqp</artifactId>
    </dependency>
    

    2.3.2 配置yml

    server:
      port: 8083
      
    dubbo:
      application:
        name: item-web
      registry:
        address: zookeeper://192.168.59.100:2181
        check: false
      consumer:
        check: false
        retries: 3
    spring:
      rabbitmq:
        host: 192.168.59.100
        virtual-host: /
    

    2.3.3 编写RabbitMQConfig

    package com.wpj.config;
    
    import org.springframework.amqp.core.Binding;
    import org.springframework.amqp.core.BindingBuilder;
    import org.springframework.amqp.core.FanoutExchange;
    import org.springframework.amqp.core.Queue;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    @Configuration
    public class RabbitMQConfig {
    
        @Bean
        public FanoutExchange fanoutExchange(){
            return new FanoutExchange("goods-exchange");
        }
        @Bean
        public Queue itemQueue(){
            return new Queue("itemQueue");
        }
        @Bean
        public Binding itemQueueToExchange(){
            return BindingBuilder.bind(itemQueue()).to(fanoutExchange());
        }
    }
    

    2.3.4 编写ItemQueueListener

    package com.wpj.listener;
    
    import com.alibaba.dubbo.config.annotation.Reference;
    import com.wpj.controller.ItemController;
    import com.wpj.entity.Goods;
    import com.wpj.service.IGoodService;
    import freemarker.template.Template;
    import freemarker.template.TemplateException;
    import org.springframework.amqp.rabbit.annotation.RabbitListener;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Configuration;
    
    import java.io.File;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.Writer;
    import java.util.HashMap;
    import java.util.Map;
    
    @Configuration
    public class ItemQueueListener {
    
        @Reference
        private IGoodService goodService;
    
        @Autowired
        private freemarker.template.Configuration configuration;
    
        @RabbitListener(queues = "itemQueue")
        public void itemQueue(Goods goods){
            // 图片的集合
            String[] split = goods.getGpic().split("\\|");
            // 把数据放到Map中
            Map<String,Object> map = new HashMap<>();
            map.put("goods",goods);
            map.put("picList",split);
            map.put("contextPath","/");
            // 得到模板
            Template template = null;
            Writer writer =null;
            try {
                template = configuration.getTemplate("goodsItem.ftl");
                // 设置静态页面输出的目录
                String staticPath = ItemController.class.getClassLoader().getResource("static").getPath();
                File file = new File(staticPath+File.separator+"goods");
                // 不存在创建目录
                if(!file.exists()){
                    file.mkdir();
                }
                // 生成页面
                writer = new FileWriter(file.getAbsolutePath()+File.separator+goods.getId()+".html");
                template.process(map,writer);
            } catch (IOException e) {
                e.printStackTrace();
            } catch (TemplateException e) {
                e.printStackTrace();
            }finally {
                try {
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    

    3. 校验

    1. localhost:8080 进去添加商品
    2. localhost:8081 进入查找商品
    3. 点进去新增的商品进入生成的页面
    展开全文
  • springboot项目**中国加油,武汉加油!**篇幅较长,配合目录观看案例准备1. 地址服务环境搭建1.1 shop-entity编写Address类1.2 shop-mapper编写mapper接口1.3 shop-service-api编写Service接口1.4 shop-service-iml...

    中国加油,武汉加油!

    篇幅较长,配合目录观看

    案例准备

    1. 本案例基于springboot篇】二十一. 基于springboot电商项目 十 购物车模块和自定义注解校验登录状况

    1. 地址服务环境搭建

    1.1 shop-entity编写Address类

    package com.wpj.entity;
    
    import com.baomidou.mybatisplus.annotations.TableField;
    import com.baomidou.mybatisplus.annotations.TableId;
    import com.baomidou.mybatisplus.annotations.TableName;
    import com.baomidou.mybatisplus.enums.IdType;
    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.NoArgsConstructor;
    
    import java.io.Serializable;
    
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    @TableName("t_address")
    public class Address implements Serializable{
    
        @TableId(type = IdType.AUTO)
        private Integer id;
        private String person;
        private String phone;
        private String address;
        @TableField(value = "is_def")
        private Integer isDef;
        @TableField(value = "user_id")
        private Integer uid;
    }
    

    1.2 shop-mapper编写mapper接口

    package com.wpj.mapper;
    
    import com.baomidou.mybatisplus.mapper.BaseMapper;
    import com.wpj.entity.Address;
    
    public interface IAddressMapper extends BaseMapper<Address> {
    }
    

    1.3 shop-service-api编写Service接口

    package com.wpj.service;
    
    import com.wpj.entity.Address;
    
    public interface IAddressService extends IBaseService<Address> {
    }
    

    1.4 shop-service-iml新建address-service(module-springboot)

    1.4.1 address-service导包

    <dependency>
        <groupId>com.wpj</groupId>
        <artifactId>shop-service-api</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>com.wpj</groupId>
        <artifactId>shop-mapper</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>com.wpj</groupId>
        <artifactId>shop-common</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>com.alibaba.boot</groupId>
        <artifactId>dubbo-spring-boot-starter</artifactId>
        <version>0.2.0</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.8</version>
    </dependency>
    

    1.4.2 编写ServiceImpl

    package com.wpj.service.impl;
    
    import com.alibaba.dubbo.config.annotation.Service;
    import com.baomidou.mybatisplus.mapper.BaseMapper;
    import com.wpj.entity.Address;
    import com.wpj.mapper.IAddressMapper;
    import com.wpj.service.IAddressService;
    import org.springframework.beans.factory.annotation.Autowired;
    
    @Service
    public class AddressServiceImpl extends BaseServiceImpl<Address> implements IAddressService {
    
        @Autowired
        private IAddressMapper addressMapper;
    
        @Override
        public BaseMapper<Address> getMapper() {
            return addressMapper;
        }
    }
    

    1.4.3 配置yml

    spring:
      datasource:
        url: jdbc:mysql://localhost:3306/nz1904-springboot-shop
        username: root
        password: 123456
        driver-class-name: com.mysql.jdbc.Driver
    dubbo:
      application:
        name: address-service
      registry:
        address: zookeeper:192.168.59.100:2181
      protocol:
        port: -1
    mybatis-plus:
      type-aliases-package: com.wpj.entity
      mapper-locations: classpath:/mapper/*.xml
    

    1.4.4 修改程序入口

    package com.wpj;
    
    import com.alibaba.dubbo.config.spring.context.annotation.DubboComponentScan;
    import org.mybatis.spring.annotation.MapperScan;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    @DubboComponentScan(basePackages = "com.wpj.service")
    @MapperScan(basePackages = "com.wpj.mapper")
    public class AddressServiceApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(AddressServiceApplication.class, args);
        }
    }
    

    1.5 shop-web新建shop-order(module-springboot)

    1.5.1 shop-order导包

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>com.wpj</groupId>
        <artifactId>shop-service-api</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>com.wpj</groupId>
        <artifactId>shop-common</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>com.alibaba.boot</groupId>
        <artifactId>dubbo-spring-boot-starter</artifactId>
        <version>0.2.0</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>
    

    1.5.2 配置yml

    server:
      port: 8086
    spring:
      thymeleaf:
        cache: false
        prefix: classpath:/templates/
      redis:
        host: 192.168.59.100
        password: admin
    dubbo:
      application:
        name: shop-order
      registry:
        address: zookeeper://192.168.59.100:2181
      consumer:
        check:  false
        retries: 3
    

    1.5.3 修改程序入口

    package com.wpj;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
    
    @SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
    public class ShopOrderApplication {
        public static void main(String[] args) {
            SpringApplication.run(ShopOrderApplication.class, args);
        }
    }
    

    1.5.4 启动AddressService并测试

    package com.wpj;
    
    import com.alibaba.dubbo.config.annotation.Reference;
    import com.wpj.entity.Address;
    import com.wpj.service.IAddressService;
    import org.junit.jupiter.api.Test;
    import org.junit.runner.RunWith;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.test.context.junit4.SpringRunner;
    
    import java.util.List;
    
    @SpringBootTest
    @RunWith(SpringRunner.class)
    class ShopOrderApplicationTests {
    
        @Reference
        private IAddressService addressService;
    
        @Test
        public void contextLoads() {
            List<Address> list = addressService.getList();
            for (Address address : list) {
                System.out.println(address);
            }
        }
    }
    

    在这里插入图片描述

    2. 地址服务模块-确认订单页面

    2.1 service-api定义方法

    package com.wpj.service;
    
    import com.wpj.entity.Address;
    
    import java.util.List;
    
    public interface IAddressService extends IBaseService<Address> {
    
        List<Address> getAddressListByUid(Integer id);
    }
    

    2.2 address-service重写方法

    @Override
    public List<Address> getAddressListByUid(Integer id) {
        EntityWrapper wrapper = new EntityWrapper();
        wrapper.eq("user_id", id);
        return addressMapper.selectList(wrapper);
    }
    

    2.3 shop-order编写Controller

    package com.wpj.controller;
    
    import com.alibaba.dubbo.config.annotation.Reference;
    import com.wpj.common.aop.IsLogin;
    import com.wpj.entity.Address;
    import com.wpj.entity.Cart;
    import com.wpj.entity.User;
    import com.wpj.service.IAddressService;
    import com.wpj.service.ICartService;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.ModelMap;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    import java.math.BigDecimal;
    import java.util.List;
    
    @Controller
    @RequestMapping(value = "/order")
    public class OrderController {
    
        @Reference
        private IAddressService addressService;
        @Reference
        private ICartService cartService;
    
        @RequestMapping(value = "/toAffirmOrder")
        @IsLogin(mustUser = true)
        public String toAffirmOrder(User user, ModelMap map){
            // 根据用户id查询地址
            List<Address> addressList = addressService.getAddressListByUid(user.getId());
            // 进来的话就已经登录了所有无需校验token
            List<Cart> cartList = cartService.getUserCartList(user, null);
    
            BigDecimal totalPrice = new BigDecimal(0);
            for(Cart cart:cartList){
                totalPrice= totalPrice.add(cart.getSubTotal());
            }
            // 数据存到ModelMap中
            map.put("addressList",addressList);
            map.put("cartList",cartList);
            map.put("totalPrice",totalPrice);
            return "affirmOrder";
        }
    }
    

    2.4 导入静态资源和编写页面

    2.5 开启包扫描

    package com.wpj;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
    
    @SpringBootApplication(scanBasePackages = "com.wpj",exclude = DataSourceAutoConfiguration.class)
    public class ShopOrderApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(ShopOrderApplication.class, args);
        }
    }
    

    2.6 启动程序入口测试

    在这里插入图片描述

    3. 地址服务模块-添加新地址

    3.1 address-service重写insert方法

    @Override
    public int insert(Address address) {
        return addressMapper.addAdderss(address);
    }
    

    3.2 shop-mapper的Mapper接口定义方法

    package com.wpj.mapper;
    
    import com.baomidou.mybatisplus.mapper.BaseMapper;
    import com.wpj.entity.Address;
    
    public interface IAddressMapper extends BaseMapper<Address> {
        int addAddress(Address address);
    }
    

    3.3 定义mapper.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE mapper
      PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
      "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <mapper namespace="com.wpj.mapper.IAddressMapper">
    	<insert id="addAddress" statementType="CALLABLE">
    			{
    				call add_address(#{person},#{phone},#{address},#{isDef},#{uid})
    			}
    	</insert>
    </mapper>
    

    3.4 数据库定义存储过程

    在这里插入图片描述

    3.5 shop-order编写Controller

    package com.wpj.controller;
    
    import com.alibaba.dubbo.config.annotation.Reference;
    import com.wpj.common.aop.IsLogin;
    import com.wpj.entity.Address;
    import com.wpj.entity.User;
    import com.wpj.service.IAddressService;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    @Controller
    @RequestMapping(value = "/address")
    public class AddressController {
    
        @Reference
        private IAddressService addressService;
    
        @RequestMapping(value = "/addAddress")
        @IsLogin(mustUser = true)
        @ResponseBody // 储存过程
        public String addAddress(Address address, User user){
            address.setUid(user.getId());
            addressService.insert(address);
            return "ok";
        }
    }
    
    展开全文
  • springboot项目**中国加油,武汉加油!**篇幅较长,配合目录观看案例准备1. 订单添加1.1 修改shop-order的affirmOrder.html1.2 修改order表结构1.3 shop-entity编写Order类1.4 shop-common编写工具类1.5 shop-mapper...
  • springboot项目**中国加油,武汉加油!**篇幅较长,配合目录观看案例准备1. 支付1.1 shop-web新建shop-pay(module-springboot)1.2 导包1.3 沙箱应用设置公钥,公钥用keytool生成1.4 编写yml1.5 程序入口开启包扫描...
  • springboot项目**中国加油,武汉加油!**篇幅较长,配合目录观看案例准备1. 新建站点工程nz1904-shop(maven)1.1 删除src目录1.2 新建shop-entity(module-maven)1.3 新建shop-mapper(module-maven)1.4 新建shop...
  • springboot项目**中国加油,武汉加油!**篇幅较长,配合目录观看案例准备1. 用户模块-添加 user-web1.1 编写WebMVCConfig1.2 编写addUser.html1.3 编写form-utils.js1.4 shop-common导入依赖编写返回数据的依赖1.5 ...
  • springboot项目**中国加油,武汉加油!**篇幅较长,配合目录观看案例准备1. 分库整合项目中1.1 脚本环境准备1.2 order-service-api修改Service接口1.3 order-service修改ServiceImpl实现类1.4 shop-order修改...
  • springboot项目**中国加油,武汉加油!**篇幅较长,配合目录观看案例准备1. java发送邮件1.1 shop-temp新建java-mail(module-maven)1.2 导相关依赖1.3 Test2 注册功能2.1 shop-web新建shop-sso(module-springboot...
  • springboot项目**中国加油,武汉加油!**篇幅较长,配合目录观看案例准备1. FreeMarker介绍2. Freemarker快速使用2.1 shop-temp新建freemaker-demo(module-springboot)2.2 导相关依赖2.3 编写hello.ftl2.4 Test2.5...
  • springboot项目**中国加油,武汉加油!**篇幅较长,配合目录观看案例准备1. 测试FastDFS user-web(shop-back)1.1 添加FastDFS依赖1.2 程序入口加入注解1.3 配置yml1.4 TestFastDFS1.5 访问图片路径2 整合Springboot...

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 1,599
精华内容 639
关键字:

springboot电商项目

spring 订阅