Spring Boot + MyBatis 多模塊項目搭建教程
作者 | 楓本非凡
鏈接 | cnblogs.com/orzlin/p/9717399.html
一、前言
1、開發(fā)工具及系統(tǒng)環(huán)境
IDE:IntelliJ IDEA 2018.2
系統(tǒng)環(huán)境:mac OSX
2、項目目錄結構
biz層:業(yè)務邏輯層
dao層:數(shù)據(jù)持久層
web層:請求處理層
二、搭建步驟
1、創(chuàng)建父工程







2、創(chuàng)建子模塊





@SpringBootApplicationpublic class BetaWebApplication {public static void main(String[] args) {SpringApplication.run(BetaWebApplication.class, args);}}
@RestController@RequestMapping()public class DemoController {public String test() {return "Hello World!";}}

4、配置模塊間的依賴關系
<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>
<dependencies><dependency><groupId>com.yibao.betagroupId><artifactId>beta-bizartifactId>dependency>dependencies>
<dependencies><dependency><groupId>com.yibao.betagroupId><artifactId>beta-daoartifactId>dependency>dependencies>
public?interface?DemoService?{String test();}
@Servicepublic class DemoServiceImpl implements DemoService {public String test() {return "test";}}
package com.yibao.beta.web.controller;@RequestMapping()public class DemoController {private DemoService demoService;public String test() {return demoService.test();}}
***************************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.
package com.yibao.beta.web;@SpringBootApplication(scanBasePackages = )@MapperScan()public class BetaWebApplication {public static void main(String[] args) {SpringApplication.run(BetaWebApplication.class, args);}}

6. 集成Mybatis
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>
<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>

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 = 123456mybatis.mapper-locations = classpath:mybatis/*.xmlmybatis.type-aliases-package = com.yibao.beta.dao.entity
package com.yibao.beta.biz.service.impl;@Servicepublic class DemoServiceImpl implements DemoService {private UserMapper userMapper;public String test() {UserDO user = userMapper.selectByPrimaryKey(1);return user.toString();}}
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.
package com.yibao.beta.web;@SpringBootApplication(scanBasePackages = )@MapperScan()public class BetaWebApplication {public static void main(String[] args) {SpringApplication.run(BetaWebApplication.class, args);}}

四、總結
五、未提到的坑
喜歡就三連呀
關注 Stephen,一起學習,一起成長。
評論
圖片
表情
