springboot 显式配置数据源 解决mybatis注解无法被识别问题

摘要:
1一般来说,只要将数据库配置添加到yml,然后pom添加相关的依赖项,springboot就可以识别带有mapper注释的mybatis接口。但是,如果在配置之后无法创建映射器注释,或者SqlSessionFactory没有创建bean,则应显式配置bean。该方法引用了springboot创建多个数据源以创建配置类的方法。@Configuration@MapperScan(基本Pac

1正常来讲,只要在yml 加上数据库配置,然后pom添加相关依赖,带有mapper注解的mybatis接口是可以被springboot识别的,但是如果配置之后出现mapper注解无法被创建bean,或者SqlSessionFactory没有创建的错误,就要显式配置bean ,本做法参照springboot创建多数据源方法 

创建一个配置类

@Configuration
@MapperScan(basePackages = {"com.xxxx.xxxx.xxxxx.mapper"}, sqlSessionFactoryRef = "sqlsession1")
public class DataSConfig {
    @Bean(name = "test1DataSource")//注入到这个容器
    @ConfigurationProperties(prefix = "spring.datasource")//表示取application.properties配置文件中的前缀
    @Primary    //primary是设置优先,因为有多个数据源,在没有明确指定用哪个的情况下,会用带有primary的,这个注解必须有一个数据源要添加

    public DataSource testDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "sqlsession1")
    @Primary
    //@Qualifier("xxx")的含义是告诉他使用哪个DataSource
    public SqlSessionFactory testSqlSessionFactory(@Qualifier("test1DataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        return bean.getObject();
    }

    @Bean(name = "test1TransactionManager")//配置事务
    @Primary
    public DataSourceTransactionManager testTransactionManager(@Qualifier("test1DataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "test1SqlSessionTemplate")
    @Primary
    public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("sqlsession1") SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

}

代码来自https://blog.csdn.net/qq_41076797/article/details/82889770

然后配置yml

spring:
  datasource:
    username: root
    password: xxxx
    jdbc-url: jdbc:mysql://xxxxx
    driver-class-name: com.mysql.cj.jdbc.Driver
#不同的springboot版本有差异,如果出错就网上搜一下

不同的数据源使用不同的mapperscan

附赠多数据源使用

封装jar的简易方法http://www.360doc.com/content/20/0129/15/13328254_888499128.shtml

jdbctemplate多数据源  https://blog.didispace.com/springbootmultidatasource/

配置连接池 https://blog.51cto.com/zero01/2161509

免责声明:文章转载自《springboot 显式配置数据源 解决mybatis注解无法被识别问题》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇linux应用层监控网线插拔状态的实现VC6.0 error LNK2001: unresolved external symbol _main解决办法下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

基于java容器注解---基于泛型的自动装配

上面利用@Configuration和@Bean配置bean,中间利用@Autowired,指定s1和s2分别指向哪个实现类,下面利用@Autowired,指定s中只有Integer实现类 例子: 在接口中指定T的泛型: 在实现类中指定确定的类型: config的class类中利用@Bean配置bean: 测试类:...

OC文件操作(2)

NSFileManager 文件管理器完成文件的创建、移动、拷贝等管理操作 1.查询文件和目录  OC中查询路径下的目录主要分为浅度遍历和深度遍历。  浅度遍历  NSFileManager * manager = [NSFileManager defaultManager];//创建文件管理器,+方法  NSError * error = nil; //...

Java之Spring 注解

概述 注释配置相对于 XML 配置具有很多的优势: 它可以充分利用 Java 的反射机制获取类结构信息,这些信息可以有效减少配置的工作。如使用 JPA 注释配置 ORM 映射时,我们就不需要指定 PO 的属性名、类型等信息,如果关系表字段和 PO 属性名、类型都一致,您甚至无需编写任务属性映射信息——因为这些信息都可以通过 Java 反射机制获取。 注释...

springboot配置文件详解

https://blog.csdn.net/myvanguard/article/details/84844583 简介 springboot的默认配置文件 application.propertiesapplication.ymlyaml是标记语言,是以数据为中心的的配置文件 server: port:8081 12342.yaml语法1.基本语法k:(...

大漠插件

一.注册大漠插件 下载:Click 两种方式 1.Cmd手动注册dll regsvr32 D:Project大漠DMTestdm.dll 2.程序注册dll static string AutoRegCom() { string strCmd = @"regsvr32 D:Project大漠DMTes...

IDEA 版权设置

@Copyright (c) $today.year. 于吉利 All Rights Reserved @File:$file.fileName @Author: 于吉利 @Function: @Date: $today @version:1.0 @Changes: @Description:...