SpringBoot的MyBatis注解:@MapperScan和@Mapper(一)

前言

1、目的

  • Spring-boot项目使用MapperScan注解。

2、搜索关键词

  • springboot整合mybatis

3、参看文章

  • SpringBoot整合Mybatis完整详细版:https://blog.csdn.net/iku5200/article/details/82856621

这篇文章整理的比较详尽。

一、简述

1、Spring Boot与MyBatis融合的矛盾问题:

Spring家族的使命就是为了简化而生,但是随着Spring的发展壮大,有点事与愿违了。为了坚持初心,Spring家族祭出了一大杀器---Spring Boot。Spring Boot的核心理念是:不建议使用xml文件配置。但是,这对MyBatis来说进退两难,因为MyBatis离不开xml,需要xml来配置sql语句。为了迎合Spring Boot的发展理念,MyBatis官方开发了mybatis-spring-boot-starter,我们要想更少的依赖xml,需要深入的研究mybatis-spring-boot-starter中的用法,尤其是@MapperScan和@Mapper的用法。

2、@MapperScan和@Mapper简介:

在不使用@MapperScan前,我们需要直接在Mapper类上面添加注解@Mapper,这种方式要求每一个Mapper类都需要添加此注解,非常麻烦,属于重复劳动。通过使用@MapperScan注解,可以让我们不用为每个Mapper类都添加@Mapper注解。

二、使用

2.1、@Mapper注解的使用

作用:在接口类上添加了@Mapper,在编译之后会生成相应的接口实现类
添加位置:接口类上面,代码如下所示:

  • @Mapper
    public interface StudentMapper {
       //todo
    }

2.2、@MapperScan注解的使用

作用:指定要变成实现类的接口所在的包,包下面的所有接口在编译之后都会生成相应的实现类
添加位置:是在Springboot启动类上面添加,

  • @SpringBootApplication
    @MapperScan("cn.mybatis.mappers")
    public class SpringbootMybatisDemoApplication {
        public static void main(String[] args) {
            SpringApplication.run(SpringbootMybatisDemoApplication.class, args);
        }
    }

添加@MapperScan("cn.mybatis.mappers")注解以后,cn.mybatis.mappers包下面的接口类,在编译之后都会生成相应的实现类

另外,使用@MapperScan注解可以作用到多个包,代码如下所示:

  • @SpringBootApplication  
    @MapperScan({"cn.mybatis.mappers.class","cn.mybatis.mappers.student"})  
    public class SpringbootMybatisDemoApplication{  
        public static void main(String[] args) {  
           SpringApplication.run(SpringbootMybatisDemoApplication.class, args);  
        }  
    } 

2.3、使用@MapperScan注解注意事项

Spring Boot不建议使用XML文件配置,MyBatis则有点犯难了,官方推荐使用mybatis-spring-boot-starter与Spring Boot整合。

MyBatis官方建议:直接在Mapper类中采用注解的形式操作数据库,通过@MapperScan扫描制定的映射器存放路径,最终不需要加任何注解,也不需要对应的xml文件来配置sql语句。代码如下形式:

//不需要加任何注解,也不需要对应的xml文件
public interface UserMapper{
@Select("select * from user")
List<User> getUserList(String userId);
}

 参看链接:http://www.mybatis.cn/archives/862.html

三、常见问题

3.1添加MapperScan后需要引入mybatis依赖

 <!--     springboot mybatis 整合包   -->
 <dependency>
     <groupId>org.mybatis.spring.boot</groupId>
     <artifactId>mybatis-spring-boot-starter</artifactId>
     <version>2.1.3</version>
 </dependency>
 <!--    mysql 驱动    -->
 <dependency>
     <groupId>mysql</groupId>
     <artifactId>mysql-connector-java</artifactId>
     <scope>runtime</scope>
 </dependency>

具体项目代码参看链接:https://blog.csdn.net/weixin_39843093/article/details/110509137

3.2@MapperScan还可以写xml文件吗

一般直接通过注解的形式,在mapper接口中写sql。

可以,下面这种方式没有试过,但是下面的博文里有例子。

yml里的mybatis注解就是告诉spring要到那里去找到xml文件

1 mybatis:
2   mapper-locations: classpath*:com/fengzi/bms/mapper/*.xml
总结就是,@Mapper或@MapperScan找到接口,yml或properties配置文件中的mybatis:找到xml

参看链接:https://blog.csdn.net/iku5200/article/details/82856621

3.3 配置文件配置

org.mybatis.spring.boot依赖如何配置文件,org.mybatis.spring.boot依赖如何配置数据源

直接在yml文件进行配置就行了。

Spring-boot整合mybatis需要的依赖及引入依赖报错。应该是用那个长的,但是f-test项目下载不下来jar包,应该是跟spring-boot的版本有冲突。
而且使用时,如果用到xml,需要注意xml的目录结果与mapper层对应,在yml文件中配置好位置。
现在用的是@Mapper注解,依赖用的短的那个,不报错。
长的应该也能用@Mapper注解。

参看链接: 

https://www.cnblogs.com/zhuxiang1633/p/11685457.html

https://www.cnblogs.com/gavincoder/p/10105762.html

posted @ 2020-10-17 10:37  慎终若始  阅读(268)  评论(0编辑  收藏  举报