谷粒商城-全栈-08 快速开发-逆向工程搭建及使用

一、代码生成器

renren-generator 是人人开源项目的代码生成器,可在线生成entity、xml、dao、service、html、js、sql代码,减少70%以上的开发任务。

二、本地部署

1、通过git下载源码

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

2、和商城项目整合

将下载的 renren-generator 复制到商城项目下,然后删掉该目录下原有的 .git 文件夹。

➜  gulimall git:(master) ✗ cd renren-generator 
➜  renren-generator git:(master) rm -rf .git

在父目录的 pom.xml 添加代码生成器renren-generator模块:

<modules>
    <module>gulimall-coupon</module>
    <module>gulimall-member</module>
    <module>gulimall-order</module>
    <module>gulimall-product</module>
    <module>gulimall-ware</module>
    <module>renren-fast</module>
    <module>renren-generator</module>
  </modules>

三、使用

1、修改连接的数据库

修改 application.yml,更新MySQL账号和密码、数据库名称,我们这里先连接商品库 gulimall_pms

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

2、更改generator.properties属性

#代码生成器配置信息

mainPath=com.atguigu
#包名
package=com.atguigu.gulimall
moduleName=product
#作者
author=kaiyi
#Email
email=corwienwong@gmail.com
#表前缀(类名不会包含表前缀)
tablePrefix=pms_

#类型转换,配置信息
tinyint=Integer
smallint=Integer
mediumint=Integer
int=Integer
integer=Integer
bigint=Long
float=Float
double=Double
decimal=BigDecimal
bit=Boolean

char=String
varchar=String
tinytext=String
text=String
mediumtext=String
longtext=String

date=Date
datetime=Date
timestamp=Date

NUMBER=Integer
INT=Integer
INTEGER=Integer
BINARY_INTEGER=Integer
LONG=String
FLOAT=Float
BINARY_FLOAT=Float
DOUBLE=Double
BINARY_DOUBLE=Double
DECIMAL=BigDecimal
CHAR=String
VARCHAR=String
VARCHAR2=String
NVARCHAR=String
NVARCHAR2=String
CLOB=String
BLOB=String
DATE=Date
DATETIME=Date
TIMESTAMP=Date
TIMESTAMP(6)=Date

int8=Long
int4=Integer
int2=Integer
numeric=BigDecimal

nvarchar=String

3、启动

前面已经配置好相关的属性了,则IDEA运行RenrenApplication.java,则可启动项目:

在使用mac启动项目的时候,发现原本在Windows下正常跑的项目报错如下:

Protocol handler start failedCaused by: java.net.SocketException: Permission denied

一模一样的代码,Windows下正常运行,但在mac上面却报错了,实在让人不解,所以这篇文章记录下解决的方案。

Linux不允许普通用户绑定到<= 1024 的TCP端口。 有一个讨论原因[这里](https://unix.stackexchange.com/questions/16564/why-are-the-first-1024-ports-restricted-to-the-root-user-only)。 你试图绑定到80,因此它失败了“权限被拒绝”。 最快和最安全的解决方案是使用大于1024的值配置端口。当您使用Boot的嵌入式Tomcat实例时,使用server.port属性在application.properties中配置端口。

好了,原因找到了,原来在linux(Mac OS)下,为了系统安全,使用小于1024端口时,需要被授权,最快的解决方案就是更换一个大于1024的端口。ok,直接把代码生成器默认的80端口号更改为8082,重启,问题解决。
application.yml

server:
  port: 8082

代码生成器项目访问路径:http://localhost:8082

file

可以看到,已经可以访问代码生成器页面了。

4、逆向

在后台选择所有的表,然后点击生成代码,你会发现已经为我们生成了开发需要的前后端代码,以后我们的工作重心只关注业务逻辑的开发。

5、导入到项目

然后将逆向生成的代码导入到我们的 gulimall_product 模块,我们可以看到默认生成的代码有报错,还不能用,相关的类还没导入:
file

我们可以看到,在逆向生成的代码中,好多公共的类都还没有,也没有 mybatis-plus, lombok,shiro, R 依赖。

6、项目公共模块和依赖完善

①、创建 gulimall-common 公共模块
new->file->moudle->Maven(这里选择Maven而不是spring init.)->项目配置

file

然后在新建的公共模块 gulimall-common 的 pom.xml 中添加公共依赖:

公共依赖:

公共依赖的 pom.xml

<?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>gulimall</artifactId>
    <groupId>com.atguigu.gulimall</groupId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <modelVersion>4.0.0</modelVersion>

  <artifactId>gulimall-common</artifactId>
  <description>每一个微服务的公共的依赖,bean,工具类等</description>

  <properties>
    <mybatisplus.version>3.2.0</mybatisplus.version>
    <commons.lang.version>2.6</commons.lang.version>
    <lombok.version>1.18.4</lombok.version>
    <httpcore.version>4.4.12</httpcore.version>
    <joda.time.version>2.9.9</joda.time.version>
    <commons.io.version>2.5</commons.io.version>
    <servlet-api.version>2.5</servlet-api.version>
  </properties>

  <dependencies>
    <!-- mybatis-plus -->
    <dependency>
      <groupId>com.baomidou</groupId>
     <artifactId>mybatis-plus-boot-starter</artifactId>
      <version>${mybatisplus.version}</version>
    </dependency>

    <!-- lombok -->
    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <version>${lombok.version}</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpcore -->
    <dependency>
      <groupId>org.apache.httpcomponents</groupId>
      <artifactId>httpcore</artifactId>
      <version>${httpcore.version}</version>
    </dependency>

    <dependency>
      <groupId>commons-lang</groupId>
      <artifactId>commons-lang</artifactId>
      <version>${commons.lang.version}</version>
    </dependency>

    <dependency>
      <groupId>joda-time</groupId>
      <artifactId>joda-time</artifactId>
      <version>${joda.time.version}</version>
    </dependency>

    <dependency>
      <groupId>commons-io</groupId>
      <artifactId>commons-io</artifactId>
      <version>${commons.io.version}</version>
    </dependency>

    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>servlet-api</artifactId>
      <version>${servlet-api.version}</version>
      <scope>provided</scope>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>5.0.6.RELEASE</version>
      <scope>compile</scope>
    </dependency>

  </dependencies>
</project>

公共工具类:
在公共模块 gulimall-common 创建公共工具类,包名为 com.atguigu.common.utils,然后将 renren-fast 模块中的src/main/java/io/renren/common/utils/ 需要的工具类导入。

file

导入renren-fast 模块中的src/main/java/io/renren/common/xss/ 文件到 gulimall-common 模块下包名为 com.atguigu.common.xss
导入之后,修改对应的包名,再对 src/main/java/com/atguigu/common/utils/Query.java 文件的引入做修改:

// import io.renren.common.xss.SQLFilter;
import com.atguigu.common.xss.SQLFilter;

然后再将 公共模块加入到我们的微服务模块,这里我添加到 gulimall-product 下边:

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

至此,在 gulimall-product 就可以使用公共模块的服务了。

gulimall-common
gulimall-common 模块的com.atguigu.common.utils工具文件夹类:

// 创建com.atguigu.common.utils,从renren-fast的io.renren.common.utils拷贝
com.atguigu.common.utils.Constant
com.atguigu.common.utils.PageUtils
com.atguigu.common.utils.Query
com.atguigu.common.utils.R
com.atguigu.common.utils.RRException    // 从renren-fast的io.renren.common.exception拷贝

com.atguigu.common.xss 包下的文件

// 创建com.atguigu.common.xss,从renren-fast的io.renren.common.xss拷贝
com.atguigu.common.xss.HTMLFilter
com.atguigu.common.xss.SQLFilter

file

根据自己项目的报错,然后解决各个文件的引用错误。

7、修改生成器模板

生成器默认的是使用shiro做权限管理的,而我们的项目是使用springsecurity 做权限管理,所以,对原来控制器里边的需要做修改,删掉shiro的引用,并且注释权限管理。

修改renren-generator\src\main\resources\template\Controller.java.vm的模板,注释RequiresPermissions相关内容。

import org.apache.shiro.authz.annotation.RequiresPermissions;   // 删掉
//@RequiresPermissions("${moduleName}:${pathName}:list")   // 注释掉

修改完之后,然后再重启代码生成器服务,重新生成gulimall-product 的逆向代码文件,然后将新生成的controller复制覆盖原来的controller:

新生成的Controller文件:AttrController.java

package com.atguigu.gulimall.product.controller;

import java.util.Arrays;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.atguigu.gulimall.product.entity.AttrEntity;
import com.atguigu.gulimall.product.service.AttrService;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;

/**
 * 商品属性
 *
 * @author kaiyi
 * @email corwienwong@gmail.com
 * @date 2020-08-10 22:03:19
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:attr:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    //@RequiresPermissions("product:attr:info")
    public R info(@PathVariable("attrId") Long attrId){
        AttrEntity attr = attrService.getById(attrId);

        return R.ok().put("attr", attr);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attr:save")
    public R save(@RequestBody AttrEntity attr){
        attrService.save(attr);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attr:update")
    public R update(@RequestBody AttrEntity attr){
        attrService.updateById(attr);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attr:delete")
    public R delete(@RequestBody Long[] attrIds){
        attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}

至此,我们的逆向代码生成这块算是改造完了。

为者常成,行者常至