美文网首页
SpringBoot + mybatis + druid + s

SpringBoot + mybatis + druid + s

作者: 墨鬁 | 来源:发表于2020-06-07 17:26 被阅读0次

    依赖:

    <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid-spring-boot-starter</artifactId>
    </dependency>
    
    <dependency>
                <groupId>com.github.pagehelper</groupId>
                <artifactId>pagehelper-spring-boot-starter</artifactId>
    </dependency>
     <!-- mybatis -->
            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
            </dependency>
    
            <!--Mysql数据库驱动-->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>${mysql-connector.version}</version>
            </dependency>
    
    <dependency>
                <groupId>org.apache.shardingsphere</groupId>
                <artifactId>sharding-jdbc-spring-boot-starter</artifactId>
    </dependency>
    

    主配置文件

    server:
      port: 8092
    
    spring:
      main:
        # 允许Bean定义被后定义的Bean覆盖
        allow-bean-definition-overriding: true
      application:
        name: csip-admin
      profiles:
        include: sharding-master-slave
    
    mybatis:
      mapper-locations:
      - classpath:dao/*.xml
      - classpath*:com/**/mapper/*.xml
    

    sharding-jdbc 读写分离的配置文件

    #shardingsphere 读写分离
    spring:
      shardingsphere:
        props:
          sql:
            show: true
        datasource:
          names: master,slave0
          master:
            type: com.alibaba.druid.pool.DruidDataSource
            driver-class-name: com.mysql.jdbc.Driver
            url: jdbc:mysql://localhost/master?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=CONVERT_TO_NULL
            username: root
            password: root
    
          slave0:
            type: com.alibaba.druid.pool.DruidDataSource
            driver-class-name: com.mysql.jdbc.Driver
            url: jdbc:mysql://localhost/slave?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=CONVERT_TO_NULL
            username: root
            password: root
    
    
        masterslave:
          load-balance-algorithm-type: round_robin
          name: ms
          master-data-source-name: master
          slave-data-source-names: slave0
    

    以上配置,启动一直报错,提示

    ***************************
    APPLICATION FAILED TO START
    ***************************
    
    Description:
    
    Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
    

    但是当吧 数据源类型 从 druid 改成默认的 HikariDataSource 之后, 又可以正常启动
    也实现了 读写分离的效果, 不知道哪里错了,尝试了很多修改,最终都是不行,
    druid 这个数据库连接池,不知道是哪里的问题....

    问题解决了:

    原来是因为我使用了 druid-spring-boot-starter 内置了自动装配,
    把依赖换成原生的就可以解决了
    <dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.1.10</version>
    </dependency>

    配之后,启动,可以看到这里注入的 DataSource 已经是 Druid了
    org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration
    这个类的 sqlSessionFactory 方法 bean定义

    image.png

    相关文章

      网友评论

          本文标题:SpringBoot + mybatis + druid + s

          本文链接:https://www.haomeiwen.com/subject/jsgctktx.html