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.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>
<dependencies>
<dependency>
<groupId>com.yibao.beta</groupId>
<artifactId>beta-biz</artifactId>
</dependency>
</dependencies>
<dependencies>
<dependency>
<groupId>com.yibao.beta</groupId>
<artifactId>beta-dao</artifactId>
</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.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>
<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>
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);
}
}
四、总结
五、未提到的坑
怎么接私活?这个渠道你100%有用!请收藏!
喜欢文章,点个在看
评论