代码生成器生成所有微服务基本CRUD代码(renren-generator)

358 阅读4分钟

携手创作,共同成长!这是我参与「掘金日新计划 · 8 月更文挑战」的第4天,点击查看活动详情

代码生成器注意点

controller模板

自动生成的controller包中代码带有安全框架shiro,这个我们不需要所以直接注释掉

修改自动生成的模板,注释掉全部的

  • 注解:@RequiresPermissions
  • 包:import org.apache.shiro.authz.annotation.RequiresPermissions;

image-20220727220907377

自定义类的返回路径

因为我们把一些通用返回类异常等都放到了mall-common模块,所以mall-common包的定义要根据代码生成器这个包的路径来写;

比如:同一返回类我们要在写在主路径.common.utils包下;

不然全部的controller都会报包的位置错误!

mainpath就是代码生成器模块配置文件指定的

image-202207281046164681、git clone克隆到桌面

image-20220727204918870

2、修改配置

这里拿product模块做示范

mainPath=com.caq
#\u5305\u540D
package=com.caq.mall
moduleName=product
#\u4F5C\u8005
author=xiaocai
#Email
email=mildcaq@gmail.com
#\u8868\u524D\u7F00(\u7C7B\u540D\u4E0D\u4F1A\u5305\u542B\u8868\u524D\u7F00)
tablePrefix=pms_

application.yml

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

3、运行模块

访问:http://localhost:80/

image-20220727205744304

4、新建common模块

因为自动生成的代码,需要有同一返回类、统一异常处理....

这些代码都在renren-fast模块,我们直接拷贝到mall-common模块即可

各模块所需代码如下:

image-20220727213039350

依赖如下:

父pom中引入新家的common模块
<modules>
    <module>mall-product</module>
    <module>mall-coupon</module>
    <module>mall-member</module>
    <module>mall-order</module>
    <module>mall-ware</module>
    <module>renren-fast</module>
    <module>renren-generator</module>
    <module>mall-common</module>
</modules>
​
common引入如下依赖
<dependencies>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.22</version>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.4.1</version>
        </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>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.17</version>
        </dependency>
    </dependencies>

5、拷贝代码注意点

打开模块所在文件夹进行拷贝

image-20220728104041823

product模块

1、引入common模块

生成的代码都要引入common模块,因为自定义的一些类都指定在了mall-common模块

2、配置文件

application.yml

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

mybatis-plus:
  global-config:
    db-config:
      id-type: auto
  mapper-locations: classpath:/mapper/**/*.xml
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

application.properties

用来配置端口和服务名称

# 应用名称
spring.application.name=mall-product
# 应用服务 WEB 访问端口
server.port=9999

3、启动类增加mappersan注解

这里强调下:

代码自动生成器生成的代码中,Mapper文件都加了@Mapper注解,所以我们不用加@MapperScan

如果你自己写的话,MapperScan是个更好的选择

@MapperScan("com.caq.mall.product.dao")

4、测试

package com.caq.mall;
​
import com.caq.mall.product.entity.BrandEntity;
import com.caq.mall.product.service.BrandService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
​
@SpringBootTest
class MallProductApplicationTests {
​
    @Autowired
    private BrandService brandService;
​
    @Test
    void contextLoads() {
        BrandEntity brandEntity = new BrandEntity();
        brandEntity.setName("苹果");
        brandService.save(brandEntity);
    }
​
}
​
JDBC Connection [HikariProxyConnection@1371953731 wrapping com.mysql.cj.jdbc.ConnectionImpl@740dcae3] will not be managed by Spring
==>  Preparing: INSERT INTO pms_brand ( name ) VALUES ( ? )
==> Parameters: 苹果(String)
<==    Updates: 1

5、上传代码到github

上传失败可搜索我的文章,今天刚更新的

Java初学者必看,这篇文章

image-20220727230049663

coupon模块

1、修改代码生成器配置文件

mainPath=com.caq
#\u5305\u540D
package=com.caq.mall
moduleName=coupon
#\u4F5C\u8005
author=xiaocai
#Email
email=mildcaq@gmail.com
#\u8868\u524D\u7F00(\u7C7B\u540D\u4E0D\u4F1A\u5305\u542B\u8868\u524D\u7F00)
tablePrefix=sms_

yml文件

server:
  port: 80

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

2、重启服务,重新生成代码文件

image-20220728102255561

3、拷贝代码,建立coupon配置文件

因为我们是通过SpringInit初始化的模块,所以每个模块都有自带的配置文件applicaiton.properties

改端口的话我们可以修改applicaiton.properties文件

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

mybatis-plus:
  mapper-locations: classpath:/mapper/**/*.xml
  global-config:
    db-config:
      id-type: auto

4、运行,测试

http://localhost:7000/coupon/coupon/list

{"msg":"success","code":0,"page":{"totalCount":0,"pageSize":10,"totalPage":0,"currPage":1,"list":[]}}

member模块

重复上述操作,改代码生成器配置文件,运行生成代码,拷贝代码到模块

拷贝后的代码,建立配置文件,改端口,运行

测试,没问题即可

image-20220728110033046

order模块

重复上述操作,改代码生成器配置文件(模块名,数据库名),运行生成代码,拷贝代码到模块

重复上述操作,拷贝后的代码,建立配置文件,改端口,运行

ware模块

重复上述操作,改代码生成器配置文件(模块名,数据库名),运行生成代码,拷贝代码到模块

重复上述操作,拷贝后的代码,建立配置文件,改端口,运行