欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

springboot+idea+maven 多模块项目搭建的详细过程(连接数据库进行测试)

程序员文章站 2022-04-25 18:13:35
创建之前项目之前记得改一下 maven 提高下载pom速度记得 setting中要改 maven 改成阿里云的。具体方法网上查第一步搭建parents项目,为maven项目 ,不为springboot...

创建之前项目之前 记得改一下 maven  提高下载pom速度 

springboot+idea+maven 多模块项目搭建的详细过程(连接数据库进行测试)

 记得 setting 中要改 maven  改成 阿里云的。具体方法 网上查第一步 搭建parents 项目,为maven项目 ,不为springboot 项目 记得修改groupid 

springboot+idea+maven 多模块项目搭建的详细过程(连接数据库进行测试)  

第二步 搭建多个子模块, honor-dao   honor-manager   honor-common记得创建 honor-manager 的时候 要把他的gruopid 改成com.honor.manager

springboot+idea+maven 多模块项目搭建的详细过程(连接数据库进行测试)springboot+idea+maven 多模块项目搭建的详细过程(连接数据库进行测试)

这里爆红的原因是 因为 我做到后面然后忘记截屏了,返回来做。实际我已经创建了 ,所以这个步骤是对的。按照这个做法,创建 honor-dao   honor-manager   honor-common,记得创建 honor-manager 的时候 要把他的gruopid 改成com.honor.manager

springboot+idea+maven 多模块项目搭建的详细过程(连接数据库进行测试)

第三步  在 honor-manager 下分别创建 honor-manager-fc(maven)  honor-manager-user(maven) honor-manager-boot (springboot,记得要使用springboot创建)创建时候 有两个坑 第一个是 父级  别选错 。他一般会默认 选第一级别,就是 honor  但是我们要继承的是 honor-manager第二个是路径 别写错

springboot+idea+maven 多模块项目搭建的详细过程(连接数据库进行测试)springboot+idea+maven 多模块项目搭建的详细过程(连接数据库进行测试)

第四步 开始对各大 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 表测试就行了 

springboot+idea+maven 多模块项目搭建的详细过程(连接数据库进行测试) 

然后搭建 beans 

springboot+idea+maven 多模块项目搭建的详细过程(连接数据库进行测试) 

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层

springboot+idea+maven 多模块项目搭建的详细过程(连接数据库进行测试)

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 中加入扫描包的注解

springboot+idea+maven 多模块项目搭建的详细过程(连接数据库进行测试) 

@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 进行测试一定要前缀一样!这里注意要前缀一样,要不 扫描不到    

springboot+idea+maven 多模块项目搭建的详细过程(连接数据库进行测试)

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 多模块项目搭建的详细过程(连接数据库进行测试)的文章就介绍到这了,更多相关springboot+idea+maven 多模块项目搭建 内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!