【SpringBoot学习】7、SpringBoot 整合 Mybatis 的方法,以及常见错误的处理 解决方案

一、springboot 整合 mybatis 的方法

和 ssm 一样的老规矩,需要使用@service,@Mapper,@Controller 等注解,但是具有一定的使用区别,网上很多都是一个层(service,serviceImpl,dao)针对写一个注解的方式,假如有很多个实体以及对应的各个层的方法,那么书写起来很不方便,所以下面做了基于 springboot 的统一的处理方法。
我写的是一个分模块的 springboot 的案例,单个项目的使用方法也是一样的。

整合所需 jia 包

<dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>1.1.1</version>
    </dependency>
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis-spring</artifactId>
        <version>1.3.2</version>
    </dependency>

1、逆向工程自动生成

使用 mybatis 逆向工程生成 mapper 层,bean 层,mapper.xml 层,不会使用的话,可以查看我的另一篇文章:springboot 整合 mybatis 逆向工程 解决方案。

https://blog.csdn.net/qq_38762237/article/details/81101205

2、添加各个层之间的注解

A、springboot 启动类添加 mapper 层扫描

@SpringBootApplication
@MapperScan("com.orangesay.mapper")
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

B、serviceImpl 层添加 service 注解

@Service
public class UserServiceImpl implements UserService {

C、mapper 层添加 mapper 注解

@Mapper
public interface UserMapper {

D、application.properties 配置文件加两句话,第一句是扫描所有类路径下的 Mapper.xml 层,第二个是扫描所有的 bean 实体类

# mybatis
mybatis.mapper-locations=classpath*:mapper/*.xml
mybatis.type-aliases-package=com.orangesay.bean

整合到此结束!

二、常见错误的处理

错误一:

这里写图片描述

错误原因:jar 包引入缺失,导致扫描不到层的注解,上图是错误信息。
解决方案:添加 springboot 整合 mybatis 的 jar 包

<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>1.1.1</version>
</dependency>

微信公众号

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Tellsea

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值