创建springboot多模块聚合项目--R
1.先建一个父项目,去掉多余的东西 只留pom文件:
pom如下:
<?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">
<modelVersion>4.0.0</modelVersion>
<packaging>pom</packaging>
<!--包含的子组件-->
<modules>
<module>springboot-dao</module>
<module>springboot-service</module>
<module>springboot-web</module>
<module>springboot-common</module>
</modules>
<!--springboot的版本-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.zhonguo</groupId>
<artifactId>zhuboyuan-good</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>zhuboyuan-good</name>
<description>Demo project for Spring Boot</description>
<!--maven依赖标签中需要各个jar包的版本号-->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<springframework.boot.version>1.5.7.RELEASE</springframework.boot.version>
<springboot.starter-redis.version>1.4.7.RELEASE</springboot.starter-redis.version>
<artifactId-parent>1.0.0-SNAPSHOT</artifactId-parent>
<spring-cloud.version>Dalston.SR3</spring-cloud.version>
<fastjson.version>1.2.9</fastjson.version>
<tk.mybatis.mapper.version>2.0.3</tk.mybatis.mapper.version>
<mysql.connector.version>5.1.45</mysql.connector.version>
<druid.version>1.1.9</druid.version>
<cmp.common.version>2.0.0-SNAPSHOT</cmp.common.version>
<beetl.version>2.9.3</beetl.version>
</properties>
<!--profiles:自定义配置信息管理。
profile:具体自定义配置(可以在不同环境下使用不同的配制文件)。
activation:profile 的子元素,指该配置的**条件。
Activation 是 profile 的开启钥匙,但不是**profile的唯一方式。-->
<profiles>
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<activatedProperties>dev</activatedProperties>
</properties>
</profile>
<profile>
<id>pro</id>
<properties>
<activatedProperties>pro</activatedProperties>
</properties>
</profile>
</profiles>
<!-- dependencies:自动引入声明在dependencies里的所有依赖,并默认被所有的子项目继承。
如果项目中不写依赖项,则会从父项目继承(属性全部继承)声明在父项目dependencies里的依赖项-->
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjson.version}</version>
</dependency>
</dependencies>
<!-- dependencyManagement里只是声明依赖,并不实现引入,因此子项目需要显示的声明需要的依赖。
如果不在子项目中声明依赖,是不会从父项目中继承的;
只有在子项目中写了该依赖项,并且没有指定具体版本,才会从父项目中继承该项,并且version和scope都读取自父pom;
如果子项目中指定了版本号,那么会使用子项目中指定的jar版本。
同时dependencyManagement让子项目引用依赖,而不用显示的列出版本号。
Maven会沿着父子层次向上走,直到找到一个拥有dependencyManagement元素的项目,
然后它就会使用在这个dependencyManagement元素中指定的版本号,实现所有子项目使用的依赖项为同一版本。-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.zhonguo</groupId>
<artifactId>springboot-common</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.zhonguo</groupId>
<artifactId>springboot-dao</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.zhonguo</groupId>
<artifactId>springboot-service</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.zhonguo</groupId>
<artifactId>springboot-web</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.connector.version}</version>
<scope>runtime</scope>
</dependency>
<!-- 为了监控数据库 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>${druid.version}</version>
</dependency>
<!--分页插件-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>${tk.mybatis.mapper.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
<version>${springboot.starter-redis.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<configuration>
<configurationFile>src/main/resources/autogen/generatorConfig.xml</configurationFile>
<overwrite>true</overwrite>
<verbose>true</verbose>
</configuration>
<version>1.3.5</version>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.45</version>
</dependency>
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-generator</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
2.创建子项目:
记得打上-
finish 完成!
项目架构如下:
3.在src/main/java下自己写上和其他模块一样的前缀包名:
4.idea右侧maven:
5.可能会出现子模块变灰的情况,只需要右击父项目重新导入一下变灰的pom文件即可:
6.子模块—dao项目:
架构如图:
6-1:pom引入依赖:
<?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>zhuboyuan-good</artifactId>
<groupId>com.zhonguo</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>springboot-dao</artifactId>
<packaging>jar</packaging>
<name>springboot-dao</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<!--引入依赖 如果父pom中的dependencyManagement标签中有有此依赖的声名,则不用写版本号,直接用父项目的版本号
如果写了版本号 就用写的版本号-->
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
//通用mapper
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
</dependency>
<!-- 为了监控数据库 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
</dependency>
<!--分页插件-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
</dependency>
</dependencies>
</project>
6-2:properties或者yml文件 连接数据库:
logging.level.root=debug
logging.level.tk.mybatis.springboot.mapper=trace
spring.datasource.url=jdbc:mysql://ip:3306/cmp_rule_engine
spring.datasource.username=账号
spring.datasource.password=密码
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.druid.initial-size=1
spring.datasource.druid.min-idle=1
spring.datasource.druid.max-active=20
spring.datasource.druid.test-on-borrow=true
spring.datasource.druid.stat-view-servlet.allow=true
6-3:准备生生成实体,mapper和xml:
generatorConfig.xml的内容如下自己配置好路径数据库信息和要生成哪些表即可:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<properties resource="application-dev.properties"/>
<context id="Mysql" targetRuntime="MyBatis3Simple" defaultModelType="flat">
<property name="beginningDelimiter" value="`"/>
<property name="endingDelimiter" value="`"/>
<plugin type="tk.mybatis.mapper.generator.MapperPlugin">
<property name="mappers" value="com.zhonguo.zhuboyuan.good.springboot.dao.base.MyMapper"/>
</plugin>
<jdbcConnection driverClass="${spring.datasource.driver-class-name}"
connectionURL="${spring.datasource.url}"
userId="${spring.datasource.username}"
password="${spring.datasource.password}">
</jdbcConnection>
<javaModelGenerator targetPackage="com.zhonguo.zhuboyuan.good.springboot.dao.model" targetProject="src/main/java"/>
<sqlMapGenerator targetPackage="sqlmapper" targetProject="src/main/resources"/>
<javaClientGenerator targetPackage="com.zhonguo.zhuboyuan.good.springboot.dao.mapper" targetProject="src/main/java"
type="XMLMAPPER"/>
<table tableName="%">
<!--mysql 配置-->
<generatedKey column="id" sqlStatement="Mysql" identity="true"/>
<!--oracle 配置-->
<!--<generatedKey column="id" sqlStatement="select SEQ_{1}.nextval from dual" identity="false" type="pre"/>-->
</table>
<!--<table tableName="t_score_card">
<generatedKey column="id" sqlStatement="Mysql" identity="true"/>
</table>
<table tableName="t_score_card_flow">
<generatedKey column="id" sqlStatement="Mysql" identity="true"/>
</table>
<table tableName="t_score_card_item_value">
<generatedKey column="id" sqlStatement="Mysql" identity="true"/>
</table>-->
</context>
</generatorConfiguration>
6-3:项目建好之后在父项目中统一编译看有无错误:
6-4:在dao项目中找到mybatis-generator:generate 点击运行即可生成数据库的相应类
7.子模块–service:
7-1:项目结构:
7-2:service的pom文件
<?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>zhuboyuan-good</artifactId>
<groupId>com.zhonguo</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>springboot-service</artifactId>
<packaging>jar</packaging>
<name>springboot-service</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!--引入其他除web的子项目 最后在web项目中只需要引入service这一个项目-->
<dependency>
<groupId>com.zhonguo</groupId>
<artifactId>springboot-dao</artifactId>
</dependency>
<dependency>
<groupId>com.zhonguo</groupId>
<artifactId>springboot-common</artifactId>
</dependency>
<dependency>
<groupId>com.zhonguo</groupId>
<artifactId>springboot-cache</artifactId>
</dependency>
</dependencies>
</project>
8:子模块–web:对外提供接口:
8-1:web结构:
8-2:web的pom:
<?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">
<!--引入父级pom文件-->
<parent>
<artifactId>zhuboyuan-good</artifactId>
<groupId>com.zhonguo</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>springboot-web</artifactId>
<packaging>jar</packaging>
<name>springboot-web</name>
<dependencies>
<!--引入service项目即可-->
<dependency>
<groupId>com.zhonguo</groupId>
<artifactId>springboot-service</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/webapp</directory>
</resource>
<resource>
<directory>src/main/resources</directory>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</build>
</project>
8-3:配置文件(properties和yml都可)配置好数据库信息和其他信息:
application.properties:
## **配置
##[email protected]@
spring.profiles.active=dev
## 端口
server.port=8090
## 服务名称
spring.application.name=ZHUBOYUAN-GOOD
spring.mvc.static-path-pattern=/static/**
spring.mvc.favicon.enabled=false
## mapper框架配置
mybatis.mapper-locations=classpath:sqlmapper/*.xml
mapper.not-empty=false
mapper.identity=MYSQL
application-dev.properties:
## 数据库配置
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://ip:3306/cmp_rule_engine?zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=utf-8
spring.datasource.username=ruleengine
spring.datasource.password=ruleengine
## Redis 配置
## Redis数据库索引(默认为0)
spring.redis.database=0
## Redis服务器地址
spring.redis.host=ip
## Redis服务器连接端口
spring.redis.port=6379
## Redis服务器连接密码(默认为空)
spring.redis.password=rosetta4rfv5TGB
## 连接池最大连接数(使用负值表示没有限制)
spring.redis.pool.max-active=8
## 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.pool.max-wait=-1
## 连接池中的最大空闲连接
spring.redis.pool.max-idle=8
## 连接池中的最小空闲连接
spring.redis.pool.min-idle=0
## 连接超时时间(毫秒)
spring.redis.timeout=3000
logging.level.com.finance.cmp.ruleEngine.dao=debug
9.主类:
package com.zhonguo.zhuboyuan.good.springboot.web;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import tk.mybatis.spring.annotation.MapperScan;
/**
* @ComponentScan注解:
* @ComponentScan注解默认就会装配标识了
* @Controller,@Service,@Repository,@Component注解的类到spring容器中
* 就可以直接用@Autowired把类注入进来使用
* @MapperScan注解:
* 扫描Mapper类的包的路径
*/
@Slf4j
@SpringBootApplication
@ComponentScan("com.zhonguo.zhuboyuan.good.*")
@MapperScan(basePackages = "com.zhonguo.zhuboyuan.good.springboot.dao.mapper")
public class ZhuboyuanGoodApplication {
public static void main(String[] args) {
SpringApplication.run(ZhuboyuanGoodApplication.class, args);
log.info("ZhuboyuanGoodApplication is success!");
}
}
10.到此 基本完成聚合项目创建:
启动:
oj8k