【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.classargs);
    }
}

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

整合到此结束!

二、常见错误的处理

错误一:

【SpringBoot学习】7、SpringBoot 整合 Mybatis 的方法,以及常见错误的处理 解决方案
这里写图片描述

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

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

微信公众号


原文始发于微信公众号(花海里):【SpringBoot学习】7、SpringBoot 整合 Mybatis 的方法,以及常见错误的处理 解决方案

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/69645.html

(0)
小半的头像小半

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!