人人开源搭建后台管理系统
按照微服务功能拆分图,我们要搭建一个后台管理系统(admin-vue)
gitee搜索人人开源
进入人人开源的主页
这里使用前后端分离、vue组件开发方式的后台管理系统作为脚手架。
克隆到本地
git bash here 控制台clone命令
将renren-fast和renren-fast-vue都clone下来,在桌面上会出现对应的两个文件夹。删除其中的.git文件,将renren-fast放到gulimall的项目目录中。
增加gulimall对renren-fast的模块管理
创建renren-fast的环境
- 新建数据库:gulimall_admin,编码utf8mb4
- 导入sql语句
3.修改yaml文件中对数据库的配置
application.yaml:
可知端口8080,使用dev.yaml的配置,路径为renren-fast
application-dev.yml:
需要修改ip、数据库名称、账号、密码
运行renren后端
运行后输入localhost:8080/renren-fast得到json返回:
{"msg":"invalid token","code":401}
需要前端项目来请求。
vscode运行renren-fast-vue
将renren-fast-vue文件夹拖到vscode图标上打开
安装组件:
1.node.js:
nodejs是个基于谷歌V8引擎的js运行环境。
打开nodejs官网下载安装
cmd窗口输入node -v可查看版本
npm是随同nodejs安装的js包管理工具,类似maven
配置npm的淘宝镜像路径
在cmd命令行窗口中输入:
npm config set registry http://registry.npm.taobao.org/
2.npm install
第一次运行前端项目在控制台安装js组件,类似maven安装
注意,请以管理员身份运行vscode,否则npm install会爆找不到命令之类的错
js组件的版本由package.json文件确定,类似pom.xml:
3. 运行
js组件安装完后使用如下命令运行:
npm run dev
启动完成后提示在8001端口运行:
登陆页面如下:
账号密码默认为
admin
admin
进入如下界面前后端联调就成功了
逆向工程搭建及其使用
下载代码生成器
桌面右键git bash here
git clone https://gitee.com/renrenio/renren-generator.git
删掉其中的.git文件,文件夹移到gulimall目录下
更改代码配置
1.在pom.xml中将代码生成器加到gulimall模块下:
2.修改yaml、properties文件
这里以gulimall_pms这个数据库为例
yaml:
properties:
表前缀:pms_
3.使用代码生成器
运行代码生成器
打开网页127.0.0.1:80
选中所有表,生成代码
解压生成的代码包,可以发现目录结构和idea中项目的目录结构一样,这是因为之前properties中设置过。
直接复制代码生成的整个main文件夹,在idea中打开product模块的main目录。
4.处理在idea的gulimall_pms模块中增加的生成代码,看看缺什么jar包
service中:
renren-fast中有utils需要的工具类:
entity中:
dao:
controller:
5.处理缺jar包导致的爆红
创建maven模块,提供公共类
可以在pom文件中增加描述
<description>每一个微服务公共的依赖,bean,工具类等</description>
product,order,member.coupon模块的pom.xml文件中添加对公共模块的依赖:
<dependency>
<groupId>com.atguigu.gulimall</groupId>
<artifactId>gulimall-common</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
在公共模块的pom.xml文件中补全需要的依赖:
mybatisplus最新依赖地址
<properties>
<commons.lang.version>2.6</commons.lang.version>
</properties>
<dependencies>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>3.4.3</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.18</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpcore -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4.14</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>${commons.lang.version}</version>
</dependency>
</dependencies>
然后在common中从renren-fast中复制过来补全需要的类:
类里面的爆红调用使用alt+enter自动import需要的类,再删掉原有的依赖。
这时只有controller中的shiro框架相应的依赖在报错,以后使用spring security,所以就重新改变renren-generator模块中的controller模板,重新生成将shiro注解注释掉的代码。
然后重启renren-generator,重新生成product模块的代码,重新只导入controller包下的代码。
整合mybatisplus:
1.1 springboot关于mybatisplus的场景启动器,直接放到common模块里,因为场景启动器里包括了mybatisplus,所以注释掉前面mybatis-plus的依赖。
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.2.0</version>
</dependency>
1.2 配置mybatisplus:数据源、使用配置
参照官网的配置
mysql驱动都要用,直接配到common的pom.xml里面:
驱动版本要对应,由于是mysql是5.7.34
所以从maven仓库里找找对应的5734版本的依赖。发现没有。
mysql官方是这么说的:官方版本说明
推荐用8的所以用8的mysql驱动就可以了,这里用的8.0.17
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.17</version>
</dependency>
gulimall-product配置:
yaml:
# DataSource Config
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://192.168.220.129:3306/gulimall_pms?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai
username: root
password: root
mybatis-plus:
mapper-locations: classpath*:/mapper/**/*.xml #不配也可以,这是默认值
global-config:
db-config:
id-type: auto #自增id
启动类增加@MapperScan(“xxxxx”):
package com.atguigu.gulimall.product;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@MapperScan("com.atguigu.gulimall.product.dao")
@SpringBootApplication
public class GulimallProductApplication {
public static void main(String[] args) {
SpringApplication.run(GulimallProductApplication.class, args);
}
}
test测试类:
package com.atguigu.gulimall.product;
import com.atguigu.gulimall.product.entity.BrandEntity;
import com.atguigu.gulimall.product.service.BrandService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class GulimallProductApplicationTests {
@Autowired
BrandService brandService;
@Test
public void contextLoads() {
BrandEntity brandEntity = new BrandEntity();
brandEntity.setName("testname");
brandService.save(brandEntity);
System.out.println("保存成功");
}
}
运行测试类,发现控制台打印了“保存成功”,数据库增加了id=1的词条,整合成功。
同样,按照上述操作,将coupon,member,order,ware模块使用代码生成器都快速创建好controller,service,dao,entity层的文件,并在test类中随便找个实体类测试通过。
或者启动模块直接测试controller:
例如:
输入localhost:8080/coupon/coupon/list,返回json串,内含success的msg基本上就通了。
{"msg":"success","code":0,"page":{"totalCount":0,"pageSize":10,"totalPage":0,"currPage":1,"list":[{"id":1,"couponType":6,"couponImg":null,"couponName":null,"num":null,"amount":null,"perLimit":null,"minPoint":null,"startTime":null,"endTime":null,"useType":null,"note":null,"publishCount":null,"useCount":null,"receiveCount":null,"enableStartTime":null,"enableEndTime":null,"code":null,"memberLevel":null,"publish":null}]}}
测试类报错就先改成下面junit4格式的,报错的test方式是更高级的junit5版本的测试
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class GulimallMemberApplicationTests {
@Test
public void contextLoads() {
}
}
各yaml中端口设置:
server:
port: 7000
coupon 7000
member 8000
order 9000
product 10000
ware 11000