第4天-搭建项目(快速搭建电商平台后台管理系统及逆向生成微服务基本功能)

1.快速搭建电商平台后台管理系统

目前在Gitee开源软件中,人人开源 和 若依 关注度比较高,优品电商平台后台系统采用人人开源的框架
来快速构建,完成基本的CRUD,开发更加关注复杂业务、高并发、高可用以及分布式架构层面。

1.1.人人开源快速构建

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

人人开源说明:

  • renren-fast:用于快速构建电商后台管理系统(后端)
  • renren-fast-vue:用于快速构建电商后台管理系统(前端)
  • renren-generator:用于快速构建微服务基础CRUD代码

1.2.克隆人人开源框架

git clone https://gitee.com/renrenio/renren-fast.git
git clone https://gitee.com/renrenio/renren-fast-vue.git

1.3.搭建电商后台系统

1.3.1.Java后台

1.将克隆好的renren-fast工程改名gmall-admin拷贝到gmall工程中,并添加到Modules

  • 改名: gmall-admin
  • 注意:将 .git 文件夹删除
    在这里插入图片描述
  1. 将 gmall-admin 聚合到 gmall 工程的 modules 中
<modules>
	<module>gmall-admin</module>
</modules>
  1. 创建数据库 gmall_admin ,执行 gmall_admin 工程里的 MySQL 数据库脚本 mysql.sql

  2. 修改 gmall_admin 工程中 application-dev.yml 配置文件中 MySQL 的连接配置
spring:
    datasource:
        type: com.alibaba.druid.pool.DruidDataSource
        druid:
            driver-class-name: com.mysql.cj.jdbc.Driver
            url: jdbc:mysql://192.168.56.10:3306/gmall_admin?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
            username: root
            password: root
  1. 修改 gmall_admin 工程 application.yml 中 context-path
servlet:
	context-path: /gmall_admin
  1. 启动 gmall_admin 后台服务

1.3.2.前端工程

前端工程搭建文档:https://gitee.com/renrenio/renren-fast-vue/wikis/Home

  1. 安装 Node.js ,并配置 npm 使用淘宝镜像
npm config set registry http://registry.npm.taobao.org
npm install -g cnpm --registry=https://registry.npm.taobao.org
  1. VSCode中打开 renren-fast-vue 前端工程(更名: gmall-admin-vue )

  2. 修改前端访问的入口文件 index.js

    在这里插入图片描述

  3. 进入VSCode终端,安装并运行前端项目

#安装项目所需的依赖到 node_modules 文件中
npm install
#运行项目
npm dev run

#Error: nodeб sass@4.9.0 postinstall: node scripts/build.js
#实际是因为sass安装时获取源的问题,使用cnpm进行安装
npm install -g cnpm --registry=https://registry.npm.taobao.org
cnpm install node-sass --save

启动成功,如下图所示:

在这里插入图片描述

  1. 启动完成后会自动打开浏览器访问 http://localhost:8001

在这里插入图片描述

  1. 输入admin / admin 成功进入后台,说明系统搭建成功!

在这里插入图片描述



2.逆向生成微服务


2.1.代码生成器

克隆 renren-generator 改名 gmall-generator 拷贝到 gmall 工程中,并聚合到 modules中

git clone https://gitee.com/renrenio/renren-generator.git

<modules>
	<module>gmall-generator</module>
</modules>

2.2.快速构建微服务

商品服务 gmall-product 基础代码生成步骤:

修改代码生成器 gmall-generator 配置:

1)application.yml

spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    #MySQL配置
    driverClassName: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://192.168.56.10:3306/gmall_pms?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai
    username: root
    password: root

2)generator.properties

# 域名
mainPath=com.atguigu
# 项目包名
package=com.atguigu.gmall
# 模块名
moduleName=product
# 开发人员
author=zhangwen
# 开发者Email
email=1466787185@qq.com
# 数据库表前缀(类名不会包含表前缀)
tablePrefix=pms_

3)修改Controller 生成类,去除 shiro 相关注解,后续项目中使用 Spring Security

  1. 进入到 renren-generator/src/main/resources/template/Controller.java.vm
  2. 注释 import org.apache.shiro.authz.annotation.RequiresPermissions;
  3. 注释 Controller.java.vm 模板中所有Method方法 @RequiresPermissions

4)运行项目,进入到 代码生成器 控制台,全选 -> 生成代码

在这里插入图片描述

5)解压生成的代码包,将 main 文件夹全部拷贝到 gmall-product/src/ 目录下即可

6)按照 gmall-product 基础代码生成步骤,依次生成其它服务的代码

  • gmall-order
  • gmall-member
  • gmall-coupon
  • gmall-ware

3.创建公共服务模块

公共服务模块: gmall-common

  • 每个微服务公共的依赖,Bean,工具类等放入到这里
  • 每个微服务都引入 gmall-common

3.1.gmall-common

3.1.1.创建Maven工程

在这里插入图片描述

将 gmall-common 公共服务模块聚合到 modules 中

<modules>
	<module>gmall-common</module>
</modules>

3.1.2.引入公共依赖

引入公共依赖,后续根据需要再依次引入所需的公共依赖

<!-- 微服务公共依赖 -->
<dependencies>
		<!-- mysql -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.21</version>
        </dependency>
        <!-- mybatis-plus-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.3.2</version>
        </dependency>
 		<!-- lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.12</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpcore</artifactId>
            <version>4.4.13</version>
        </dependency>
        <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
            <version>2.6</version>
        </dependency>
</dependencies>

3.2.工具类

3.2.1.com.atguigu.common.utils

  • Constant
  • PageUtils
  • Query
  • R

3.2.2.com.atguigu.common.xss

  • SQLFilter

3.2.3.com.atguigu.common.exception

  • RRException


4.整合MyBatis-Plus


4.1.MyBatis-Plus介绍

MyBatis-Plus 官网:https://baomidou.com/

在这里插入图片描述


4.2.导入依赖

在 gmall-common 公共模块中导入依赖

<dependency>
	<groupId>com.baomidou</groupId>
	<artifactId>mybatis-plus-boot-starter</artifactId>
	<version>3.3.2</version>
</dependency>

4.3.数据源配置

官网配置

4.3.1.引入数据库驱动

MySQL Connector/J Versions

在这里插入图片描述

在 gmall-common 公共模块中导入依赖

<dependency>
	<groupId>mysql</groupId>
	<artifactId>mysql-connector-java</artifactId>
	<version>8.0.21</version>
</dependency>

4.3.2.配置数据源

在商品服务 gmall-product 的 application.yml 配置数据源相关信息

spring:
  datasource:
    url: jdbc:mysql://192.168.56.10:3306/gmall_pms?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: root

4.4.配置MyBatis-Plus


4.4.1.使用@MapperScan

在服务启动类上使用 @MapperScan

@MapperScan(“com.atguigu.gmall.product.dao”)


4.4.2.SQL映射文件

告诉MyBatis-Plus,SQL映射文件位置

mybatis-plus:
  mapper-locations: classpath:/mapper/product/*.xml
  global-config:
    db-config:
      id-type: auto  #id自增,后续分库分表后再使用其它方式

4.4.3.单元测试

@SpringBootTest
class GmallProductApplicationTests {
	@Autowired
	private BrandService brandService;
	/**
	 * 测试插入
	 */
	@Test
	void testSave() {
		BrandEntity brandEntity = new BrandEntity();
		brandEntity.setName("华为");
		brandService.save(brandEntity);
	}
	
	/**
	 * 测试更新
	 */
	@Test
	void testUpdate() {
		BrandEntity brandEntity = new BrandEntity();
		brandEntity.setBrandId(1L);
		brandEntity.setDescript("华为");
		brandService.updateById(brandEntity);
	}
	
	/**
	 * 测试查询
	 */
	@Test
	void testQuery() {
		List<BrandEntity> list = brandService.list(
			new QueryWrapper<BrandEntity>()
				.eq("brand_id", 1L));
		list.forEach(item -> {
			System.out.println(item);
		});
	}
}

测试结果:

在这里插入图片描述
对其它服务模块依次进行测试:

  • gmall-coupon
  • gmall-member
  • gmall-order
  • gmall-ware

在每个微服务模块 pom 文件中导入公共模块 gmall-common

 		<dependency>
            <groupId>com.atguigu.gmall</groupId>
            <artifactId>gmall-common</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

修改每个模块的 application.yml 配置文件,主要是 port 和 数据库名称

server:
  port: 7000
spring:
  datasource:
    url: jdbc:mysql://192.168.56.10:3306/gmall_oms?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: root
mybatis-plus:
  mapper-locations: classpath:/mapper/order/*.xml
  global-config:
    db-config:
      id-type: auto

微服务端口规划

  • 优惠服务(gmall-coupon):5000
  • 会员服务(gmall-member):6000
  • 订单服务(gmall-order):7000
  • 商品服务(gmall-product):8000
  • 仓储服务(gmall-ware):9000
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值