点击上方“方志朋”,选择“设为星标”

回复”666“获取新整理的面试资料

来源:http://sina.lt/gmQc

一、前言

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

1、开发工具及系统环境

  • IDE:IntelliJ IDEA 2018.2

  • 系统环境:mac OSX

2、项目目录结构

  • biz层:业务逻辑层

  • dao层:数据持久层

  • web层:请求处理层

二、搭建步骤

1、创建父工程

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

img

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

img

③ 填写输入框,点击Next

img

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

img

⑤ 点击Finish创建项目

img

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

img

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

img

2、创建子模块

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

img

② 选择Maven,点击Next

img

③ 填写ArifactId,点击Next

img

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

img

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

img

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得到如下效果

img

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

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

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

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

<dependencyManagement><dependencies><dependency><groupId>com.yibao.betagroupId><artifactId>beta-bizartifactId><version>${beta.version}version>dependency><dependency><groupId>com.yibao.betagroupId><artifactId>beta-daoartifactId><version>${beta.version}version>dependency><dependency><groupId>com.yibao.betagroupId><artifactId>beta-webartifactId><version>${beta.version}version>dependency>dependencies>dependencyManagement>

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

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

<dependencies><dependency><groupId>com.yibao.betagroupId><artifactId>beta-bizartifactId>dependency>dependencies>

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

<dependencies><dependency><groupId>com.yibao.betagroupId><artifactId>beta-daoartifactId>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 */@Servicepublic class DemoServiceImpl implements DemoService {

@Overridepublic 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 {

@Autowiredprivate 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得到如下效果

img

6、集成Mybatis

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

dependencyManagement><dependencies><dependency><groupId>org.mybatis.spring.bootgroupId><artifactId>mybatis-spring-boot-starterartifactId><version>1.3.2version>dependency><dependency><groupId>org.projectlombokgroupId><artifactId>lombokartifactId><version>1.16.22version>dependency>dependencies>dependencyManagement>

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

<dependencies><dependency><groupId>org.mybatis.spring.bootgroupId><artifactId>mybatis-spring-boot-starterartifactId>dependency><dependency><groupId>mysqlgroupId><artifactId>mysql-connector-javaartifactId>dependency><dependency><groupId>org.projectlombokgroupId><artifactId>lombokartifactId>dependency>dependencies>

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

img

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

spring.datasource.driverClassName = com.mysql.jdbc.Driverspring.datasource.url = jdbc:mysql://192.168.1.1/test?useUnicode=true&characterEncoding=utf-8spring.datasource.username = testspring.datasource.password = 123456

mybatis.mapper-locations = classpath:mybatis/*.xmlmybatis.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 */@Servicepublic class DemoServiceImpl implements DemoService {

@Autowiredprivate UserMapper userMapper;

@Overridepublic 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得到如下效果

img

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

四、总结

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

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

五、未提到的坑

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

热门内容:     

  • 不用找了,大厂在用的分库分表方案,都在这了!

  • Spring Boot + Vue + Shiro 实现前后端分离、权限控制

  • 学 Redis ,至少要看看这篇!7000 字小结

  • 系统运行缓慢,CPU 100%,以及Full GC次数过多问题的排查思路

  • 恕我直言,HttpClient 你不一定会用

  • 除了不要 SELECT * ,数据库还有哪些技巧

  • Redis为什么这么快?一文深入了解Redis内存模型!

最近面试BAT,整理一份面试资料《Java面试BAT通关手册》,覆盖了Java核心技术、JVM、Java并发、SSM、微服务、数据库、数据结构等等。

获取方式:点“在看”,关注公众号并回复 666 领取,更多内容陆续奉上。

明天见(。・ω・。)ノ♡

2020idea创建web项目_Spring Boot + Mybatis 多模块(module)项目的完整搭建教程相关推荐

  1. idea创建springboot项目+mybatis_Spring Boot + MyBatis 多模块项目搭建教程

    Java后端,选择"" 优质文章,及时送达 作者 | 枫本非凡 链接 | cnblogs.com/orzlin/p/9717399.html 上篇 | IDEA 远程一键部署 Sp ...

  2. Spring Boot + Mybatis 多模块(module)项目的完整搭建教程

    点击上方"方志朋",选择"设为星标" 回复"666"获取新整理的面试资料 来源:http://sina.lt/gmQc 一.前言 最近公司项 ...

  3. pom添加mysql依赖tomcat崩溃_Spring Boot + Mybatis + Spring MVC环境配置(一) :Spring Boot初始化,依赖添加...

    最近在搭建一个Spring Boot + Mybatis + Spring MVC的环境,折腾来折腾去,两三天才搞定,记录下大概过程和遇到的错误 看一下Spring Boot官方的介绍 : Sprin ...

  4. hikari如何切换数据源_spring boot+mybatis 多数据源切换(实例讲解)

    由于公司业务划分了多个数据库,开发一个项目会同事调用多个库,经过学习我们采用了注解+aop的方式实现的 1.首先定义一个注解类 @Retention(RetentionPolicy.RUNTIME) ...

  5. vue表单中批量导入功能_spring boot mybatis+ vue 使用POI实现从Excel中批量导入数据

    一.前端vue+element 1.前端使用element的upload组件来实现文件的上传 style="display: inline-flex;margin-right: 8px&qu ...

  6. java怎么新建模块_spring boot添加新模块的方法教程

    前言 在springboot项目框架里,把一个项目两大模块,主项目main和测试项目test,而我们的测试项目根据功能又可以再分,比如可以有单元测试,集成测试,业务测试等等. 对于一个初学者来说,建立 ...

  7. java idea 模块_IDEA搭建java多模块module项目-Go语言中文社区

    1.新建项目 2.用maven创建多模块项目 点击next进入下一步 3.建立groupId,artifactId,version信息 4.建项目名与项目位置 点击finish后进入第5步 5.新建项 ...

  8. druid 多数据源_Spring Boot + Mybatis 中 配置Druid多数据源并实现自由切换

    概述 前面我们已经介绍过了对MyBatis.Druid的整合,接下来我们在之前的基础上做扩展,实现对Druid多数据源的配置以及动态切换数据源. 问题:多数据源使用场景有哪些呢? 回答:在业务发展中, ...

  9. java 分页_Spring Boot + MyBatis 如何借助PageHelper插件实现分页效果

    概述 上文中已经介绍了Spring和MyBatis的整合,在上文的基础上我们加入了PageHelper这个插件,来实现MyBatis列表查询的分页效果 PageHelper是啥 PageHelper是 ...

最新文章

  1. tcl c语言笔试题,TCL技术类笔试题目.doc
  2. js中如何通过身份证号计算出生日期和年龄
  3. 项目微管理18 - 嘴遁
  4. 【转】Sql server锁,独占锁,共享锁,更新锁,乐观锁,悲观锁
  5. 尼康d850相机参数测试软件,尼康 - D850 - 产品介绍
  6. OpenShift 4 之 高可靠运行MS SQL Server 2019数据库
  7. 冯诺依曼结构和哈佛结构01
  8. 边界布局BorderLayout源码解析
  9. vue设置页面滚动高度_vue中获取滚动高度或指定滚动到某位置
  10. storm笔记:Storm+Kafka简单应用
  11. java bytebuffer读取_Java NIO学习笔记之二-图解ByteBuffer
  12. 通达信资金净流入公式_通达信当天净流入公式,通达信资金净流入公式
  13. 基于STM32设计的掌上游戏机(运行NES游戏模拟器)详细开发过程
  14. VS如何安装到电脑上
  15. 如何利用python中的pandas模块计算环比和同比
  16. JS实现中文转拼音首字母和五笔简码
  17. 尚学堂视频笔记一:java面向对象基础和java基础知识
  18. 添加视频字幕后期制作Premiere Pro 2022中文
  19. 高校成绩管理数据库系统的设计与实现
  20. 01-android 微信实现本地视频发布到朋友圈功能

热门文章

  1. LiveVideoStackCon讲师热身分享第一季
  2. 11位大咖带你玩转WebRTC开发(内附PPT资料下载)
  3. 如何在IDEA中使用git
  4. Nginx URL重写(rewrite)配置及信息详解
  5. Cockroach DB 1.0发布
  6. 一步一步理解Paxos算法
  7. 使用bootstrap按钮组并设置其按钮组中按钮的尺寸和距离
  8. mysql中实现over partiton by,进行分组排序取topN
  9. extract local variale 和 jsp中查找选中内容的快捷键
  10. leetcode 782. Transform to Chessboard | 782. 变为棋盘(Java)