springboot+idea+maven 多模块项目搭建的详细过程(连接数据库进行测试)
创建之前项目之前 记得改一下 maven 提高下载Pom速度
记得 setting 中要改 maven 改成 阿里云的。具体方法 网上查第一步 搭建parents 项目,为maven项目 ,不为springboot 项目 记得修改groupId
第二步 搭建多个子模块, honor-dao honor-manager honor-common记得创建 honor-manager 的时候 要把他的gruopId 改成com.honor.manager
这里爆红的原因是 因为 我做到后面然后忘记截屏了,返回来做。实际我已经创建了 ,所以这个步骤是对的。按照这个做法,创建 honor-dao honor-manager honor-common,记得创建 honor-manager 的时候 要把他的gruopId 改成com.honor.manager
第三步 在 honor-manager 下分别创建 honor-manager-fc(maven) honor-manager-user(maven) honor-manager-boot (springboot,记得要使用springboot创建)创建时候 有两个坑 第一个是 父级 别选错 。他一般会默认 选第一级别,就是 honor 但是我们要继承的是 honor-manager第二个是路径 别写错
第四步 开始对各大 Pom 进行整合 dao 中一般需要引入 common ,common 一般不需要引 其他模块,然后呢 manager 中要同时引入dao 和 common第一个是 honor 父级的 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><!-- 去掉默认配置 --> <!--<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之后会报错,加入下面的依赖,使用外部容器 --> <!--<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文档生产工具--> <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> <!--没有会引起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>
第二个是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>
第三个是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>
第四个是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的依赖,记得要把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>
第二阶段 开始测试 首先搭建数据库,做个简单的User 表测试就行了
然后搭建 beans
User 的内容
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")
然后编写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
紧接着编写 userController 进行测试一定要前缀一样!这里注意要前缀一样,要不 扫描不到
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"; } }
访问 http://localhost:8880/honor-manager/api/user
自
此 多级模块的项目就搭建成功了!
总结
到此这篇关于springboot+idea+maven 多模块项目搭建的详细过程(连接数据库进行测试)的文章就介绍到这了,更多相关springboot+idea+maven 多模块项目搭建 内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!