最近公司项目准备开始重构,框架选定为SpringBoot+Mybatis,本篇主要记录了在IDEA中搭建SpringBoot多模块项目的过程。java
① IDEA 工具栏选择菜单 File -> New -> Project...mysql
② 选择Spring Initializr,Initializr默认选择Default,点击Nextgit
③ 填写输入框,点击Nextweb
④ 这步不须要选择直接点Nextredis
⑤ 点击Finish建立项目spring
⑥ 最终获得的项目目录结构以下sql
⑦ 删除无用的.mvn目录、src目录、mvnw及mvnw.cmd文件,最终只留.gitignore和pom.xmlmybatis
① 选择项目根目录beta右键呼出菜单,选择New -> Moduleapp
② 选择Maven,点击Next框架
③ 填写ArifactId,点击Next
④ 修改Module name增长横杠提高可读性,点击Finish
⑤ 同理添加【beta-dao】、【beta-web】子模块,最终获得项目目录结构以下图
① 在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获得以下效果
以上虽然项目能正常启动,可是模块间的依赖关系却还未添加,下面继续完善
各个子模块的依赖关系: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>
① 在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获得以下效果
① 父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包致使项目启动不了。