点击查看: Spring Cloud实战教程
Common
模块 中包含的是 请求封装类、响应封装类、分页封装类 等, pom.xml 文件中包含一些子模块 通用的依赖。(父项目 中包含的依赖多为各种依赖的 启动器)
Generator
模块 使用Mybatis-Plus-Generator
依赖, 根据数据库表生成:实体类
、Mapper
、Service
、ServiceImpl
、Controller
代码。
点击查看: Spring Cloud 和 Spring Boot 版本对应
推荐版本:
Spring Cloud
: Hoxton.SR8 (最常用的版本)Spring Cloud Alibaba
: 2.2.5.RELEASESpring Boot
: 2.3.2.RELEASE
所有版本关系:
<properties>
<maven.compiler.source>8maven.compiler.source>
<maven.compiler.target>8maven.compiler.target>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<spring-cloud.version>Hoxton.SR8spring-cloud.version>
<cloud-alibabba.version>2.2.5.RELEASEcloud-alibabba.version>
<mysql.version>8.0.26mysql.version>
<mybatis-plus.version>3.5.2mybatis-plus.version>
<druid.version>1.2.14druid.version>
<kaptcha.version>1.2.1kaptcha.version>
<fastjson.version>1.2.71fastjson.version>
<httpclientutil.version>1.0.4httpclientutil.version>
<commons-lang.version>2.6commons-lang.version>
<commons-collections.version>3.2.2commons-collections.version>
<commons-io.version>2.6commons-io.version>
properties>
Springcloud-实现跨项目相互调用 (简易版: 不使用Feign版本)
点击查看: Maven项目命名规范 (groupID、artifactid)
: 定义当前 Maven项目 隶属的 实际项目,例如
com.compang.project
。
- 前半部分
com.compang
: 代表此项目隶属的 公司。- 后半部分
project
: 代表 项目的名称。- 例如: 腾讯(tencent) 的 微信(wechat)项目:
com.tencent.wechat
。
: 构件ID, 该元素定义实际项目中的一个 Maven项目 或者是 子模块
- 构建名 称必须 小写字母,没有其他的 特殊字符。
- 父项目: 【公司名-项目名】,例如:
tencent-wechat
。- 子模块: 【
公司名-项目名-模块名
】,例如:tencent-wechat-user
、tencent-wechat-file。
> - 子模块方案二: 【项目名-模块名
】,例如:wechat-user
、wechat-file。
父模块
<groupId>com.tencent.wechatgroupId>
<artifactId>tencent-wechatartifactId>
<packaging>pompackaging>
<version>1.0-SNAPSHOTversion>
子模块
<parent>
<groupId>com.tencent.wechatgroupId>
<artifactId>tencent-wechatartifactId>
<version>1.0-SNAPSHOTversion>
parent>
<artifactId>tencent-wechat-userartifactId>
点击查看: Spring Cloud 中 dependencyManagement、type、scope在 父模块 和 子模块 的作用?
- 在 父项目 中使用
dependencyManagement
,继承该 父项目 的 子项目 默认不会直接引入 dependencyManagement 管理的 jar包。- 子项目 要继承 父项目 的 依赖 时, 需要 显式的声明 需要用的 依赖,并且不指定 version,才会从 父项目 中继承该 依赖,这时
version
和scope
都读取自父pom
;
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-dependenciesartifactId>
<version>${spring-cloud.version}version>
<type>pomtype>
<scope>importscope>
dependency>
dependencies>
dependencyManagement>
<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.0modelVersion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.3.2.RELEASEversion>
<relativePath/>
parent>
<groupId>com.tencent.wechatgroupId>
<artifactId>tencent-wechatartifactId>
<version>0.0.1-SNAPSHOTversion>
<packaging>pompackaging>
<modules>
<module>tencent-wechat-usermodule>
<module>tencent-wechat-blogmodule>
<module>tencent-wechat-commonmodule>
modules>
<properties>
<maven.compiler.source>8maven.compiler.source>
<maven.compiler.target>8maven.compiler.target>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<spring-cloud.version>Hoxton.SR8spring-cloud.version>
<cloud-alibabba.version>2.2.5.RELEASEcloud-alibabba.version>
<mysql.version>8.0.26mysql.version>
<mybatis-plus.version>3.5.2mybatis-plus.version>
<druid.version>1.2.14druid.version>
<kaptcha.version>1.2.1kaptcha.version>
<fastjson.version>1.2.71fastjson.version>
<httpclientutil.version>1.0.4httpclientutil.version>
<commons-lang.version>2.6commons-lang.version>
<commons-collections.version>3.2.2commons-collections.version>
<commons-io.version>2.6commons-io.version>
properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-dependenciesartifactId>
<version>${spring-cloud.version}version>
<type>pomtype>
<scope>importscope>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>${mysql.version}version>
dependency>
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plusartifactId>
<version>${mybatis-plus.version}version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druid-spring-boot-starterartifactId>
<version>${druid.version}version>
dependency>
<dependency>
<groupId>com.github.pengglegroupId>
<artifactId>kaptchaartifactId>
<version>${kaptcha.version}version>
dependency>
<dependency>
<groupId>com.aliyun.ossgroupId>
<artifactId>aliyun-sdk-ossartifactId>
<version>3.8.0version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>fastjsonartifactId>
<version>${fastjson.version}version>
dependency>
<dependency>
<groupId>com.arronlonggroupId>
<artifactId>httpclientutilartifactId>
<version>${httpclientutil.version}version>
dependency>
<dependency>
<groupId>commons-langgroupId>
<artifactId>commons-langartifactId>
<version>${commons-lang.version}version>
dependency>
<dependency>
<groupId>commons-collectionsgroupId>
<artifactId>commons-collectionsartifactId>
<version>${commons-collections.version}version>
dependency>
<dependency>
<groupId>commons-iogroupId>
<artifactId>commons-ioartifactId>
<version>${commons-io.version}version>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<optional>trueoptional>
<version>1.18.20version>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.13.2version>
<scope>testscope>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-jdk14artifactId>
<version>1.5.6version>
dependency>
dependencies>
dependencyManagement>
project>
<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.0modelVersion>
<parent>
<groupId>com.tencent.wechatgroupId>
<artifactId>tencent-wechatartifactId>
<version>0.0.1-SNAPSHOTversion>
parent>
<artifactId>tencent-wechat-userartifactId>
<properties>
<java.version>1.8java.version>
<maven.compiler.source>8maven.compiler.source>
<maven.compiler.target>8maven.compiler.target>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
properties>
<dependencies>
<dependency>
<groupId>com.tencent.wechatgroupId>
<artifactId>tencent-wechat-commonartifactId>
<version>0.0.1-SNAPSHOTversion>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-devtoolsartifactId>
dependency>
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-coreartifactId>
<version>3.5.2version>
<scope>compilescope>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
dependency>
<dependency>
<groupId>com.github.yulichanggroupId>
<artifactId>mybatis-plus-joinartifactId>
<version>1.3.8version>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
project>
server:
port: 8190
servlet:
context-path: /user #上下文请求路径,请求前缀 ip:port/user
spring:
application:
name: user-server #应用名
#数据库配置
datasource:
url: jdbc:mysql://xxx.xxx.xxx.xxx:3306/xxx?zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&autoReconnect=true&nullCatalogMeansCurrent=true&allowMultiQueries=true
username: xxx
password: xxx
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis-plus:
type-aliases-package: com.tencent.wechat.user.entities
mapper-locations: classpath*:com/tencent/wechat/user/mapper/**/*.xml
configuration:
map-underscore-to-camel-case: false
logging:
level:
com.pkpmgl.pt.deptuser.mapper: debug
public class CodeGenerator {
//工程
private static String PROJECT_NNAME ="tencent-wechat-user";
// private static String PROJECT_NNAME ="tencent-wechat-blog";
//子包名
private static String MODULE_NAME = "user";
// private static String MODULE_NAME = "blog";
//数据库名称
private static String DATABASE_NAME ="xxx";
public static String scanner(String tip) {
Scanner scanner = new Scanner(System.in);
StringBuilder help = new StringBuilder();
help.append("请输入" + tip + ":");
System.out.println(help.toString());
if (scanner.hasNext()) {
String ipt = scanner.next();
if (StringUtils.isNotEmpty(ipt)) {
return ipt;
}
}
throw new MybatisPlusException("请输入正确的" + tip + "!");
}
public static void main(String[] args) {
//代码生成器
AutoGenerator mpg = new AutoGenerator();
//数据库配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl("jdbc:mysql://xxx.xxx.xxx.68:xxx/"+DATABASE_NAME+"?useUnicode=ture&characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8");
dsc.setDriverName("com.mysql.cj.jdbc.Driver");
dsc.setUsername("xxx");
dsc.setPassword("xxx");
mpg.setDataSource(dsc);
//全局配置
GlobalConfig gc = new GlobalConfig();
String projectPath = System.getProperty("user.dir")+"/";
gc.setOutputDir(projectPath + PROJECT_NNAME +"/src/main/java");
gc.setIdType(IdType.ASSIGN_ID); //分布式id
gc.setAuthor("xqzhao");
gc.setFileOverride(true);
gc.setOpen(false);
gc.setDateType(DateType.ONLY_DATE);
gc.setSwagger2(true);
mpg.setGlobalConfig(gc);
//包全局
PackageConfig pc = new PackageConfig();
pc.setParent("com.tencent.wechat");
pc.setController(MODULE_NAME+".controller");
pc.setService(MODULE_NAME+".service");
pc.setServiceImpl(MODULE_NAME+".service.impl");
pc.setMapper(MODULE_NAME+".mapper");
pc.setXml(MODULE_NAME+".mapper.xml");
pc.setEntity(MODULE_NAME+".entities");
mpg.setPackageInfo(pc);
//策略配置
StrategyConfig strategy = new StrategyConfig();
strategy.setNaming(NamingStrategy.underline_to_camel);
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
strategy.setEntityLombokModel(true);
strategy.setEntitySerialVersionUID(true);
strategy.setRestControllerStyle(true);
strategy.setInclude(scanner("表名,多个英文逗号分割").split(","));
strategy.setControllerMappingHyphenStyle(true);
//strategy.setTablePrefix("T_E_App_");//去掉前缀
mpg.setStrategy(strategy);
mpg.setTemplateEngine(new FreemarkerTemplateEngine());
mpg.execute();
}
}
公用模块包含的 依赖: (一般引入 依赖的启动器)
spring-boot-starter-web
mysql-connector-java
mybatis-plus-boot-starter
druid-spring-boot-starter
mybatis-plus-join
mybatis-plus-join-core
- …
<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.0modelVersion>
<parent>
<groupId>com.tencent.wechatgroupId>
<artifactId>tencent-wechatartifactId>
<version>0.0.1-SNAPSHOTversion>
parent>
<artifactId>tencent-wechat-commonartifactId>
<properties>
<java.version>1.8java.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-validationartifactId>
dependency>
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-boot-starterartifactId>
<version>3.5.2version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druid-spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<scope>runtimescope>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-configuration-processorartifactId>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
dependency>
<dependency>
<groupId>com.spring4allgroupId>
<artifactId>swagger-spring-boot-starterartifactId>
<version>1.9.1.RELEASEversion>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>fastjsonartifactId>
dependency>
<dependency>
<groupId>com.arronlonggroupId>
<artifactId>httpclientutilartifactId>
dependency>
<dependency>
<groupId>commons-langgroupId>
<artifactId>commons-langartifactId>
dependency>
<dependency>
<groupId>commons-collectionsgroupId>
<artifactId>commons-collectionsartifactId>
dependency>
<dependency>
<groupId>commons-iogroupId>
<artifactId>commons-ioartifactId>
dependency>
<dependency>
<groupId>com.github.yulichanggroupId>
<artifactId>mybatis-plus-join-coreartifactId>
<version>1.3.8version>
<scope>compilescope>
dependency>
<dependency>
<groupId>com.github.yulichanggroupId>
<artifactId>mybatis-plus-joinartifactId>
<version>1.3.8version>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
project>
三、项目源码
点击前往 Gitee 查看项目源码