SpringBoot+Mybatis多模块(module)项目搭建教程

一、前言

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

1、开发工具及系统环境

  • IDE:IntelliJ IDEA
  • 系统环境: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
 */
@SpringBootApplicationpublic 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包导致项目启动不了。

原文地址:https://www.cnblogs.com/orzlin/p/9717399.html

时间: 2024-08-01 20:54:08

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

基于SpringBoot + Mybatis实现SpringMVC Web项目

一.热身 一个现实的场景是:当我们开发一个Web工程时,架构师和开发工程师可能更关心项目技术结构上的设计.而几乎所有结构良好的软件(项目)都使用了分层设计.分层设计是将项目按技术职能分为几个内聚的部分,从而将技术或接口的实现细节隐藏起来. 从另一个角度上来看,结构上的分层往往也能促进了技术人员的分工,可以使开发人员更专注于某一层业务与功能的实现,比如前端工程师只关心页面的展示与交互效果(例如专注于HTML,JS等),而后端工程师只关心数据和业务逻辑的处理(专注于Java,Mysql等).两者之间

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

概述 相信对于Java开发者而言,spring和springMvc两个框架一定不陌生,这两个框架需要我们手动配置的地方非常多,各种的xml文件,properties文件,构建一个项目还是挺复杂的,在这种情况下,springboot应运而生,他能够快速的构建spring项目,而且让项目正常运行起来的配置文件非常少,甚至只需要几个注解就可以运行整个项目. 总的说来,springboot项目可以打成jar包独立运行部署,因为它内嵌servlet容器,之前spring,springMvc需要的大量依赖,

vue.js项目搭建教程(参考)

第一步:安装node.js 1.传送门下载安装https://nodejs.org/en/ 2.打开cmd控制台分别查看node版本.path,npm node -v path npm -v 3.推荐淘宝镜像 npm config set registry https://registry.npm.taobao.org 4.安装全局vue-cli脚手架,并检查 cnpm install --global vue-cli vue -V 第二步:建立一个vue-test文件夹,并使用webpack模

IDEA SpringBoot多模块项目搭建详细过程(转)

文章转自https://blog.csdn.net/zcf980/article/details/83040029 项目源码: 链接: https://pan.baidu.com/s/1Gp9cY1Qf51tG9-5gUZsnHQ 提取码: 5izt CSDN源码下载: https://download.csdn.net/download/zcf980/10719615 1. 项目介绍: 本项目包含一个父工程 demo  和 四 个子模块(demo-base, demo-dao, demo-se

Spring+SpringMvc+Mybatis框架集成搭建教程一(背景介绍及项目创建)

一.背景 最近有很多同学由于没有过SSM(Spring+SpringMvc+Mybatis , 以下简称SSM)框架的搭建的经历,所以在自己搭建SSM框架集成的时候,出现了这样或者那样的问题,很是苦恼,网络上又没有很详细的讲解以及搭建的教程.闲来无事,我就利用空闲时间来写这样一个教程和搭建步骤,来帮助那些有问题的小伙伴,让你从此SSM搭建不再有问题. 二.搭建步骤 1.框架搭建环境 Spring 4.2.6.RELEASE SpringMvc 4.2.6.RELEASE Mybatis 3.2.

使用idea+springboot+Mybatis搭建web项目

使用idea+springboot+Mybatis搭建web项目 springboot的优势之一就是快速搭建项目,省去了自己导入jar包和配置xml的时间,使用非常方便. 1.创建项目project,然后选择Spring initializr,点击下一步  2.按图示进行勾选,点击下一步,给项目起个名字,点击确定. 3.项目生成有,点击add as maven project,idea 会自动下载jar包,时间比较长  4.项目生成后格式如下图所示:  其中DemoApplication.jav

springboot基于maven多模块项目搭建(直接启动webApplication)

1. 新建maven项目springboot-module 2.把src删掉,新建module项目 springboot-module-api springboot-module-model springboot-module-service springboot-module-util springboot-module-web 3. 添加模块之间的依赖 3.1   springboot-module.pom 1 <?xml version="1.0" encoding=&qu

Spring Boot 项目实战(一)Maven 多模块项目搭建

Maven父项目 以SpringBoot项目为例https://blog.csdn.net/weixin_30606669/article/details/99478544 Maven 多模块父子工程 (含Spring Boot示例)https://www.cnblogs.com/meitanzai/p/10945085.html https://www.cnblogs.com/orzlin/p/10330163.html 一.前言 最近公司项目准备开始重构,框架选定为 Spring Boot

spring-boot 2.0 多模块化项目和EurekaServer的搭建

Spring boot由于其 1.易于开发和维护.2.单个微服务启动快.3.局部修改部署容易.4.技术栈不受语言限制等优点受到越来越多公司的重视.spring-boot还集成了许多关于微服务开发的框架(例如配置管理,服务发现,断路器,智能路由,微代理,控制总线,一次性令牌,全局锁,领导选举,分布式 会话,群集状态),使我们部署微服务免去了繁琐的配置. 下面我们来学习利用spring-boot搭建电商项目. Spring Boot 2.0.0-SNAPSHOT 要求 Java 8 和 Spring