本文主要是介绍【前后端的那些事】解放后端!10min快速上手人人代码生成器(后端篇),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
人人代码生成器【后端篇】
文章目录
- 人人代码生成器【后端篇】
- 1. 克隆renren-generator
- 2. 配置项目信息
- 3. 配置数据库信息
- 4. 启动项目
- 5. 创建springboot项目
- 5.1 pom.xml
- 5.2 创建包结构
- 5.3 编写application.yml
- 5.4 将生成代码集成到项目中
- 5.5 集成common模块
- 5.6 启动项目
前言:最近写项目,发现了一些很有意思的功能,想写文章,录视频把这些内容记录下。但这些功能太零碎,如果为每个功能都单独搭建一个项目,这明显不合适。于是我想,就搭建一个项目,把那些我想将的小功能全部整合到一起。实现 搭一次环境,处处使用。
本文主要实现以下功能
- 集成人人代码生成器【后端】
已录制视频
视频链接
仓库地址
https://github.com/xuhuafeifei/fgbg-font-and-back.git
1. 克隆renren-generator
git clone https://gitee.com/renrenio/renren-generator.git
2. 配置项目信息
点击/resource/generator.properties
将下述信息按照自己的需求进行配置
本文涉及到的项目,mainPath
等参数填写如下
不知道mainPath
等参数什么意思的,可以看看template目录下,Controller.java.vm这个模板类中的代码
tip:
- package:包名
- moduleName: 模块名
- mainPath:主路径
我们当前设置下,目录结构如下
- com- fgbg- test 【模块名】- controller- service- entity...- common 【通用模块】...后续介绍
3. 配置数据库信息
代码生成器是依据数据库信息,来生成对应的增删改查代码。因此,我们需要设置我们想要生成增删改查代码的数据库信息
点击application.yml,配置url,username,password
4. 启动项目
在浏览器中输入localhost:80
,启动项目前端
在菜单栏中选择renren-fast,在界面中勾选需要生成对应代码的数据库表,点击生成代码即可
5. 创建springboot项目
5.1 pom.xml
我使用的是springboot2.7.5,jdk8
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.7.5</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.fgbg</groupId><artifactId>demo</artifactId><version>0.0.1-SNAPSHOT</version><name>demo</name><description>Demo project for Spring Boot</description><properties><java.version>1.8</java.version><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version><mybatisplus.version>3.3.1</mybatisplus.version><mysql.version>8.0.28</mysql.version><mssql.version>4.0</mssql.version><oracle.version>11.2.0.3</oracle.version><druid.version>1.1.13</druid.version><quartz.version>2.3.0</quartz.version><commons.lang.version>2.6</commons.lang.version><commons.fileupload.version>1.2.2</commons.fileupload.version><commons.io.version>2.5</commons.io.version><commons.codec.version>1.10</commons.codec.version><commons.configuration.version>1.10</commons.configuration.version><shiro.version>1.9.0</shiro.version><jwt.version>0.7.0</jwt.version><kaptcha.version>0.0.9</kaptcha.version><qiniu.version>7.2.23</qiniu.version><aliyun.oss.version>2.8.3</aliyun.oss.version><qcloud.cos.version>4.4</qcloud.cos.version><swagger.version>2.7.0</swagger.version><joda.time.version>2.9.9</joda.time.version><gson.version>2.8.5</gson.version><hutool.version>4.1.1</hutool.version><lombok.version>1.18.4</lombok.version></properties><dependencies><dependency><groupId>com.fasterxml.jackson.datatype</groupId><artifactId>jackson-datatype-jsr310</artifactId></dependency><!--日志--><dependency><groupId>ch.qos.logback</groupId><artifactId>logback-classic</artifactId></dependency><dependency><groupId>ch.qos.logback</groupId><artifactId>logback-access</artifactId></dependency><dependency><groupId>ch.qos.logback</groupId><artifactId>logback-core</artifactId></dependency><dependency><groupId>commons-dbcp</groupId><artifactId>commons-dbcp</artifactId><version>1.4</version></dependency><dependency><groupId>com.auth0</groupId><artifactId>java-jwt</artifactId><version>3.2.0</version></dependency><dependency><groupId>com.aliyun</groupId><artifactId>dysmsapi20170525</artifactId><version>2.0.23</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-validation</artifactId></dependency><dependency><groupId>commons-io</groupId><artifactId>commons-io</artifactId><version>2.11.0</version></dependency><dependency><groupId>javax.annotation</groupId><artifactId>javax.annotation-api</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jdbc</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>${mybatisplus.version}</version><exclusions><exclusion><groupId>com.baomidou</groupId><artifactId>mybatis-plus-generator</artifactId></exclusion></exclusions></dependency><dependency><groupId>org.apache.shiro</groupId><artifactId>shiro-core</artifactId><version>${shiro.version}</version></dependency><dependency><groupId>org.apache.shiro</groupId><artifactId>shiro-spring</artifactId><version>${shiro.version}</version></dependency><dependency><groupId>io.jsonwebtoken</groupId><artifactId>jjwt</artifactId><version>${jwt.version}</version></dependency><dependency><groupId>com.github.axet</groupId><artifactId>kaptcha</artifactId><version>${kaptcha.version}</version></dependency><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger2</artifactId><version>${swagger.version}</version></dependency><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger-ui</artifactId><version>${swagger.version}</version></dependency><dependency><groupId>com.qiniu</groupId><artifactId>qiniu-java-sdk</artifactId><version>${qiniu.version}</version></dependency><dependency><groupId>com.aliyun.oss</groupId><artifactId>aliyun-sdk-oss</artifactId><version>${aliyun.oss.version}</version></dependency><dependency><groupId>com.qcloud</groupId><artifactId>cos_api</artifactId><version>${qcloud.cos.version}</version><exclusions><exclusion><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId></exclusion></exclusions></dependency><dependency><groupId>joda-time</groupId><artifactId>joda-time</artifactId><version>${joda.time.version}</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.79</version></dependency><dependency><groupId>com.google.code.gson</groupId><artifactId>gson</artifactId><version>${gson.version}</version></dependency><dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId><version>${hutool.version}</version></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>${lombok.version}</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency><dependency><groupId>commons-lang</groupId><artifactId>commons-lang</artifactId><version>${commons.lang.version}</version></dependency><dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId></dependency><!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-redis --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId><version>2.7.5</version></dependency><!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-redis --><dependency><groupId>org.springframework.data</groupId><artifactId>spring-data-redis</artifactId><version>2.7.5</version></dependency><dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><version>3.1.0</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>druid-spring-boot-starter</artifactId><version>1.2.13</version></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>io.minio</groupId><artifactId>minio</artifactId><version>8.2.2</version></dependency><dependency><groupId>org.apache.commons</groupId><artifactId>commons-lang3</artifactId></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><excludes><exclude><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></exclude></excludes></configuration></plugin></plugins></build>
</project>
5.2 创建包结构
因为我们的mainPath是com.fgbg【详见第2部分】,所以创建的时候包结构创建2层即可
具体如下
TestApplication.java 启动类
package com.fgbg;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class TestApplication {public static void main(String[] args) {SpringApplication.run(TestApplication.class, args);}
}
5.3 编写application.yml
application.yml 启动环境为dev
# Tomcat
server:tomcat:uri-encoding: UTF-8max-threads: 1000min-spare-threads: 30port: 9006connection-timeout: 5000ms
# servlet:
# context-path: /api_zbt # 在后端所有的请求路径前添加/api_zbtspring:# 环境 dev|test|prodprofiles:active: dev # 启动dev环境# jackson时间格式化jackson:time-zone: GMT+8date-format: yyyy-MM-dd HH:mm:ssservlet:multipart:max-file-size: 100MBmax-request-size: 100MBenabled: trueredis:open: false # 是否开启redis缓存 true开启 false关闭database: 0host: localhostport: 6379# password: 123456 # 密码(默认为空)timeout: 6000ms # 连接超时时长(毫秒)jedis:pool:max-active: 1000 # 连接池最大连接数(使用负值表示没有限制)max-wait: -1ms # 连接池最大阻塞等待时间(使用负值表示没有限制)max-idle: 10 # 连接池中的最大空闲连接min-idle: 5 # 连接池中的最小空闲连接mvc:throw-exception-if-no-handler-found: truepathmatch:matching-strategy: ANT_PATH_MATCHER
# resources:
# add-mappings: false#mybatis
mybatis-plus:logging:level:# org.springframework: warnorg.apache.ibatis.logging: debug# com.tmxk.municipal.**.dao: debugmapper-locations: classpath*:/mapper/**/*.xml#实体扫描,多个package用逗号或者分号分隔typeAliasesPackage: io.renren.modules.*.entityglobal-config:#数据库相关配置db-config:#主键类型 AUTO:"数据库ID自增", INPUT:"用户输入ID", ID_WORKER:"全局唯一ID (数字类型唯一ID)", UUID:"全局唯一ID UUID";# id-type: ASSIGN_IDid-type: AUTOlogic-delete-value: 0logic-not-delete-value: 1banner: false#原生配置configuration:map-underscore-to-camel-case: truecache-enabled: falsecall-setters-on-nulls: truejdbc-type-for-null: 'null'log-impl: org.apache.ibatis.logging.slf4j.Slf4jImplrenren:redis:open: falseshiro:redis: false# APP模块,是通过jwt认证的,如果要使用APP模块,则需要修改【加密秘钥】jwt:# 加密秘钥secret: f4e2e52034348f86b67cde581c0f9eb5[www.renren.io]# token有效时长,7天,单位秒expire: 604800header: token
application-dev.yml
spring:datasource:type: com.alibaba.druid.pool.DruidDataSourcedruid:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghaiusername: rootpassword: rootinitial-size: 10max-active: 100min-idle: 10max-wait: 60000pool-prepared-statements: truemax-pool-prepared-statement-per-connection-size: 20time-between-eviction-runs-millis: 60000min-evictable-idle-time-millis: 300000#Oracle需要打开注释#validation-query: SELECT 1 FROM DUALtest-while-idle: truetest-on-borrow: falsetest-on-return: falsestat-view-servlet:enabled: trueurl-pattern: /druid/*#login-username: admin#login-password: adminfilter:stat:log-slow-sql: trueslow-sql-millis: 1000merge-sql: falsewall:config:multi-statement-allow: true
5.4 将生成代码集成到项目中
解压缩文件,将文件中的main文件夹全部cv覆盖项目中的main文件夹
覆盖springboot项目的main模块
完美集成!
但点击CouresController,会发现有报错
这是因为项目中缺少common模块导致的
5.5 集成common模块
common模块的官方做法是,从renren-fast后端项目中cv过来。但官方的代码cv过来会出现包模块错误的问题,而且功能冗余,兼容性不太好。所以使用我自己整理好的common模块,具体获取方式为关注微信公众号 一只学Java的飞鸽呀,回复common
,即可获取common代码
现在,我们将common集成到项目中
重点:ctrl+shift+r,用idea自带的全局替换功能,将com.zbg.common
替换成我们自己项目中,common模块的路径com.fgbg.common
现在Controller不报错了,几乎都没咋改动代码,完美集成
5.6 启动项目
测试查询接口
成功!
这篇关于【前后端的那些事】解放后端!10min快速上手人人代码生成器(后端篇)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!