整合SpringBoot+MyBatis
Posted shisandao
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了整合SpringBoot+MyBatis相关的知识,希望对你有一定的参考价值。
1、新建一个SpringBoot项目
2、添加基本依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.2</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency>
3、完善默认端口设置、mybatis配置、数据源配置
server: port: 8066 # mybatis配置 mybatis: #mapper文件路径(**表示匹配多个) mapperLocations: classpath:com/ssd/gem/**/mappers/*.xml #typeAlias包路径 type-alias-package: spring: # 数据源 datasource: url: jdbc:mysql://localhost:3306/gem?serverTimezone=UTC&useSSL=false username: root password: 123456 driver-class-name: com.mysql.cj.jdbc.Driver
4、使用mybatis generator生成pojo、mapper接口、mapper.xml。可参考https://www.cnblogs.com/shisandao/p/12524249.html
5、service接口
package com.ssd.gem.service; import com.ssd.gem.mbg.model.GemOrder; public interface IGemOrderService { void insert(GemOrder gemOrder); }
6、service实现
package com.ssd.gem.service.impl; import com.ssd.gem.mbg.mapper.GemOrderMapper; import com.ssd.gem.mbg.model.GemOrder; import com.ssd.gem.service.IGemOrderService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class GemOrderServiceImpl implements IGemOrderService { @Autowired private GemOrderMapper gemOrderMapper; @Override public void insert(GemOrder gemOrder) { gemOrderMapper.insert(gemOrder); return; } }
7、controller控制器
package com.ssd.gem.controller; import com.ssd.gem.mbg.model.GemOrder; import com.ssd.gem.service.IGemOrderService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class GemOrderController { @Autowired private IGemOrderService iGemOrderService; @RequestMapping("/order/insert") void insert(@RequestBody GemOrder gemOrder) { iGemOrderService.insert(gemOrder); } }
8、设置bean的扫描路径:@SpringBootApplication(scanBasePackages={"com.ssd.gem.*"})
package com.ssd.gem; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication(scanBasePackages={"com.ssd.gem.*"}) @MapperScan("com.ssd.gem.**.mapper") public class GemApplication { public static void main(String[] args) { SpringApplication.run(GemApplication.class, args); } }
SpringBootApplication启动时会默认扫描主类当前包及子包的所有类,寻找标注了@Component,@Service,@Controller,@Repository注解的类,并把这些类纳入进spring容器中管理。设置扫描路径可以缩小扫描范围。
9、设置mapper接口的扫描路径:@MapperScan("com.ssd.gem.**.mapper")
作用:指定要变成实现类的接口所在的包,然后包下面的所有接口在编译之后都会生成相应的实现类。
注解多个包:@MapperScan("A","B")。
该注释可以使用注释 @Mapper 代替,需要在每个mapper接口类上加该注解,比较繁琐。
10、测试:http://localhost:8066/order/insert(注意参数格式)
以上是关于整合SpringBoot+MyBatis的主要内容,如果未能解决你的问题,请参考以下文章