Spring Boot + MyBatis 多模块项目搭建教程
作者 | 枫本非凡
链接 | cnblogs.com/orzlin/p/9717399.html
一、前言
1、开发工具及系统环境
IDE:IntelliJ IDEA 2018.2
系统环境:mac OSX
2、项目目录结构
biz层:业务逻辑层
dao层:数据持久层
web层:请求处理层
二、搭建步骤
1、创建父工程
2、创建子模块
@SpringBootApplication
public 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();
}
@Service
public 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.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
package com.yibao.beta.biz.service.impl;
@Service
public 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);
}
}
四、总结
五、未提到的坑
程序汪资料链接
卧槽!字节跳动《算法中文手册》火了,完整版 PDF 开放下载!
卧槽!阿里大佬总结的《图解Java》火了,完整版PDF开放下载!
欢迎添加程序汪个人微信 itwang009 进粉丝群或围观朋友圈
评论