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

网友投稿 498 2022-11-27


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

xmlns:xsi="http://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">

4.0.0

com.honor

phone

pom

1.0-SNAPSHOT

honor-dao

honor-common

honor-manager

org.springframework.boot

spring-boot-starter-parent

2.1.7.RELEASE

1.0-SNAPSHOT

1.0-SNAPSHOT

UTF-8

UTF-8

1.8

true

4.5.7

2.7.0

1.3.7

3.4.6

42.2.12

2.1.5.RELEASE

0.9.0

2.5.0

2.8.5

2.13.3

3.1.0

6.0.0

4.1.2

org.springframework.boot

spring-boot-starter-web

org.hibernate.validator

hibernate-validator

ch.qos.logback

logback-classic

org.springframework.boot

spring-boot-starter-logging

org.springframework.boot

spring-boot-starter-actuator

org.springframework.boot

spring-boot-starter-aop

org.springframework.boot

spring-boot-starter-test

test

org.apache.logging.log4j

log4j-slf4j-impl

${log4j2.version}

compile

org.apache.logging.log4j

log4j-core

${log4j2.version}

compile

org.apache.logging.log4j

log4j-jul

${log4j2.version}

compile

org.apache.logging.log4j

log4j-api

${log4j2.version}

compile

javax.servlet

javax.servlet-api

3.1.0

provided

org.junit.jupiter

junit-jupiter-api

5.5.0

test

com.honor

honor-common

${honor-common}

com.honor

honor-dao

${honor-dao}

com.alibaba

druid-spring-boot-starter

1.1.20

io.springfox

springfox-swagger2

${swagger2.version}

io.springfox

springfox-swagger-ui

${swagger2.version}

com.baomidou

mybatis-plus-boot-starter

${mybatis-plus.version}

com.baomidou

mybatis-plus-generator

${mybatis-plus.version}

mysql

mysql-connector-java

runtime

8.0.13

org.springframework.data

spring-data-commons

${spring-data-commons.version}

io.jsonwebtoken

jjwt

${jjwt.version}

com.google.code.gson

gson

${gson.version}

net.sf.json-lib

json-lib

2.4

jdk15

org.activiti

activiti-engine

${activiti.version}

org.mybatis

mybatis

org.activiti

activiti-spring-boot-starter-jpa

${activiti.version}

org.apache.poi

poi

${poi.version}

org.apache.poi

poi-ooxml

${poi.version}

xmlns:xsi="http://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">

4.0.0

com.honor

phone

pom

1.0-SNAPSHOT

honor-dao

honor-common

honor-manager

org.springframework.boot

spring-boot-starter-parent

2.1.7.RELEASE

1.0-SNAPSHOT

1.0-SNAPSHOT

UTF-8

UTF-8

1.8

true

4.5.7

2.7.0

1.3.7

3.4.6

42.2.12

2.1.5.RELEASE

0.9.0

2.5.0

2.8.5

2.13.3

3.1.0

6.0.0

4.1.2

org.springframework.boot

spring-boot-starter-web

org.hibernate.validator

hibernate-validator

ch.qos.logback

logback-classic

org.springframework.boot

spring-boot-starter-logging

org.springframework.boot

spring-boot-starter-actuator

org.springframework.boot

spring-boot-starter-aop

org.springframework.boot

spring-boot-starter-test

test

org.apache.logging.log4j

log4j-slf4j-impl

${log4j2.version}

compile

org.apache.logging.log4j

log4j-core

${log4j2.version}

compile

org.apache.logging.log4j

log4j-jul

${log4j2.version}

compile

org.apache.logging.log4j

log4j-api

${log4j2.version}

compile

javax.servlet

javax.servlet-api

3.1.0

provided

org.junit.jupiter

junit-jupiter-api

5.5.0

test

com.honor

honor-common

${honor-common}

com.honor

honor-dao

${honor-dao}

com.alibaba

druid-spring-boot-starter

1.1.20

io.springfox

springfox-swagger2

${swagger2.version}

io.springfox

springfox-swagger-ui

${swagger2.version}

com.baomidou

mybatis-plus-boot-starter

${mybatis-plus.version}

com.baomidou

mybatis-plus-generator

${mybatis-plus.version}

mysql

mysql-connector-java

runtime

8.0.13

org.springframework.data

spring-data-commons

${spring-data-commons.version}

io.jsonwebtoken

jjwt

${jjwt.version}

com.google.code.gson

gson

${gson.version}

net.sf.json-lib

json-lib

2.4

jdk15

org.activiti

activiti-engine

${activiti.version}

org.mybatis

mybatis

org.activiti

activiti-spring-boot-starter-jpa

${activiti.version}

org.apache.poi

poi

${poi.version}

org.apache.poi

poi-ooxml

${poi.version}

第二个是honor-common

xmlns:xsi="http://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">

phone

com.honor

1.0-SNAPSHOT

4.0.0

honor-common

org.projectlombok

lombok

com.baomidou

mybatis-plus-annotation

3.1.0

compile

xmlns:xsi="http://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">

phone

com.honor

1.0-SNAPSHOT

4.0.0

honor-common

org.projectlombok

lombok

com.baomidou

mybatis-plus-annotation

3.1.0

compile

第三个是honor-dao的 pom

xmlns:xsi="http://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">

phone

com.honor

1.0-SNAPSHOT

4.0.0

honor-dao

com.honor

honor-common

com.baomidou

mybatis-plus-boot-starter

com.baomidou

mybatis-plus-generator

mysql

mysql-connector-java

xmlns:xsi="http://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">

phone

com.honor

1.0-SNAPSHOT

4.0.0

honor-dao

com.honor

honor-common

com.baomidou

mybatis-plus-boot-starter

com.baomidou

mybatis-plus-generator

mysql

mysql-connector-java

第四个是honor-manager的

xmlns:xsi="http://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">

phone

com.honor

1.0-SNAPSHOT

4.0.0

com.honor.manager

honor-manager

pom

honor-manager-txn1

honor-manager-user1

honor-manager-boot

com.honor

honor-common

com.honor

honor-dao

com.baomidou

mybatis-plus-boot-starter

org.springframework.boot

spring-boot-starter-web

com.honor.manager

honor-manager-boot

1.0-SNAPSHOT

com.honor.manager

>honor-manager-txn1

1.0-SNAPSHOT

com.honor.manager

>honor-manager-user1

1.0-SNAPSHOT

xmlns:xsi="http://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">

phone

com.honor

1.0-SNAPSHOT

4.0.0

com.honor.manager

honor-manager

pom

honor-manager-txn1

honor-manager-user1

honor-manager-boot

com.honor

honor-common

com.honor

honor-dao

com.baomidou

mybatis-plus-boot-starter

org.springframework.boot

spring-boot-starter-web

com.honor.manager

honor-manager-boot

1.0-SNAPSHOT

com.honor.manager

>honor-manager-txn1

1.0-SNAPSHOT

com.honor.manager

>honor-manager-user1

1.0-SNAPSHOT

然后 是 honor-manager-user

xmlns:xsi="http://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">

honor-manager

com.honor.manager

1.0-SNAPSHOT

4.0.0

honor-manager-user1

org.springframework.boot

spring-boot-starter-web

xmlns:xsi="http://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">

honor-manager

com.honor.manager

1.0-SNAPSHOT

4.0.0

honor-manager-user1

org.springframework.boot

spring-boot-starter-web

然后 是honor-manager-boot记得boot 要引入 txn1 和 user的依赖,记得要把parents 要改一下,是为honor-manager.

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

honor-manager

com.honor.manager

1.0-SNAPSHOT

com.honor.manager

honor-manager-boot

0.0.1-SNAPSHOT

honor-manager-boot

Demo project for Spring Boot

1.8

com.honor.manager

honor-manager-user1

1.0-SNAPSHOT

com.honor.manager

honor-manager-txn1

1.0-SNAPSHOT

mysql

mysql-connector-java

runtime

org.junit.jupiter

junit-jupiter-api

com.alibaba

druid-spring-boot-starter

org.springframework.boot

spring-boot-starter-web

org.springframework.boot

spring-boot-starter-test

test

org.junit.vintage

junit-vintage-engine

org.junit.jupiter

junit-jupiter

RELEASE

test

org.springframework.boot

spring-boot-maven-plugin

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

honor-manager

com.honor.manager

1.0-SNAPSHOT

com.honor.manager

honor-manager-boot

0.0.1-SNAPSHOT

honor-manager-boot

Demo project for Spring Boot

1.8

com.honor.manager

honor-manager-user1

1.0-SNAPSHOT

com.honor.manager

honor-manager-txn1

1.0-SNAPSHOT

mysql

mysql-connector-java

runtime

org.junit.jupiter

junit-jupiter-api

com.alibaba

druid-spring-boot-starter

org.springframework.boot

spring-boot-starter-web

org.springframework.boot

spring-boot-starter-test

test

org.junit.vintage

junit-vintage-engine

org.junit.jupiter

junit-jupiter

RELEASE

test

org.springframework.boot

spring-boot-maven-plugin

第二阶段  开始测试 首先搭建数据库,做个简单的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 {

}

UserMapper.xml

第三部分 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 params) {

return userMapper.selectById("1");

}

@GetMapping("/api/test")

public String hello() {

return "hello";

}

}

访问 http://localhost:8880/honor-manager/api/user

此 多级模块的项目就搭建成功了!

总结


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:springboot 2.3之后消失的hibernate
下一篇:SpringBoot2.3定制错误页面的方法示例
相关文章

 发表评论

暂时没有评论,来抢沙发吧~