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

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

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

bb64d12c3609e467607e4b1a7ccd3e8a.png

来源: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...

f95e54ef87f580c00244ca5d62164423.png
img

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

8d1398fe6ea4fcbe577ac36f41c3cecc.png
img

③ 填写输入框,点击Next

ebc9aef94c815fdc0f7cf5ae9e4c76ed.png
img

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

584f80e7039b56317b41f0594dfa04f3.png
img

⑤ 点击Finish创建项目

fcc324ab9a7b49c1d86e1654b802d04b.png
img

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

7c8cc9870aa912d90685feab5c32031d.png
img

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

4c551d68bfc5acc08e97a708fdee8685.png
img

2、创建子模块

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

5041b2f5290239cdd189cfb2e2261231.png
img

② 选择Maven,点击Next

57b23c50551f3408ed48b9c9bd5e829c.png
img

③ 填写ArifactId,点击Next

431a147d47323dc5ccf10c51d50d7d13.png
img

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

e3a4cd844850b2fabb9c997a03654f54.png
img

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

54cb4e2361789c57e8a3b34c24f69c19.png
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
*/
@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得到如下效果

cab79cf39575765efec11ae4b8be6d93.png
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
*/
@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得到如下效果

27c15938dfd22020f0d13b124ff9675e.png
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),存放目录如下

29be1a0a4c47843bf993e0f3e3807a09.png
img

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

1f46b0f018ee895e467837133bf27d9a.png
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内存模型!

1bad87fbb20c3d9c9b62acc01bb78a68.png

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

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值