一、前言

最近公司项目准备开始重构,框架选定为SpringBoot+Mybatis,本篇主要记录了在IDEA中搭建SpringBoot多模块项目的过程。

1、开发工具及系统环境

  • IDE:IntelliJ IDEA 2018.2
  • 系统环境:mac OSX

2、项目目录结构

  • biz层:业务逻辑层
  • dao层:数据持久层
  • web层:请求处理层

二、搭建步骤

1、创建父工程

① IDEA 工具栏选择菜单 File -> New -> Project...

② 选择Spring Initializr,Initializr默认选择Default,点击Next

③ 填写输入框,点击Next

④ 这步不需要选择直接点Next

⑤ 点击Finish创建项目

⑥ 最终得到的项目目录结构如下

⑦ 删除无用的.mvn目录、src目录、mvnw及mvnw.cmd文件,最终只留.gitignore和pom.xml

2、创建子模块

① 选择项目根目录beta右键呼出菜单,选择New -> Module

② 选择Maven,点击Next

③ 填写ArifactId,点击Next

④ 修改Module name增加横杠提升可读性,点击Finish

⑤ 同理添加【beta-dao】、【beta-web】子模块,最终得到项目目录结构如下图

3、运行项目

① 在beta-web层创建com.yibao.beta.web包(注意:这是多层目录结构并非单个目录名,com >> yibao >> beta >> web)并添加入口类BetaWebApplication.java

package com.yibao.beta.web;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; /**
* @author linjian
* @date 2018/9/29
*/
@SpringBootApplication
public class BetaWebApplication { public static void main(String[] args) {
SpringApplication.run(BetaWebApplication.class, args);
}
}

② 在com.yibao.beta.web包中添加controller目录并新建一个controller,添加test方法测试接口是否可以正常访问

package com.yibao.beta.web.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; /**
* @author linjian
* @date 2018/9/29
*/
@RestController
@RequestMapping("demo")
public class DemoController { @GetMapping("test")
public String test() {
return "Hello World!";
}
}

③ 运行BetaWebApplication类中的main方法启动项目,默认端口为8080,访问http://localhost:8080/demo/test得到如下效果

以上虽然项目能正常启动,但是模块间的依赖关系却还未添加,下面继续完善

4、配置模块间的依赖关系

各个子模块的依赖关系:biz层依赖dao层,web层依赖biz层

① 父pom文件中声明所有子模块依赖(dependencyManagement及dependencies的区别自行查阅文档)

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>com.yibao.beta</groupId>
            <artifactId>beta-biz</artifactId>
            <version>${beta.version}</version>
        </dependency>
        <dependency>
            <groupId>com.yibao.beta</groupId>
            <artifactId>beta-dao</artifactId>
            <version>${beta.version}</version>
        </dependency>
        <dependency>
            <groupId>com.yibao.beta</groupId>
            <artifactId>beta-web</artifactId>
            <version>${beta.version}</version>
        </dependency>
    </dependencies>
</dependencyManagement>

其中${beta.version}定义在properties标签中

② 在beta-web层中的pom文件中添加beta-biz依赖

<dependencies>
    <dependency>
        <groupId>com.yibao.beta</groupId>
        <artifactId>beta-biz</artifactId>
    </dependency>
</dependencies>

③ 在beta-biz层中的pom文件中添加beta-dao依赖

<dependencies>
    <dependency>
        <groupId>com.yibao.beta</groupId>
        <artifactId>beta-dao</artifactId>
    </dependency>
</dependencies>

5、web层调用biz层接口测试

① 在beta-biz层创建com.yibao.beta.biz包,添加service目录并在其中创建DemoService接口类

package com.yibao.beta.biz.service;

/**
* @author linjian
* @date 2018/9/29
*/
public interface DemoService { String test();
}
package com.yibao.beta.biz.service.impl;

import com.yibao.beta.biz.service.DemoService;
import org.springframework.stereotype.Service; /**
* @author linjian
* @date 2018/9/29
*/
@Service
public class DemoServiceImpl implements DemoService { @Override
public String test() {
return "test";
}
}

② DemoController通过@Autowired注解注入DemoService,修改DemoController的test方法使之调用DemoService的test方法,最终如下所示

package com.yibao.beta.web.controller;

import com.yibao.beta.biz.service.DemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; /**
* @author linjian
* @date 2018/9/29
*/
@RestController
@RequestMapping("demo")
public class DemoController { @Autowired
private DemoService demoService; @GetMapping("test")
public String test() {
return demoService.test();
}
}

③ 再次运行BetaWebApplication类中的main方法启动项目,发现如下报错

***************************
APPLICATION FAILED TO START
*************************** Description: Field demoService in com.yibao.beta.web.controller.DemoController required a bean of type 'com.yibao.beta.biz.service.DemoService' that could not be found. Action: Consider defining a bean of type 'com.yibao.beta.biz.service.DemoService' in your configuration.

原因是找不到DemoService类,此时需要在BetaWebApplication入口类中增加包扫描,设置@SpringBootApplication注解中的scanBasePackages值为com.yibao.beta,最终如下所示

package com.yibao.beta.web;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; /**
* @author linjian
* @date 2018/9/29
*/
@SpringBootApplication(scanBasePackages = "com.yibao.beta")
@MapperScan("com.yibao.beta.dao.mapper")
public class BetaWebApplication { public static void main(String[] args) {
SpringApplication.run(BetaWebApplication.class, args);
}
}

设置完后重新运行main方法,项目正常启动,访问http://localhost:8080/demo/test得到如下效果

6、集成Mybatis

① 父pom文件中声明mybatis-spring-boot-starter及lombok依赖

dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.22</version>
        </dependency>
    </dependencies>
</dependencyManagement>

② 在beta-dao层中的pom文件中添加上述依赖

<dependencies>
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
    </dependency>
</dependencies>

③ 在beta-dao层创建com.yibao.beta.dao包,通过mybatis-genertaor工具生成dao层相关文件(DO、Mapper、xml),存放目录如下

④ applicatio.properties文件添加jdbc及mybatis相应配置项

spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.url = jdbc:mysql://192.168.1.1/test?useUnicode=true&characterEncoding=utf-8
spring.datasource.username = test
spring.datasource.password = 123456 mybatis.mapper-locations = classpath:mybatis/*.xml
mybatis.type-aliases-package = com.yibao.beta.dao.entity

⑤ DemoService通过@Autowired注解注入UserMapper,修改DemoService的test方法使之调用UserMapper的selectByPrimaryKey方法,最终如下所示

package com.yibao.beta.biz.service.impl;

import com.yibao.beta.biz.service.DemoService;
import com.yibao.beta.dao.entity.UserDO;
import com.yibao.beta.dao.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; /**
* @author linjian
* @date 2018/9/29
*/
@Service
public class DemoServiceImpl implements DemoService { @Autowired
private UserMapper userMapper; @Override
public String test() {
UserDO user = userMapper.selectByPrimaryKey(1);
return user.toString();
}
}

⑥ 再次运行BetaWebApplication类中的main方法启动项目,发现如下报错

APPLICATION FAILED TO START
*************************** Description: Field userMapper in com.yibao.beta.biz.service.impl.DemoServiceImpl required a bean of type 'com.yibao.beta.dao.mapper.UserMapper' that could not be found. Action: Consider defining a bean of type 'com.yibao.beta.dao.mapper.UserMapper' in your configuration.

原因是找不到UserMapper类,此时需要在BetaWebApplication入口类中增加dao层包扫描,添加@MapperScan注解并设置其值为com.yibao.beta.dao.mapper,最终如下所示

package com.yibao.beta.web;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; /**
* @author linjian
* @date 2018/9/29
*/
@SpringBootApplication(scanBasePackages = "com.yibao.beta")
@MapperScan("com.yibao.beta.dao.mapper")
public class BetaWebApplication { public static void main(String[] args) {
SpringApplication.run(BetaWebApplication.class, args);
}
}

设置完后重新运行main方法,项目正常启动,访问http://localhost:8080/demo/test得到如下效果

至此,一个简单的SpringBoot+Mybatis多模块项目已经搭建完毕,我们也通过启动项目调用接口验证其正确性。

四、总结

一个层次分明的多模块工程结构不仅方便维护,而且有利于后续微服务化。在此结构的基础上还可以扩展common层(公共组件)、server层(如dubbo对外提供的服务)

此为项目重构的第一步,后续还会的框架中集成logback、disconf、redis、dubbo等组件

五、未提到的坑

在搭建过程中还遇到一个maven私服的问题,原因是公司内部的maven私服配置的中央仓库为阿里的远程仓库,它与maven自带的远程仓库相比有些jar包版本并不全,导致在搭建过程中好几次因为没拉到相应jar包导致项目启动不了。

SpringBoot+Mybatis多模块(module)项目搭建教程的更多相关文章

  1. SpringBoot+Mybatis多模块项目搭建教程

    一.前言 框架为SpringBoot+Mybatis,本篇主要记录了在IDEA中搭建SpringBoot多模块项目的过程. 1.开发工具及系统环境 IDE:IntelliJ IDEA 2018.2 系 ...

  2. springboot + mybatis 前后端分离项目的搭建 适合在学习中的大学生

    人生如戏,戏子多半掉泪! 我是一名大四学生,刚进入一家软件件公司实习,虽说在大学中做过好多个实训项目,都是自己完成,没有组员的配合.但是在这一个月的实习中,我从以前别人教走到了现在的自学,成长很多. ...

  3. SpringBoot&Dubbo&Zookeeper远程调用项目搭建

    序言 Dubbo一款分布式服务框架,作为阿里巴巴SOA服务化治理方案的核心框架,通过高性能和透明化的RPC实现服务的远程调用,对服务的负载均衡以及项目的耦合性提供很强的解决方式;具体Dubbo的介绍和 ...

  4. 基于SpringBoot + Mybatis实现SpringMVC Web项目

    一.热身 一个现实的场景是:当我们开发一个Web工程时,架构师和开发工程师可能更关心项目技术结构上的设计.而几乎所有结构良好的软件(项目)都使用了分层设计.分层设计是将项目按技术职能分为几个内聚的部分 ...

  5. Spring Boot + MyBatis 多模块项目搭建教程

    一.前言 1.开发工具及系统环境 IDE:IntelliJ IDEA 2020.2.2 系统环境:Windows 2.项目目录结构 biz层:业务逻辑层 dao层:数据持久层 web层:请求处理层 二 ...

  6. 使用idea 搭建一个 SpringBoot + Mybatis + logback 的maven 项目

    (注意项目名不能有大写......),把项目类型 改成 War 类型.(web项目) 使用 mybatis-generator 插件 生成 实体类 和 接口 在 resources 目录 中 新建一个 ...

  7. springboot学习之构建简单项目搭建

    概述 相信对于Java开发者而言,spring和springMvc两个框架一定不陌生,这两个框架需要我们手动配置的地方非常多,各种的xml文件,properties文件,构建一个项目还是挺复杂的,在这 ...

  8. springboot+mybatis+freemark+oauth开发环境搭建

    一.创建springboot工程 1.环境介绍: a:jdk版本:1.7 b:Springboot版本:1.5.6(使用1.5.9的版本整合mybatis会报错:java.lang.NoClassDe ...

  9. .Net Core2.2 + EF Core + DI,三层框架项目搭建教程

    笔记: 近两年.Net Core发展的很快,目前最新版为3.0预览版,之前在网上买了一本1.1版书籍都还没来得及看呢,估计现在拿出来看也毫无意义了.已多年.net工作经验,看书不如直接实际上手来得快, ...

随机推荐

  1. ubuntu16.04+anaconda的安装+解决conda不可用(配置路径)+卸载

    首先一点,之前我一直自己安装python,然后直接在python环境下再安装第三方库,但自从另一台电脑重装系统之后,我当时在没有python的情况下直接安装的anaconda,觉得她超级好用(所以如果 ...

  2. Theano教程:Python的内存管理

    在写大型程序时候的一大挑战是如何保证最少的内存使用率.但是在Python中的内存管理是比较简单的.Python显示分配内存,使用引用计数系统管理对象,当指向某一个对象的引用数变为 0 的时候,该对象所 ...

  3. LOJ 3093: 洛谷 P5323: 「BJOI2019」光线

    题目传送门:LOJ #3093. 题意简述: 有 \(n\) 面玻璃,第 \(i\) 面的透光率为 \(a\),反射率为 \(b\). 问把这 \(n\) 面玻璃按顺序叠在一起后,\(n\) 层玻璃的 ...

  4. uboot中的快捷菜单的制作说明 【转】

    转自:http://blog.chinaunix.net/uid-22030783-id-366971.html   在uboot中加入快捷操作菜单的方法非常简单,在论坛发布的uboot201003V ...

  5. mysql安装与卸载(阿里云)

    1.安装rpm包rpm -Uvh http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm 2.安装mysqlyum -y i ...

  6. 修改centos和ubuntu ssh远程连接端口提升系统安全性

    #修改centos服务器ssh端口 sed -i 's/#Port 22/Port 38390/' /etc/ssh/sshd_config sed -i 's/^GSSAPIAuthenticati ...

  7. vue系列之获取多选框中被选中的值

    多个勾选框,绑定到同一个数组: <input type="checkbox" id="jack" value="Jack" v-mod ...

  8. 从LeNet-5到DenseNet

    一篇不错的总结:https://zhuanlan.zhihu.com/p/31006686

  9. hdu3282 链表或者对顶堆

    维护序列的动态中位数 第一次用链表做题..感觉指针指来指去也挺麻烦的.. 本题链表解法就是用数组模拟出一个链表,然后离线输入所有数,排序,按照输入顺序在链表里删除元素,一次性删掉两个,然后中位数指针对 ...

  10. git提交本地分支到远程分支

    git提交本地分支到远程分支   git 常用命令(含删除文件) Git常用操作命令收集: 1) 远程仓库相关命令 检出仓库:$ git clone git://github.com/jquery/j ...