springBoot mybatis 包扫描实例

springBoot mybatis 包扫描

@MapperScan(basePackages = {"com.zscat.*.dao","com.zscat.*.*.dao"})
@EnableTransactionManagement(proxyTargetClass = true)
@SpringBootApplication
@MapperScan(basePackages = {"com.zscat.*.dao","com.zscat.*.*.dao"})
public class   ShopServiceApplication {
    public static void main(String[] args) {
        SpringApplication application = new SpringApplication(ShopServiceApplication.class);
        application.run(args);
    }
}

springBoot 扫描不到 mybatis 接口包

只需要在spring boot启动类上加上注解,并指定jar包中接口文件包路径即可

@MapperScan(basePackages = "com.xx.**.dao")

如果使用@Controller和@EnableAutoConfiguration 注解还应该再加上一个注解:@ComponentScan 就可以了。

@Controller和@EnableAutoConfiguration没有扫描注解的功能,而@ComponentScan是

springboot专门用来扫描@Component, @Service, @Repository, @Controller等注解的注解

总结:

使用springboot启动类配置扫描的两种注解配置方式:

1、@Controller

   @EnableAutoConfiguration
   @ComponentScan

2、@SpringBootApplication

@SpringBootApplication注解等价于@Configuration, @EnableAutoConfiguration and @ComponentScan

另外application.java(启动类)也应该按照官方的建议放在root目录下,这样才能扫描到Service和dao,不然还会引起,扫描不到注解的问题。

--- 更新日期:2018-10-14 ---

最近用了最新的springboot 2.0.5.RELEASE 版本 多了一种新的扫描注解,新版的springboot application可以放在任意位置,只要加上

@ComponentScan(basePackages = {"com.oskyhang", "com.frames"})

注解就可以,注解指定扫描的包,就可以扫描到,更灵活方便。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持我们。

(0)

相关推荐

  • SpringBoot整合Mybatis无法扫描xml文件的解决

    网上说是使用idea在SpringBoot整合Mybatis时候会扫描不到xml文件 1.将xml文件放在resources下 2.在application.properties中配置xml文件的扫面 补充知识:Springboot整合mybatis /*.xml路径URl does not exist问题 解决一: 在配置文件下 扫描不到 xml文件: 原来的文件: <bean id="sqlSessionFactory" class="org.mybatis.spr

  • 解决SpringBoot整合Mybatis扫描不到Mapper的问题

    闲来无事,想学学springboot,开始搭建一个项目,但是一直显示mapper扫描不到的错误: "Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userController': Unsa

  • springboot多模块包扫描问题的解决方法

    问题描述: springboot建立多个模块,当一个模块需要使用另一个模块的服务时,需要注入另一个模块的组件,如下面图中例子: memberservice模块中的MemberServiceApiImpl类需要注入common模块中的RedisService组件,该怎么注入呢? 解决: 在memberservice模块的启动类上加上RedisService类所在包的全路径的组件扫描,就像这样: 注意启动类上方的注解@ComponentScan(basePackages={"com.whu.comm

  • 深入理解SpringBoot中关于Mybatis使用的三个问题

    原本是要讲讲PostgreSQL的一些学习总结的,不巧的是最近一段时间的进度都是一些类似于加减乘除.位移.类型转换的稍显小儿科的一些内容,额~(ಠ .̫.̫ ಠ),这也不是什么问题,只是觉得这中间没什么终点和难点可讲的,也就暂时略过了~,这里首先说声抱歉啊,后续如有什么使用难点或有趣的地方一定拿出来讲讲♥◠‿◠)ノ:额,每次开篇总要讲一堆看似没啥用的内容,有啥用,有啥用,

  • springBoot mybatis 包扫描实例

    springBoot mybatis 包扫描 @MapperScan(basePackages = {"com.zscat.*.dao","com.zscat.*.*.dao"}) @EnableTransactionManagement(proxyTargetClass = true) @SpringBootApplication @MapperScan(basePackages = {"com.zscat.*.dao","com.z

  • SpringBoot默认包扫描机制及@ComponentScan指定扫描路径详解

    目录 SpringBoot默认包扫描机制 @ComponentScan的使用 常用参数含义 @Component与@ComponentScan SpringBoot默认包扫描机制 标注了@Component和@Component的衍生注解如@Controller,@Service,@Repository就可以把当前的Bean加入到IOC容器中.那么SpringBoot是如何知道要去扫描@Component注解的呢.@ComponentScan做的事情就是告诉Spring从哪里找到bean Spr

  • Linux编辑启动、停止与重启springboot jar包脚本实例

    前言 springboot的配置文件中,配置文件的名字都有各自的意义跟用途 dev 开发环境 prod 生产环境(默认) test 测试环境 加载指定配置文件 --spring.profiles.active=prod springboot加载jar包的方式有 // 直接在控制台进行启动,缺点就是控制台关闭项目也就关闭了. java -jar bootdo.jar // 这种方式可以运行在后台,但是如果推出了shell的话,那也会挂 java -jar /bootdo-2.0.0.jar > b

  • SpringBoot+MyBatis简单数据访问应用的实例代码

    因为实习用的是MyBatis框架,所以写一篇关于SpringBoot整合MyBatis框架的总结. 一,Pom文件 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:

  • springboot 2.0 mybatis mapper-locations扫描多个路径的实现

    springboot 2.0 mybatis mapper-locations扫描多个路径 mapper-locations扫描多个路径,中间以,分开, 如果mapper.xml在源码包下,配置成classpath*开头比较好使 mybatis: mapper-locations: classpath*:mapper/*.xml,classpath*:com/urthink/upfs/**/*Mapper.xml type-aliases-package: com.urthink.upfs.sp

  • SpringBoot整合MyBatis逆向工程及 MyBatis通用Mapper实例详解

    一.添加所需依赖,当前完整的pom文件如下: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&q

  • Springboot+Mybatis中typeAliasesPackage正则扫描实现方式

    Mybatis typeAliasesPackage正则扫描 mybatis默认配置typeAliasesPackage是不支持正则扫描package的,因此需要手动继承org.mybatis.spring.SqlSessionFactoryBean,自己实现正则扫描,方法和传统的spring+mybatis没什么区别,不同的是一个需要继承类一个是使用的扫描实现. 对于两个或多个扫描路径,例: cn.com.onethird.integration.entity cn.com.onethird.

  • SpringBoot+mybatis实现多数据源支持操作

    什么是多数据源支持? 简单的说,就是一个项目里,同时可以访问多个不同的数据库. 实现原理 单个数据源在配置时会绑定一套mybatis配置,多个数据源时,不同的数据源绑定不同的mybatis配置就可以了,简单的思路就是让不同的数据源扫描不同的包,让不同的包下的mapper对应连接不同的数据源去处理逻辑. 业务场景假设 项目底层有正常业务库和日志库,希望解决的是将项目中的一些日志单独记录到一个库里,比如用户操作记录.产品更新记录等. 说一下为什么会有这个需求:用户操作记录和产品更新记录可能很多,而实

随机推荐