一、搭建后端项目结构
1、父模块的搭建
pom.xml
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.SR1</spring-cloud.version>
<spring-boot.version>2.0.5.RELEASE</spring-boot.version>
</properties>
<!-- 管理springcloud的版本-->
<dependencyManagement>
<dependencies>
<!--管理springboot版本-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--管理springcloud版本-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
2、注册中心Eureka
(1)依赖
pom.xml
<dependencies>
<!--eureka服务端的场景启动器-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<configuration>
<mainClass>io.niker.hrm.EurekaServerApplication</mainClass>
</configuration>
</plugin>
</plugins>
</build>
(2)配置
application.yml
server:
port: 8761
eureka:
instance:
hostname: localhost
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ #单机版
(3)启动类
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class,args);
}
}
3、配置中心服务端
(1)Gitee搭建配置文件的仓库
略
(2)创建一个springboot项目,只保留resources,存放配置文件
pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
(3)上传配置文件的springboot项目到Gitee仓库中
(4)搭建配置中心服务端
pom.xml
<dependencies>
<!--配置中心服务端场景启动器-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<!--eureka-client的场景启动器-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<configuration>
<mainClass>io.niker.hrm.ConfigServerApplication</mainClass>
</configuration>
</plugin>
</plugins>
</build>
application.yml
# 端口号
server:
port: 1999
# 注册到eureka服务端
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka # 注册到eureka服务端
instance:
instance-id: config-server:1999 # 配置中心的标识
prefer-ip-address: true # 使用ip地址注册
# 配置中心服务名称
spring:
application:
name: CONFIG-SERVER
# 拉取配置
cloud:
config:
server:
git:
uri: # gitee的仓库地址
search-paths: src/main/resources # 从这个文件夹里面拉取配置文件
#username: **** # 用户名
#password: **** # 密码
启动类
@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class,args);
}
}
4、网关
(1)依赖
<dependencies>
<!--网关的场景启动器-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
<!--eureka-client的场景启动器-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--配置中心客户端-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<configuration>
<mainClass>io.niker.hrm.ZuulApplication</mainClass>
</configuration>
</plugin>
</plugins>
</build>
(2)配置
项目中:bootstrap.yml
#配置中心客户端配置
spring:
cloud:
config:
discovery:
enabled: true
service-id: CONFIG-SERVER
name: application-zuul
profile: dev
#eureka客户端配置
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
配置文件仓库中:application-zuul-dev.yml
server:
port: 1299
spring:
application:
name: ZUUL-SERVICE
#测试一波
eureka:
instance:
instance-id: zuul-service:1299 #服务实例的标识
prefer-ip-address: true #以ip注册
zuul:
ignored-services: "*"
prefix: /servies
#路由配置
# routes:
# user:
# path: /myusers/**
# serviceId: users
(3)启动类
@SpringBootApplication
@EnableZuulProxy
public class ZuulApplication {
public static void main(String[] args) {
SpringApplication.run(ZuulApplication.class,args);
}
}
二、myatis-plus的入门
mybatis-plus是使用mybatis的工具,无缝的整合到mybatis的项目中,只做增强,不做改变,如丝般顺滑。
1、入门案例
根据官网走
2、mybatis-plus的核心
(1)CRUD接口
BaseMapper接口
IService
ServceImpl<M extends BaseMapper,T>
都是mybatis-plus为我们提供的基本的CRUD的操作接口和实现类
(2)条件构造器
使用条件构造器构建where后面的条件
@Test
public void testWrapper(){
/**
* 创建一个条件构造器对象
*/
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
//构造条件
queryWrapper.gt("age",24);// age>20
//.......
List<User> list = userService.list(queryWrapper);
for (User user : list) {
System.out.println(user);
}
}
(3)分页插件
-
配置
分页插件不需要添加额外依赖
只需要在@Configuration中配置一个分页插件
/** * 配置分页插件 * @return */ @Bean public PaginationInterceptor paginationInterceptor() { PaginationInterceptor paginationInterceptor = new PaginationInterceptor(); paginationInterceptor.setCountSqlParser(new JsqlParserCountOptimize(true)); return paginationInterceptor; }
-
使用
@Test public void testPage(){ //调用service的page方法,传入Page对象作为分页参数 Page<Employee> page = employeeService.page(new Page(1, 10)); //Page对象的getTotal方法返回总条目数 //Page对象的getRecords方法返回当前页的数据 System.out.println(page.getTotal()); for (Employee employee : page.getRecords()) { System.out.println(employee); } }
(4)代码生成器
项目中要重写编写代码生成器的配置。
public class CodeGenerator {
public static void main(String[] args) {
// 代码生成器
AutoGenerator mpg = new AutoGenerator();
// 全局配置
GlobalConfig gc = new GlobalConfig();
String projectPath = System.getProperty("user.dir");//获取项目路径
gc.setOutputDir(projectPath + "/src/main/java");
gc.setAuthor("solargen");
gc.setOpen(false);
mpg.setGlobalConfig(gc);
// 数据源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl("jdbc:mysql://localhost:3306/mybatisplus?useUnicode=true&useSSL=false&characterEncoding=utf8");
dsc.setDriverName("com.mysql.jdbc.Driver");
dsc.setUsername("root");
dsc.setPassword("123456");
mpg.setDataSource(dsc);
// 包配置
PackageConfig pc = new PackageConfig();
pc.setParent("io.niker.mybatisplus");//设置父包名称
pc.setEntity("domain");
mpg.setPackageInfo(pc);
// 自定义配置 - 重新配置mapper.xml的生成规则
InjectionConfig cfg = new InjectionConfig() {
@Override
public void initMap() {
// to do nothing
}
};
// 如果模板引擎是 velocity
String templatePath = "/templates/mapper.xml.vm";
// 自定义输出配置
List<FileOutConfig> focList = new ArrayList<>();
// mapper.xml
focList.add(new FileOutConfig(templatePath) {
@Override
public String outputFile(TableInfo tableInfo) {
// 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
return projectPath + "/src/main/resources/io/niker/mybatisplus/mapper/" +
"/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
}
});
cfg.setFileOutConfigList(focList);
mpg.setCfg(cfg);
// 配置模板 - mybatisplus都是根据模板生成的文件,如果不想使用它的模板,可以自己设置
//如果模板设置为null,则不会根据他的策略进行生成
TemplateConfig templateConfig = new TemplateConfig();
templateConfig.setXml(null);
mpg.setTemplate(templateConfig);
// 策略配置
StrategyConfig strategy = new StrategyConfig();
strategy.setNaming(NamingStrategy.underline_to_camel);
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
strategy.setEntityLombokModel(true);
strategy.setRestControllerStyle(true);
//生成的表
strategy.setInclude("t_employee","t_department");
strategy.setTablePrefix("t_");
mpg.setStrategy(strategy);
//设置模板引擎
mpg.setTemplateEngine(new VelocityTemplateEngine());
mpg.execute();
}
}
三、系统管理服务中心
1、需求分析
Sass系统:软件及服务
机构需要入驻,购买套餐,享受具体的提供的服务
(1)创建数据库和表
hrm-system
导入sql文件
套餐表 、 权限表 (多对多)
2、模块的设计与划分
3、搭建三个模块,并且添加三个模块的依赖关系
依赖项目中的模块,版本的设计:
<!--依赖common-->
<dependency>
<groupId>io.niker</groupId>
<artifactId>hrm-system-common</artifactId>
<version>${project.version}</version>
</dependency>
4、配置系统管理服务【hrm-system-service】
(1)搭建传统服务-SSM【先不使用配置中心】
(2)将配置信息抽取到配置中心中管理
(3)具体实现
pom.xml
<dependencies>
<!--依赖common-->
<dependency>
<groupId>io.niker</groupId>
<artifactId>hrm-system-common</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!--mybatisplus 的场景启动器-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>${mybatis.plus.version}</version>
</dependency>
<!--mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.connector.java.version}</version>
</dependency>
<!--eureka-client的场景启动器-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--配置中心客户端-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<configuration>
<mainClass>io.niker.hrm.SystemApplication</mainClass>
</configuration>
</plugin>
</plugins>
</build>
bootstrap.yml
#配置中心
spring:
cloud:
config:
discovery:
enabled: true
service-id: CONFIG-SERVER
name: application-system
profile: dev
#eureka客户端配置
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
配置中心文件:
application-system-dev.yml
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/hrm-system?useSSL=false&useUnicode=true&characterEncoding=utf8
username: root
password: 123456
application:
name: HRM-SYSTEM
server:
port: 9001
#eureka客户端配置
eureka:
instance:
prefer-ip-address: true
instance-id: hrm-system:9001
启动类
@SpringBootApplication
@EnableTransactionManagement
@MapperScan("io.niker.hrm.mapper")
public class SystemApplication {
public static void main(String[] args) {
SpringApplication.run(SystemApplication.class,args);
}
}
(3)配置网关的路由
application-zuul-dev.yml
server:
port: 1299
spring:
application:
name: ZUUL-SERVICE
#测试一波
eureka:
instance:
instance-id: zuul-service:1299 #服务实例的标识
prefer-ip-address: true #以ip注册
zuul:
ignored-services: "*"
prefix: /services
#路由配置
routes:
system:
path: /system/**
serviceId: HRM-SYSTEM
(4)集成swagger
-
当前项目中集成swagger
pom.xml
<!--引入swagger支持--> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> </dependency> <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.9.2</version> </dependency>
Swagger2.java Swagger的配置类
package io.niker.hrm.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.service.ApiInfo; import springfox.documentation.service.Contact; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; /** * swagger的配置 * swagger的目的:(1)通过html的方式展示项目中所有的接口(2)提供接口测试 */ @Configuration @EnableSwagger2 public class Swagger2 { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() //对外暴露服务的包,以controller的方式暴露,所以就是controller的包. .apis(RequestHandlerSelectors.basePackage("io.niker.hrm.controller")) .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("系统管理服务api") .description("系统管理服务接口文档说明") .contact(new Contact("lidong", "", "lidong_java@io.niker")) .version("1.0") .build(); } }
-
在网关中集成swagger
为什么要在网关中集成
pom.xml
<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> </dependency> <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.9.2</version> </dependency>
配置
package io.niker.hrm.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import org.springframework.stereotype.Component; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.service.ApiInfo; import springfox.documentation.service.Contact; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger.web.SwaggerResource; import springfox.documentation.swagger.web.SwaggerResourcesProvider; import springfox.documentation.swagger.web.UiConfiguration; import springfox.documentation.swagger2.annotations.EnableSwagger2; import java.util.ArrayList; import java.util.List; @Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("分布式人力资源管理平台") .description("人力资源管理平台接口文档说明") .contact(new Contact("lidong", "", "lidong_java@io.niker")) .version("1.0") .build(); } }
package io.niker.hrm.config; import org.springframework.context.annotation.Primary; import org.springframework.stereotype.Component; import springfox.documentation.swagger.web.SwaggerResource; import springfox.documentation.swagger.web.SwaggerResourcesProvider; import java.util.ArrayList; import java.util.List; @Component @Primary public class DocumentationConfig implements SwaggerResourcesProvider { /** * 服务列表展示 * * /{网关配置的前缀}/{服务对应的url}/v2/api-docs * /services/system/v2/api-docs * @return */ @Override public List<SwaggerResource> get() { List resources = new ArrayList<>(); resources.add(swaggerResource("系统管理", "/services/system/v2/api-docs", "2.0")); resources.add(swaggerResource("系统管理1", "/services/system/v2/api-docs", "2.0")); resources.add(swaggerResource("系统管理2", "/services/system/v2/api-docs", "2.0")); return resources; } private SwaggerResource swaggerResource(String name, String location, String version) { SwaggerResource swaggerResource = new SwaggerResource(); swaggerResource.setName(name); swaggerResource.setLocation(location); swaggerResource.setSwaggerVersion(version); return swaggerResource; } }
-