SpringBoot多模块搭建
发布日期:2021-05-08 09:46:08 浏览次数:24 分类:精选文章

本文共 4465 字,大约阅读时间需要 14 分钟。

Spring Boot 多模块项目搭建指南

项目结构

本项目采用模块化设计,通过Maven管理多个子模块,实现高效的项目维护和管理。项目结构如下:

├── demo-parent
│ ├── demo-dao
│ │ ├── pom.xml
│ │ └── application-dao.properties
│ ├── demo-biz
│ │ ├── pom.xml
│ │ ├── DemoService.java
│ │ └── DemoServiceImpl.java
│ └── demo-web
│ ├── pom.xml
│ ├── DemoController.java
│ └── application.properties

父项目配置

父项目demo-parentpom.xml配置如下:

4.0.0
jar
demo-dao
demo-biz
demo-web
org.springframework.boot
spring-boot-starter-parent
2.2.2.RELEASE
../
com.example
demo
0.0.1-SNAPSHOT

子模块创建

1. Dao模块

pom.xml

demo
com.example
0.0.1-SNAPSHOT
4.0.0
demo-dao
org.mybatis.spring.boot
mybatis-spring-boot-starter
${mybatis.version}
mysql
mysql-connector-java
${mysql-connector.version}
org.project.lombok
lombok
${lombok.version}

应用配置

spring.datasource.driver-class-name=com.mysql.jdbc.Driverspring.datasource.url=jdbc:mysql://localhost:3306/goods?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8spring.datasource.username=rootspring.datasource.password=rootmybatis.mapper-locations=classpath:mybatis/*.xmlmybatis.type-aliases-package=com.demo.dao.entity

2. Biz模块

pom.xml

demo
com.example
0.0.1-SNAPSHOT
4.0.0
demo-biz
com.example
demo-dao
${demo.version}

服务接口与实现

package com.demo.biz.service;
public interface DemoService {
String test();
}
package com.demo.biz.service.impl;
import com.demo.biz.service.DemoService;
import com.demo.dao.entity.Good;
import com.demo.dao.mapper.GoodMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class DemoServiceImpl implements DemoService {
@Autowired
private GoodMapper goodMapper;
public String test() {
Good good = goodMapper.selectByPrimaryKey("1");
return good.toString();
}
}

3. Web模块

pom.xml

demo
com.example
0.0.1-SNAPSHOT
4.0.0
demo-web
com.example
demo-biz
${demo.version}
org.springframework.boot
spring-boot-starter-web
${springboot.version}

Web应用配置

package com.demo.web;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication(scanBasePackages = "com.demo")
@MapperScan("com.demo.dao.mapper")
public class DemoWebApplication {
public static void main(String[] args) {
SpringApplication.run(DemoWebApplication.class, args);
}
}

Web控制器

package com.demo.web.controller;
import com.demo.biz.service.DemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("demo")
public class DemoController {
@Autowired
private DemoService demoService;
@GetMapping("test")
public String test() {
return demoService.test();
}
}

运行说明

  • 打开终端,进入父项目目录,执行以下命令:
  • mvn spring-boot:run
    1. 访问地址:
      • http://localhost:8080/demo/test

      以上配置文件和项目结构可根据实际需求进行修改和扩展,方便多环境配置管理,确保代码的可维护性和可扩展性。

    上一篇:MyCat加MySQL实现读写分离,故障转移
    下一篇:MySQL5.7主从同步

    发表评论

    最新留言

    很好
    [***.229.124.182]2025年04月18日 03时10分15秒