您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關(guān)springboot+idea+maven多模塊項(xiàng)目搭建的方法的內(nèi)容。小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧。
創(chuàng)建之前項(xiàng)目之前 記得改一下 maven 提高下載Pom速度
記得 setting 中要改 maven 改成 阿里云的。具體方法 網(wǎng)上查第一步 搭建parents 項(xiàng)目,為maven項(xiàng)目 ,不為springboot 項(xiàng)目 記得修改groupId
第二步 搭建多個(gè)子模塊, honor-dao honor-manager honor-common記得創(chuàng)建 honor-manager 的時(shí)候 要把他的gruopId 改成com.honor.manager
這里爆紅的原因是 因?yàn)?nbsp;我做到后面然后忘記截屏了,返回來(lái)做。實(shí)際我已經(jīng)創(chuàng)建了 ,所以這個(gè)步驟是對(duì)的。按照這個(gè)做法,創(chuàng)建 honor-dao honor-manager honor-common,記得創(chuàng)建 honor-manager 的時(shí)候 要把他的gruopId 改成com.honor.manager
第三步 在 honor-manager 下分別創(chuàng)建 honor-manager-fc(maven) honor-manager-user(maven) honor-manager-boot (springboot,記得要使用springboot創(chuàng)建)創(chuàng)建時(shí)候 有兩個(gè)坑 第一個(gè)是 父級(jí) 別選錯(cuò) 。他一般會(huì)默認(rèn) 選第一級(jí)別,就是 honor 但是我們要繼承的是 honor-manager第二個(gè)是路徑 別寫(xiě)錯(cuò)
第四步 開(kāi)始對(duì)各大 Pom 進(jìn)行整合 dao 中一般需要引入 common ,common 一般不需要引 其他模塊,然后呢 manager 中要同時(shí)引入dao 和 common第一個(gè)是 honor 父級(jí)的 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> <groupId>com.honor</groupId> <artifactId>phone</artifactId> <packaging>pom</packaging> <version>1.0-SNAPSHOT</version> <modules> <module>honor-dao</module> <module>honor-common</module> <module>honor-manager</module> </modules> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.7.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <honor-common>1.0-SNAPSHOT</honor-common> <honor-dao>1.0-SNAPSHOT</honor-dao> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <skipTests>true</skipTests> <hutool.version>4.5.7</hutool.version> <swagger2.version>2.7.0</swagger2.version> <mybatis-generator.version>1.3.7</mybatis-generator.version> <mybatis.version>3.4.6</mybatis.version> <postgresql.version>42.2.12</postgresql.version> <spring-data-commons.version>2.1.5.RELEASE</spring-data-commons.version> <jjwt.version>0.9.0</jjwt.version> <aliyun-oss.version>2.5.0</aliyun-oss.version> <gson.version>2.8.5</gson.version> <log4j2.version>2.13.3</log4j2.version> <mybatis-plus.version>3.1.0</mybatis-plus.version> <activiti.version>6.0.0</activiti.version> <poi.version>4.1.2</poi.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions><!-- 去掉默認(rèn)配置 --> <!--<exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion>--> <exclusion> <groupId>org.hibernate.validator</groupId> <artifactId>hibernate-validator</artifactId> </exclusion> <exclusion> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> </exclusion> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </exclusion> </exclusions> </dependency> <!--<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency>--> <!-- 移除Tomcat之后會(huì)報(bào)錯(cuò),加入下面的依賴(lài),使用外部容器 --> <!--<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency>--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-slf4j-impl</artifactId> <version>${log4j2.version}</version> <scope>compile</scope> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>${log4j2.version}</version> <scope>compile</scope> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-jul</artifactId> <version>${log4j2.version}</version> <scope>compile</scope> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api</artifactId> <version>${log4j2.version}</version> <scope>compile</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>5.5.0</version> <scope>test</scope> </dependency> <dependency> <groupId>com.honor</groupId> <artifactId>honor-common</artifactId> <version>${honor-common}</version> </dependency> <dependency> <groupId>com.honor</groupId> <artifactId>honor-dao</artifactId> <version>${honor-dao}</version> </dependency> <!--阿里巴巴連接池--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.20</version> </dependency> <!--Swagger-UI API文檔生產(chǎn)工具--> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>${swagger2.version}</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>${swagger2.version}</version> </dependency> <!-- MyBatis--> <!--<dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis.version}</version> </dependency>--> <!-- mybatis plus --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>${mybatis-plus.version}</version> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-generator</artifactId> <version>${mybatis-plus.version}</version> </dependency> <!--mysql--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> <version>8.0.13</version> </dependency> <!--SpringData工具包--> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-commons</artifactId> <version>${spring-data-commons.version}</version> </dependency> <!--JWT(Json Web Token)登錄支持--> <dependency> <groupId>io.jsonwebtoken</groupId> <artifactId>jjwt</artifactId> <version>${jjwt.version}</version> </dependency> <!--Google的JSON解析工具--> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>${gson.version}</version> </dependency> <!-- https://mvnrepository.com/artifact/net.sf.json-lib/json-lib --> <dependency> <groupId>net.sf.json-lib</groupId> <artifactId>json-lib</artifactId> <version>2.4</version> <classifier>jdk15</classifier> </dependency> <!-- https://mvnrepository.com/artifact/org.activiti/activiti-engine --> <dependency> <groupId>org.activiti</groupId> <artifactId>activiti-engine</artifactId> <version>${activiti.version}</version> <exclusions> <exclusion> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> </exclusion> </exclusions> </dependency> <!--沒(méi)有會(huì)引起mybaits plus通用mapper和activity的沖突--> <dependency> <groupId>org.activiti</groupId> <artifactId>activiti-spring-boot-starter-jpa</artifactId> <version>${activiti.version}</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>${poi.version}</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>${poi.version}</version> </dependency> </dependencies> </dependencyManagement> </project>
第二個(gè)是honor-common
<?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>phone</artifactId> <groupId>com.honor</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>honor-common</artifactId> <dependencies> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-annotation</artifactId> <version>3.1.0</version> <scope>compile</scope> </dependency> </dependencies> </project>
第三個(gè)是honor-dao的 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>phone</artifactId> <groupId>com.honor</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>honor-dao</artifactId> <dependencies> <dependency> <groupId>com.honor</groupId> <artifactId>honor-common</artifactId> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-generator</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> </dependencies> </project>
第四個(gè)是honor-manager的
<?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>phone</artifactId> <groupId>com.honor</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <groupId>com.honor.manager</groupId> <artifactId>honor-manager</artifactId> <packaging>pom</packaging> <modules> <module>honor-manager-txn1</module> <module>honor-manager-user1</module> <module>honor-manager-boot</module> </modules> <dependencies> <dependency> <groupId>com.honor</groupId> <artifactId>honor-common</artifactId> </dependency> <dependency> <groupId>com.honor</groupId> <artifactId>honor-dao</artifactId> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>com.honor.manager</groupId> <artifactId>honor-manager-boot</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>com.honor.manager</groupId> <artifactId>>honor-manager-txn1</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>com.honor.manager</groupId> <artifactId>>honor-manager-user1</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies> </dependencyManagement> </project>
然后 是 honor-manager-user
<?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>honor-manager</artifactId> <groupId>com.honor.manager</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>honor-manager-user1</artifactId> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> </project>
然后 是honor-manager-boot記得boot 要引入 txn1 和 user的依賴(lài),記得要把parents 要改一下,是為honor-manager.
<?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> <artifactId>honor-manager</artifactId> <groupId>com.honor.manager</groupId> <version>1.0-SNAPSHOT</version> </parent> <groupId>com.honor.manager</groupId> <artifactId>honor-manager-boot</artifactId> <version>0.0.1-SNAPSHOT</version> <name>honor-manager-boot</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>com.honor.manager</groupId> <artifactId>honor-manager-user1</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>com.honor.manager</groupId> <artifactId>honor-manager-txn1</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> </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> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter</artifactId> <version>RELEASE</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
第二階段 開(kāi)始測(cè)試 首先搭建數(shù)據(jù)庫(kù),做個(gè)簡(jiǎn)單的User 表測(cè)試就行了
然后搭建 beans
User 的內(nèi)容
package com.honor.common.beans; import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableId; import lombok.Data; import lombok.EqualsAndHashCode; import lombok.experimental.Accessors; @Data @EqualsAndHashCode(callSuper = false) @Accessors(chain = true) public class User { @TableId(type = IdType.AUTO) private Integer id; private String username; private String password; }
第二部 dao層
UserMapper接口
package com.honor.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.honor.common.beans.User; import org.apache.ibatis.annotations.Mapper; import java.util.List; @Mapper public interface UserMapper extends BaseMapper<User> { }
UserMapper.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.honor.mapper.UserMapper"> <resultMap id="User" type="com.eft.common.beans.User"/> </mapper>
第三部分 springboot 的application 中加入掃描包的注解
@ServletComponentScan @MapperScan("com.honor.mapper")
然后編寫(xiě)application.yml 文件
server: port: 8880 servlet: context-path: /honor-manager spring: application: name: users datasource: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/eft?characterEncoding=UTF-8&serverTimezone=UTC&useSSL=true username: root password: 111111 mybatis-plus: mapper-locations: - classpath*:com/honor/mapper/*.xml check-config-location: true debug: true
緊接著編寫(xiě) userController 進(jìn)行測(cè)試一定要前綴一樣!這里注意要前綴一樣,要不 掃描不到
UserController代碼
package com.honor.manager.controller; import com.honor.common.beans.User; import com.honor.mapper.UserMapper; import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; import java.util.Map; @RestController public class UserController { @Resource UserMapper userMapper; @RequestMapping( value = "/api/user", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE ) @ResponseBody public User read(@RequestParam Map<String, Object> params) { return userMapper.selectById("1"); } @GetMapping("/api/test") public String hello() { return "hello"; } }
訪問(wèn) http://localhost:8880/honor-manager/api/user
自
此 多級(jí)模塊的項(xiàng)目就搭建成功了!
感謝各位的閱讀!關(guān)于springboot+idea+maven多模塊項(xiàng)目搭建的方法就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。