1. 首页
  2. IT资讯

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

“u003Cpu003EJava后端,选择“”u003Cu002Fpu003Eu003Cpu003E优质文章,及时送达u003Cu002Fpu003Eu003Cimg src=”http:u002Fu002Fp1.pstatp.comu002Flargeu002Fpgc-imageu002FRcsUnd92eoboDR” img_width=”640″ img_height=”29″ alt=”Spring Boot + MyBatis 多模块项目搭建教程” inline=”0″u003Eu003Cpu003E作者 | 枫本非凡u003Cu002Fpu003Eu003Cpu003E链接 | cnblogsu003Ci class=”chrome-extension-mutihighlight chrome-extension-mutihighlight-style-6″u003E.comu003Cu002Fiu003Eu002Forzlinu002Fpu002F971u003Ci class=”chrome-extension-mutihighlight chrome-extension-mutihighlight-style-1″u003E73u003Cu002Fiu003E99.htmlu003Cu002Fpu003Eu003Cpu003E上篇 | IDEA 远程一键部署 Spring Boot 到 Dockeru003Cu002Fpu003Eu003Cpu003Eu003Cstrong toutiao-origin=”span” class=”highlight-text”u003E一、前言u003Cu002Fstrongu003Eu003Cu002Fpu003Eu003Cpu003E最近公司项目准备开始重构,框架选定为SpringBoot+Mybatis,本篇主要记录了在IDEA中搭建SpringBoot多模块项目的过程。u003Cu002Fpu003Eu003Cpu003Eu003Cu002Fpu003Eu003Ch2 toutiao-origin=”h3″u003Eu003Cstrongu003E1、开发工具及系统环境u003Cu002Fstrongu003Eu003Cu002Fh2u003Eu003Culu003Eu003Cliu003Eu003Cpu003EIDE:u003Cu002Fpu003Eu003Cpu003EIntelliJ IDEA 2018.2u003Cu002Fpu003Eu003Cu002Fliu003Eu003Cliu003Eu003Cpu003E系统环境:u003Cu002Fpu003Eu003Cpu003Emac OSXu003Cu002Fpu003Eu003Cu002Fliu003Eu003Cu002Fulu003Eu003Cpu003Eu003Cu002Fpu003Eu003Ch2 toutiao-origin=”h3″u003Eu003Cstrongu003E2、项目目录结构u003Cu002Fstrongu003Eu003Cu002Fh2u003Eu003Culu003Eu003Cliu003Eu003Cpu003Ebiz层:u003Cu002Fpu003Eu003Cpu003E业务逻辑层u003Cu002Fpu003Eu003Cu002Fliu003Eu003Cliu003Eu003Cpu003Edao层:u003Cu002Fpu003Eu003Cpu003E数据持久层u003Cu002Fpu003Eu003Cu002Fliu003Eu003Cliu003Eu003Cpu003Eweb层:u003Cu002Fpu003Eu003Cpu003E请求处理层u003Cu002Fpu003Eu003Cu002Fliu003Eu003Cu002Fulu003Eu003Cpu003Eu003Cu002Fpu003Eu003Ch1 toutiao-origin=”h2″u003E二、搭建步骤u003Cu002Fh1u003Eu003Ch2 toutiao-origin=”h3″u003E1、创建父工程u003Cu002Fh2u003Eu003Cpu003EIDEA 工具栏选择菜单 File -> New -> Project…u003Cu002Fpu003Eu003Cimg src=”http:u002Fu002Fp1.pstatp.comu002Flargeu002Fpgc-imageu002FRdXNQFn72RDCfl” img_width=”1080″ img_height=”104″ alt=”Spring Boot + MyBatis 多模块项目搭建教程” inline=”0″u003Eu003Cpu003E选择Spring Initializr,Initializr默认选择Default,点击Nextu003Cu002Fpu003Eu003Cimg src=”http:u002Fu002Fp3.pstatp.comu002Flargeu002Fpgc-imageu002FRdXNQG98rEFh2n” img_width=”1080″ img_height=”675″ alt=”Spring Boot + MyBatis 多模块项目搭建教程” inline=”0″u003Eu003Cpu003E填写输入框,点击Nextu003Cu002Fpu003Eu003Cimg src=”http:u002Fu002Fp3.pstatp.comu002Flargeu002Fpgc-imageu002FRdXNQGT6oshSFL” img_width=”1080″ img_height=”675″ alt=”Spring Boot + MyBatis 多模块项目搭建教程” inline=”0″u003Eu003Cpu003E这步不需要选择直接点Nextu003Cu002Fpu003Eu003Cimg src=”http:u002Fu002Fp1.pstatp.comu002Flargeu002Fpgc-imageu002FRdXNQGf1T6vicC” img_width=”1080″ img_height=”675″ alt=”Spring Boot + MyBatis 多模块项目搭建教程” inline=”0″u003Eu003Cpu003E点击Finish创建项目u003Cu002Fpu003Eu003Cimg src=”http:u002Fu002Fp3.pstatp.comu002Flargeu002Fpgc-imageu002FRdXNQGsHdUaXGZ” img_width=”1080″ img_height=”675″ alt=”Spring Boot + MyBatis 多模块项目搭建教程” inline=”0″u003Eu003Cpu003E最终得到的项目目录结构如下u003Cu002Fpu003Eu003Cimg src=”http:u002Fu002Fp1.pstatp.comu002Flargeu002Fpgc-imageu002FRdXNQn74rZiknn” img_width=”1080″ img_height=”610″ alt=”Spring Boot + MyBatis 多模块项目搭建教程” inline=”0″u003Eu003Cpu003E删除无用的.mvn目录、src目录、mvnw及mvnw.cmd文件,最终只留.gitignore和pom.xmlu003Cu002Fpu003Eu003Cimg src=”http:u002Fu002Fp3.pstatp.comu002Flargeu002Fpgc-imageu002FRdXNQnVN6RL1G” img_width=”1080″ img_height=”602″ alt=”Spring Boot + MyBatis 多模块项目搭建教程” inline=”0″u003Eu003Cpu003Eu003Cu002Fpu003Eu003Ch2 toutiao-origin=”h3″u003E2、创建子模块u003Cu002Fh2u003Eu003Cpu003E选择项目根目录beta右键呼出菜单,选择New -> Moduleu003Cu002Fpu003Eu003Cimg src=”http:u002Fu002Fp1.pstatp.comu002Flargeu002Fpgc-imageu002FRdXNQnl9h0RiIo” img_width=”1080″ img_height=”347″ alt=”Spring Boot + MyBatis 多模块项目搭建教程” inline=”0″u003Eu003Cpu003E选择Maven,点击Nextu003Cu002Fpu003Eu003Cimg src=”http:u002Fu002Fp3.pstatp.comu002Flargeu002Fpgc-imageu002FRdXNQoAHMS4hJa” img_width=”1080″ img_height=”675″ alt=”Spring Boot + MyBatis 多模块项目搭建教程” inline=”0″u003Eu003Cpu003E填写ArifactId,点击Nextu003Cu002Fpu003Eu003Cimg src=”http:u002Fu002Fp3.pstatp.comu002Flargeu002Fpgc-imageu002FRdXNQoYEydGfEu” img_width=”1080″ img_height=”675″ alt=”Spring Boot + MyBatis 多模块项目搭建教程” inline=”0″u003Eu003Cpu003E修改Module name增加横杠提升可读性,点击Finishu003Cu002Fpu003Eu003Cimg src=”http:u002Fu002Fp1.pstatp.comu002Flargeu002Fpgc-imageu002FRdXNRY49k1ydUf” img_width=”1080″ img_height=”675″ alt=”Spring Boot + MyBatis 多模块项目搭建教程” inline=”0″u003Eu003Cpu003E同理添加beta-dao、beta-web子模块,最终得到项目目录结构如下图u003Cu002Fpu003Eu003Cimg src=”http:u002Fu002Fp1.pstatp.comu002Flargeu002Fpgc-imageu002FRdXNRYU3gXgV87″ img_width=”1080″ img_height=”602″ alt=”Spring Boot + MyBatis 多模块项目搭建教程” inline=”0″u003Eu003Cpu003Eu003Cu002Fpu003Eu003Ch2 toutiao-origin=”h3″u003E3、运行项目u003Cu002Fh2u003Eu003Cpu003E在beta-web层创建com.yibao.beta.web包(注意:这是多层目录结构并非单个目录名,com >> yibao >> beta >> web)并添加入口类BetaWebApplication.javau003Cu002Fpu003Eu003Cpreu003Eu003Cdivu003Eu003Cpu003Eu003Ccodeu003E@SpringBootApplicationu003Cu002Fcodeu003Eu003Ccodeu003Epublic class BetaWebApplication {u003Cu002Fcodeu003Eu003Cu002Fpu003Eu003Cpu003Eu003Ccodeu003Epublic static void main(String[] args) {u003Cu002Fcodeu003Eu003Ccodeu003E SpringApplication.run(BetaWebApplication.class, args);u003Cu002Fcodeu003Eu003Ccodeu003E }u003Cu002Fcodeu003Eu003Ccodeu003E}u003Cu002Fcodeu003Eu003Cu002Fpu003Eu003Cu002Fdivu003Eu003Cu002Fpreu003Eu003Cpu003E在com.yibao.beta.web包中添加controller目录并新建一个controller,添加test方法测试接口是否可以正常访问u003Cu002Fpu003Eu003Cpreu003Eu003Cdivu003Eu003Cpu003Eu003Ccodeu003E@RestControlleru003Cu002Fcodeu003Eu003Ccodeu003E@RequestMapping(“demo”)u003Cu002Fcodeu003Eu003Ccodeu003Epublic class DemoController {u003Cu002Fcodeu003Eu003Cu002Fpu003Eu003Cpu003Eu003Ccodeu003E@GetMapping(“test”)u003Cu002Fcodeu003Eu003Ccodeu003E public String test {u003Cu002Fcodeu003Eu003Ccodeu003E return “Hello World!”;u003Cu002Fcodeu003Eu003Ccodeu003E }u003Cu002Fcodeu003Eu003Ccodeu003E}u003Cu002Fcodeu003Eu003Cu002Fpu003Eu003Cu002Fdivu003Eu003Cu002Fpreu003Eu003Cpu003E运行BetaWebApplication类中的main方法启动项目,默认端口为8080,访问http:u002Fu002Flocalhost:8080u002Fdemou002Ftest得到如下效果u003Cu002Fpu003Eu003Cimg src=”http:u002Fu002Fp1.pstatp.comu002Flargeu002Fpgc-imageu002FRdXNRYkACxT34K” img_width=”806″ img_height=”244″ alt=”Spring Boot + MyBatis 多模块项目搭建教程” inline=”0″u003Eu003Cpu003E以上虽然项目能正常启动,但是模块间的依赖关系却还未添加,下面继续完善。u003Cemu003Eu003Ci class=”chrome-extension-mutihighlight chrome-extension-mutihighlight-style-1″u003E微信u003Cu002Fiu003E搜索 web_resource 获取更多推送u003Cu002Femu003Eu003Cu002Fpu003Eu003Cpu003Eu003Cu002Fpu003Eu003Ch2 toutiao-origin=”h3″u003E4、配置模块间的依赖关系u003Cu002Fh2u003Eu003Cpu003E各个子模块的依赖关系:biz层依赖dao层,web层依赖biz层u003Cu002Fpu003Eu003Cpu003E父pom文件中声明所有子模块依赖(dependencyManagement及dependencies的区别自行查阅文档)u003Cu002Fpu003Eu003Cpreu003Eu003Cpu003Eu003Ccodeu003E<dependencyManagement>u003Cu002Fcodeu003Eu003Ccodeu003E <dependencies>u003Cu002Fcodeu003Eu003Ccodeu003E <dependency>u003Cu002Fcodeu003Eu003Ccodeu003E <groupId>com.yibao.beta<u002FgroupId>u003Cu002Fcodeu003Eu003Ccodeu003E <artifactId>beta-biz<u002FartifactId>u003Cu002Fcodeu003Eu003Ccodeu003E <version>${beta.version}<u002Fversion>u003Cu002Fcodeu003Eu003Ccodeu003E <u002Fdependency>u003Cu002Fcodeu003Eu003Ccodeu003E <dependency>u003Cu002Fcodeu003Eu003Ccodeu003E <groupId>com.yibao.beta<u002FgroupId>u003Cu002Fcodeu003Eu003Ccodeu003E <artifactId>beta-dao<u002FartifactId>u003Cu002Fcodeu003Eu003Ccodeu003E <version>${beta.version}<u002Fversion>u003Cu002Fcodeu003Eu003Ccodeu003E <u002Fdependency>u003Cu002Fcodeu003Eu003Ccodeu003E <dependency>u003Cu002Fcodeu003Eu003Ccodeu003E <groupId>com.yibao.beta<u002FgroupId>u003Cu002Fcodeu003Eu003Ccodeu003E <artifactId>beta-web<u002FartifactId>u003Cu002Fcodeu003Eu003Ccodeu003E <version>${beta.version}<u002Fversion>u003Cu002Fcodeu003Eu003Ccodeu003E <u002Fdependency>u003Cu002Fcodeu003Eu003Ccodeu003E <u002Fdependencies>u003Cu002Fcodeu003Eu003Ccodeu003E<u002FdependencyManagement>u003Cu002Fcodeu003Eu003Cu002Fpu003Eu003Cu002Fpreu003Eu003Cpu003E其中${beta.version}定义在properties标签中u003Cu002Fpu003Eu003Cpu003E在beta-web层中的pom文件中添加beta-biz依赖u003Cu002Fpu003Eu003Cpreu003Eu003Cpu003Eu003Ccodeu003E<dependencies>u003Cu002Fcodeu003Eu003Ccodeu003E <dependency>u003Cu002Fcodeu003Eu003Ccodeu003E <groupId>com.yibao.beta<u002FgroupId>u003Cu002Fcodeu003Eu003Ccodeu003E <artifactId>beta-biz<u002FartifactId>u003Cu002Fcodeu003Eu003Ccodeu003E <u002Fdependency>u003Cu002Fcodeu003Eu003Ccodeu003E<u002Fdependencies>u003Cu002Fcodeu003Eu003Cu002Fpu003Eu003Cu002Fpreu003Eu003Cpu003E在beta-biz层中的pom文件中添加beta-dao依赖u003Cu002Fpu003Eu003Cpreu003Eu003Cpu003Eu003Ccodeu003E<dependencies>u003Cu002Fcodeu003Eu003Ccodeu003E <dependency>u003Cu002Fcodeu003Eu003Ccodeu003E <groupId>com.yibao.beta<u002FgroupId>u003Cu002Fcodeu003Eu003Ccodeu003E <artifactId>beta-dao<u002FartifactId>u003Cu002Fcodeu003Eu003Ccodeu003E <u002Fdependency>u003Cu002Fcodeu003Eu003Ccodeu003E<u002Fdependencies>u003Cu002Fcodeu003Eu003Cu002Fpu003Eu003Cu002Fpreu003Eu003Cpu003Eu003Cstrongu003E4. web层调用biz层接口测试u003Cu002Fstrongu003Eu003Cu002Fpu003Eu003Cpu003E在beta-biz层创建com.yibao.beta.biz包,添加service目录并在其中创建DemoService接口类,u003Cemu003Eu003Ci class=”chrome-extension-mutihighlight chrome-extension-mutihighlight-style-1″u003E微信u003Cu002Fiu003E搜索 web_resource 获取更多推送u003Cu002Femu003Eu003Cu002Fpu003Eu003Cpreu003Eu003Cpu003Eu003Ccodeu003Epublic interface DemoService {u003Cu002Fcodeu003Eu003Ccodeu003E String test;u003Cu002Fcodeu003Eu003Ccodeu003E}u003Cu002Fcodeu003Eu003Cu002Fpu003Eu003Cu002Fpreu003Eu003Cpreu003Eu003Cdivu003Eu003Cpu003Eu003Ccodeu003E@Serviceu003Cu002Fcodeu003Eu003Ccodeu003Epublic class DemoServiceImpl implements DemoService {u003Cu002Fcodeu003Eu003Cu002Fpu003Eu003Cpu003Eu003Ccodeu003E@Overrideu003Cu002Fcodeu003Eu003Ccodeu003E public String test {u003Cu002Fcodeu003Eu003Ccodeu003E return “test”;u003Cu002Fcodeu003Eu003Ccodeu003E }u003Cu002Fcodeu003Eu003Ccodeu003E}u003Cu002Fcodeu003Eu003Cu002Fpu003Eu003Cu002Fdivu003Eu003Cu002Fpreu003Eu003Cpu003EDemoController通过@Autowired注解注入DemoService,修改DemoController的test方法使之调用DemoService的test方法,最终如下所示:u003Cu002Fpu003Eu003Cpreu003Eu003Cdivu003Eu003Cpu003Eu003Ccodeu003Epackage com.yibao.beta.web.controller;@RestControlleru003Cu002Fcodeu003Eu003Ccodeu003E@RequestMapping(“demo”)u003Cu002Fcodeu003Eu003Ccodeu003Epublic class DemoController {u003Cu002Fcodeu003Eu003Cu002Fpu003Eu003Cpu003Eu003Ccodeu003E@Autowiredu003Cu002Fcodeu003Eu003Ccodeu003E private DemoService demoService;u003Cu002Fcodeu003Eu003Cu002Fpu003Eu003Cpu003Eu003Ccodeu003E@GetMapping(“test”)u003Cu002Fcodeu003Eu003Ccodeu003E public String test {u003Cu002Fcodeu003Eu003Ccodeu003E return demoService.test;u003Cu002Fcodeu003Eu003Ccodeu003E }u003Cu002Fcodeu003Eu003Ccodeu003E}u003Cu002Fcodeu003Eu003Cu002Fpu003Eu003Cu002Fdivu003Eu003Cu002Fpreu003Eu003Cpu003E再次运行BetaWebApplication类中的main方法启动项目,发现如下报错u003Cu002Fpu003Eu003Cpreu003Eu003Cdivu003Eu003Cpu003Eu003Ccodeu003E***************************u003Cu002Fcodeu003Eu003Ccodeu003EAPPLICATION FAILED TO STARTu003Cu002Fcodeu003Eu003Ccodeu003E***************************u003Cu002Fcodeu003Eu003Cu002Fpu003Eu003Cpu003Eu003Ccodeu003EDescription:u003Cu002Fcodeu003Eu003Ccodeu003EField demoService in com.yibao.beta.web.controller.DemoController required a bean of type ‘com.yibao.beta.biz.service.DemoService’ that could not be found.u003Cu002Fcodeu003Eu003Cu002Fpu003Eu003Cpu003Eu003Ccodeu003EAction:u003Cu002Fcodeu003Eu003Ccodeu003EConsider defining a bean of type ‘com.yibao.beta.biz.service.DemoService’ in your configuration.u003Cu002Fcodeu003Eu003Cu002Fpu003Eu003Cu002Fdivu003Eu003Cu002Fpreu003Eu003Cpu003E原因是找不到DemoService类,此时需要在BetaWebApplication入口类中增加包扫描,设置@SpringBootApplication注解中的scanBasePackages值为com.yibao.beta,最终如下所示u003Cu002Fpu003Eu003Cpreu003Eu003Cdivu003Eu003Cpu003Eu003Ccodeu003Epackage com.yibao.beta.web;u003Cu002Fcodeu003Eu003Cu002Fpu003Eu003Cpu003Eu003Ccodeu003E@SpringBootApplication(scanBasePackages = “com.yibao.beta”)u003Cu002Fcodeu003Eu003Ccodeu003E@MapperScan(“com.yibao.beta.dao.mapper”)u003Cu002Fcodeu003Eu003Ccodeu003Epublic class BetaWebApplication {u003Cu002Fcodeu003Eu003Cu002Fpu003Eu003Cpu003Eu003Ccodeu003Epublic static void main(String[] args) {u003Cu002Fcodeu003Eu003Ccodeu003E SpringApplication.run(BetaWebApplication.class, args);u003Cu002Fcodeu003Eu003Ccodeu003E }u003Cu002Fcodeu003Eu003Ccodeu003E}u003Cu002Fcodeu003Eu003Cu002Fpu003Eu003Cu002Fdivu003Eu003Cu002Fpreu003Eu003Cpu003E设置完后重新运行main方法,项目正常启动,访问http:u002Fu002Flocalhost:8080u002Fdemou002Ftest得到如下效果u003Cu002Fpu003Eu003Cimg src=”http:u002Fu002Fp1.pstatp.comu002Flargeu002Fpgc-imageu002FRdXNRZ23qOQyxD” img_width=”788″ img_height=”280″ alt=”Spring Boot + MyBatis 多模块项目搭建教程” inline=”0″u003Eu003Cpu003Eu003Cu002Fpu003Eu003Ch2 toutiao-origin=”h3″u003E6. 集成Mybatisu003Cu002Fh2u003Eu003Cpu003E父pom文件中声明mybatis-spring-boot-starter及lombok依赖u003Cu002Fpu003Eu003Cpreu003Eu003Cpu003Eu003Ccodeu003EdependencyManagement>u003Cu002Fcodeu003Eu003Ccodeu003E <dependencies>u003Cu002Fcodeu003Eu003Ccodeu003E <dependency>u003Cu002Fcodeu003Eu003Ccodeu003E <groupId>org.mybatis.spring.boot<u002FgroupId>u003Cu002Fcodeu003Eu003Ccodeu003E <artifactId>mybatis-spring-boot-starter<u002FartifactId>u003Cu002Fcodeu003Eu003Ccodeu003E <version>1.3.2<u002Fversion>u003Cu002Fcodeu003Eu003Ccodeu003E <u002Fdependency>u003Cu002Fcodeu003Eu003Ccodeu003E <dependency>u003Cu002Fcodeu003Eu003Ccodeu003E <groupId>org.projectlombok<u002FgroupId>u003Cu002Fcodeu003Eu003Ccodeu003E <artifactId>lombok<u002FartifactId>u003Cu002Fcodeu003Eu003Ccodeu003E <version>1.16.22<u002Fversion>u003Cu002Fcodeu003Eu003Ccodeu003E <u002Fdependency>u003Cu002Fcodeu003Eu003Ccodeu003E <u002Fdependencies>u003Cu002Fcodeu003Eu003Ccodeu003E<u002FdependencyManagement>u003Cu002Fcodeu003Eu003Cu002Fpu003Eu003Cu002Fpreu003Eu003Cpu003E在beta-dao层中的pom文件中添加上述依赖u003Cu002Fpu003Eu003Cpreu003Eu003Cpu003Eu003Ccodeu003E<dependencies>u003Cu002Fcodeu003Eu003Ccodeu003E <dependency>u003Cu002Fcodeu003Eu003Ccodeu003E <groupId>org.mybatis.spring.boot<u002FgroupId>u003Cu002Fcodeu003Eu003Ccodeu003E <artifactId>mybatis-spring-boot-starter<u002FartifactId>u003Cu002Fcodeu003Eu003Ccodeu003E <u002Fdependency>u003Cu002Fcodeu003Eu003Ccodeu003E <dependency>u003Cu002Fcodeu003Eu003Ccodeu003E <groupId>mysql<u002FgroupId>u003Cu002Fcodeu003Eu003Ccodeu003E <artifactId>mysql-connector-java<u002FartifactId>u003Cu002Fcodeu003Eu003Ccodeu003E <u002Fdependency>u003Cu002Fcodeu003Eu003Ccodeu003E <dependency>u003Cu002Fcodeu003Eu003Ccodeu003E <groupId>org.projectlombok<u002FgroupId>u003Cu002Fcodeu003Eu003Ccodeu003E <artifactId>lombok<u002FartifactId>u003Cu002Fcodeu003Eu003Ccodeu003E <u002Fdependency>u003Cu002Fcodeu003Eu003Ccodeu003E<u002Fdependencies>u003Cu002Fcodeu003Eu003Cu002Fpu003Eu003Cu002Fpreu003Eu003Cpu003E在beta-dao层创建com.yibao.beta.dao包,通过mybatis-genertaor工具生成dao层相关文件(DO、Mapper、xml),存放目录如下u003Cu002Fpu003Eu003Cimg src=”http:u002Fu002Fp9.pstatp.comu002Flargeu002Fpgc-imageu002FRdXNRZE8M6NEO0″ img_width=”664″ img_height=”718″ alt=”Spring Boot + MyBatis 多模块项目搭建教程” inline=”0″u003Eu003Cpu003Eapplicatio.properties文件添加jdbc及mybatis相应配置项u003Cu002Fpu003Eu003Cpreu003Eu003Cdivu003Eu003Cpu003Eu003Ccodeu003Espring.datasource.driverClassName = com.mysql.jdbc.Driveru003Cu002Fcodeu003Eu003Ccodeu003Espring.datasource.url = jdbc:mysql:u002Fu002F192.168.1.1u002Ftest?useUnicode=true&characterEncoding=utf-8u003Cu002Fcodeu003Eu003Ccodeu003Espring.datasource.username = testu003Cu002Fcodeu003Eu003Ccodeu003Espring.datasource.password = 123456u003Cu002Fcodeu003Eu003Cu002Fpu003Eu003Cpu003Eu003Ccodeu003Emybatis.mapper-locations = classpath:mybatisu002F*.xmlu003Cu002Fcodeu003Eu003Ccodeu003Emybatis.type-aliases-package = com.yibao.beta.dao.entityu003Cu002Fcodeu003Eu003Cu002Fpu003Eu003Cu002Fdivu003Eu003Cu002Fpreu003Eu003Cpu003EDemoService通过@Autowired注解注入UserMapper,修改DemoService的test方法使之调用UserMapper的selectByPrimaryKey方法,最终如下所示u003Cu002Fpu003Eu003Cpreu003Eu003Cdivu003Eu003Cpu003Eu003Ccodeu003Epackage com.yibao.beta.biz.service.impl;u003Cu002Fcodeu003Eu003Cu002Fpu003Eu003Cpu003Eu003Ccodeu003E@Serviceu003Cu002Fcodeu003Eu003Ccodeu003Epublic class DemoServiceImpl implements DemoService {u003Cu002Fcodeu003Eu003Cu002Fpu003Eu003Cpu003Eu003Ccodeu003E@Autowiredu003Cu002Fcodeu003Eu003Ccodeu003E private UserMapper userMapper;u003Cu002Fcodeu003Eu003Cu002Fpu003Eu003Cpu003Eu003Ccodeu003E@Overrideu003Cu002Fcodeu003Eu003Ccodeu003E public String test {u003Cu002Fcodeu003Eu003Ccodeu003E UserDO user = userMapper.selectByPrimaryKey(1);u003Cu002Fcodeu003Eu003Ccodeu003E return user.toString;u003Cu002Fcodeu003Eu003Ccodeu003E }u003Cu002Fcodeu003Eu003Ccodeu003E}u003Cu002Fcodeu003Eu003Cu002Fpu003Eu003Cu002Fdivu003Eu003Cu002Fpreu003Eu003Cpu003E再次运行BetaWebApplication类中的main方法启动项目,发现如下报错u003Cu002Fpu003Eu003Cpreu003Eu003Cdivu003Eu003Cpu003Eu003Ccodeu003EAPPLICATION FAILED TO STARTu003Cu002Fcodeu003Eu003Ccodeu003E***************************u003Cu002Fcodeu003Eu003Cu002Fpu003Eu003Cpu003Eu003Ccodeu003EDescription:u003Cu002Fcodeu003Eu003Ccodeu003EField 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.u003Cu002Fcodeu003Eu003Cu002Fpu003Eu003Cpu003Eu003Ccodeu003EAction:u003Cu002Fcodeu003Eu003Ccodeu003EConsider defining a bean of type ‘com.yibao.beta.dao.mapper.UserMapper’ in your configuration.u003Cu002Fcodeu003Eu003Cu002Fpu003Eu003Cu002Fdivu003Eu003Cu002Fpreu003Eu003Cpu003E原因是找不到UserMapper类,此时需要在BetaWebApplication入口类中增加dao层包扫描,添加@MapperScan注解并设置其值为com.yibao.beta.dao.mapper,最终如下所示u003Cu002Fpu003Eu003Cpreu003Eu003Cdivu003Eu003Cpu003Eu003Ccodeu003Epackage com.yibao.beta.web;u003Cu002Fcodeu003Eu003Cu002Fpu003Eu003Cpu003Eu003Ccodeu003E@SpringBootApplication(scanBasePackages = “com.yibao.beta”)u003Cu002Fcodeu003Eu003Ccodeu003E@MapperScan(“com.yibao.beta.dao.mapper”)u003Cu002Fcodeu003Eu003Ccodeu003Epublic class BetaWebApplication {u003Cu002Fcodeu003Eu003Cu002Fpu003Eu003Cpu003Eu003Ccodeu003Epublic static void main(String[] args) {u003Cu002Fcodeu003Eu003Ccodeu003E SpringApplication.run(BetaWebApplication.class, args);u003Cu002Fcodeu003Eu003Ccodeu003E }u003Cu002Fcodeu003Eu003Ccodeu003E}u003Cu002Fcodeu003Eu003Cu002Fpu003Eu003Cu002Fdivu003Eu003Cu002Fpreu003Eu003Cpu003E设置完后重新运行main方法,项目正常启动,访问http:u002Fu002Flocalhost:8080u002Fdemou002Ftest得到如下效果u003Cu002Fpu003Eu003Cimg src=”http:u002Fu002Fp1.pstatp.comu002Flargeu002Fpgc-imageu002FRdXNRzjAL4Bofe” img_width=”790″ img_height=”258″ alt=”Spring Boot + MyBatis 多模块项目搭建教程” inline=”0″u003Eu003Cpu003E至此,一个简单的SpringBoot+Mybatis多模块项目已经搭建完毕,我们也通过启动项目调用接口验证其正确性。u003Cu002Fpu003Eu003Cpu003Eu003Cu002Fpu003Eu003Ch1 toutiao-origin=”h2″u003E四、总结u003Cu002Fh1u003Eu003Cpu003E一个层次分明的多模块工程结构不仅方便维护,而且有利于后续微服务化。在此结构的基础上还可以扩展common层(公共组件)、server层(如dubbo对外提供的服务)u003Cemu003Eu003Ci class=”chrome-extension-mutihighlight chrome-extension-mutihighlight-style-1″u003E微信u003Cu002Fiu003E搜索 web_resource 获取更多推送u003Cu002Femu003Eu003Cu002Fpu003Eu003Cpu003E此为项目重构的第一步,后续还会的框架中集成logback、disconf、redis、dubbo等组件u003Cu002Fpu003Eu003Cpu003Eu003Cu002Fpu003Eu003Ch1 toutiao-origin=”h2″u003E五、未提到的坑u003Cu002Fh1u003Eu003Cpu003E在搭建过程中还遇到一个maven私服的问题,原因是公司内部的maven私服配置的中央仓库为阿里的远程仓库,它与maven自带的远程仓库相比有些jar包版本并不全,导致在搭建过程中好几次因为没拉到相应jar包导致项目启动不了。u003Cu002Fpu003Eu003Cpu003E-END-u003Cu002Fpu003Eu003Cpu003E如果看到这里,说明你喜欢这篇文章,请u003Cstrong toutiao-origin=”span” class=”highlight-text”u003E转发u003Cu002Fstrongu003Eu003Cstrong toutiao-origin=”span” class=”highlight-text”u003E、点赞u003Cu002Fstrongu003E。u003Ci class=”chrome-extension-mutihighlight chrome-extension-mutihighlight-style-1″u003E微信u003Cu002Fiu003E搜索「web_resource」,u003Ci class=”chrome-extension-mutihighlight chrome-extension-mutihighlight-style-1″u003E关注u003Cu002Fiu003E后回复「进群」或者扫描下方二维码即可进入无广告交流群。u003Cu002Fpu003E”

原文始发于:Spring Boot + MyBatis 多模块项目搭建教程

主题测试文章,只做测试使用。发布者:℅傍ㄖ免沦陷dε鬼,转转请注明出处:http://www.cxybcw.com/18012.html

联系我们

13687733322

在线咨询:点击这里给我发消息

邮件:1877088071@qq.com

工作时间:周一至周五,9:30-18:30,节假日休息

QR code