接上一節的內容,上一節把環境和軟件都已經搭建完成,並且運行成功。
一、逆向工程使用
人人開源項目中的代碼生成器:https://gitee.com/renrenio/renren-generator
克隆下來放入工程中,加入moudle
修改數據庫配置
修改一下逆向工程的模板,把一些沒有用的註釋掉。
首先生成xuanyuan_pms數據庫的相關代碼 。
啓動逆向工程
全部生成完成後是一個壓縮包,把裏邊的內容解壓出來複製到product項目中
發現了好多錯誤,所以 創建xuanyuan_common工具類,每個微服務依賴的類都放進去
pom文件的配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>ware</artifactId>
<groupId>com.xuanyuan</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>xuanyuan-common</artifactId>
<description>每一個微服務公共的依賴</description>
<dependencies>
<!-- mybatis-plus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.8</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpcore -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4.12</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<!-- 導入mysql驅動 -->
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.17</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<!-- 服務註冊/發現-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!-- 配置中心來做配置管理-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>2.0.1.Final</version>
</dependency>
</dependencies>
</project>
然後把一些常用的類粘貼進去。
調整項目沒有錯誤之後,在product裏邊進行單元測試
package com.xuanyuan.product;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xuanyuan.product.entity.BrandEntity;
import com.xuanyuan.product.service.BrandService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
@SpringBootTest
class ProductApplicationTests {
@Autowired
BrandService brandService;
@Test
void contextLoads() {
// BrandEntity brandEntity = new BrandEntity();
//
// brandEntity.setDescript("");
// brandEntity.setName("小米");
// brandService.save(brandEntity);
//
// System.out.println("baocunchengg");
// }
List<BrandEntity> list = brandService.list(new QueryWrapper<BrandEntity>().eq("brand_id", 1L));
list.forEach((item) -> {
System.out.println(item);
});
}
}
測試成功!
二、爲其他的各個服務進行逆向生成
之前用的商品服務product進行測試
1、coupon優惠服務
修改代碼生成器的配置文件:
mainPath=com.xuanyuan
#\u5305\u540D
package=com.xuanyuan
moduleName=coupon
#\u4F5C\u8005
author=jiaxuan
#Email
[email protected]
#\u8868\u524D\u7F00(\u7C7B\u540D\u4E0D\u4F1A\u5305\u542B\u8868\u524D\u7F00)
tablePrefix=sms_
driverClassName: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://123.57.255.146:3306/xuanyuan_sms?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai
username: root
password: root
運行之後得到壓縮包,複製到coupon優惠項目中,增加common依賴,導包修改錯誤。
把product的配置文件複製過來,修改數據庫爲sms
spring:
datasource:
username: root
password: root
url: jdbc:mysql://123.57.255.146:3306/xuanyuan_sms
driver-class-name: com.mysql.jdbc.Driver
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
jackson:
date-format: yyyy-MM-dd HH:mm:ss
mybatis-plus:
mapper-locations: classpath:/mapper/**/*.xml
global-config:
db-config:
# 自增主鍵
id-type: auto
logic-delete-value: 1
logic-not-delete-value: 0
server:
port: 8080
logging:
level:
com.atguigu.gulimall: debug
運行後得到返回結果:
同理:其餘項目按此步驟依次完成。調整接口
coupon優惠系統 sms 7000
member會員系統 ums 8000
order訂單系統 oms 9000
product商品系統 pms 10000
ware庫存系統wms 11000
總體步驟就是 改代碼生成器的配置文件——》運行下載代碼——》複製粘貼——》修改配置文件和導入common依賴——》測試