蕃薯耀 阅读(87) 评论(0)

sharding-jdbc-spring-boot-starter 整合 mybatis-plus-boot-starter + druid

sharding-jdbc整合 mybatis-plus + druid

shardingsphere分库分表

 

 

================================

©Copyright 蕃薯耀 2020-10-21

 

一、环境:

 

复制代码
<parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>2.2.10.RELEASE</version>
  <relativePath/>
</parent>
复制代码
<mybatisPlus.version>3.4.0</mybatisPlus.version>
<shardingsphere.version>4.1.1</shardingsphere.version>
复制代码
<!-- 不能使用druid-spring-boot-starter,会导致:Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required -->
        <!-- 
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.2.1</version>
        </dependency>
         -->
        
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.2.1</version>
        </dependency>
        
        <dependency>
            <groupId>org.apache.shardingsphere</groupId>
            <artifactId>sharding-jdbc-spring-boot-starter</artifactId>
            <version>${shardingsphere.version}</version>
        </dependency>
        
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.2</version>
        </dependency>
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>
        </dependency>
        
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.2.13</version>
        </dependency>
        
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>${mybatisPlus.version}</version>
        </dependency>
        
        <!--mybatis-plus 码生成器 添加 模板引擎依赖 这个需要增加模板引擎依赖,如freemarker-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-generator</artifactId>
            <version>${mybatisPlus.version}</version>
        </dependency>
        
        
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <scope>provided</scope>
        </dependency>
复制代码

 

数据源连接配置:

复制代码

#com.alibaba.druid.pool.DruidDataSource
#DruidDataSource需要引入druid的Jar包,使用:url
#com.zaxxer.hikari.HikariDataSource
#HikariDataSource要使用:jdbc-url
spring.shardingsphere.datasource.ss1.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.ss1.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.ss1.url=jdbc:mysql://localhost:3306/ss1?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8&pinGlobalTxToPhysicalConnection=true&autoReconnect=true&nullNamePatternMatchesAll=true
spring.shardingsphere.datasource.ss1.username=root
spring.shardingsphere.datasource.ss1.password=root

复制代码

 

 

注意:

不能使用:druid-spring-boot-starter,不然会导致下面的问题:java.lang.IllegalArgumentException: Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required

复制代码
Caused by: java.lang.IllegalArgumentException: Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required
    at org.springframework.util.Assert.notNull(Assert.java:201)
    at org.mybatis.spring.support.SqlSessionDaoSupport.checkDaoConfig(SqlSessionDaoSupport.java:122)
    at org.mybatis.spring.mapper.MapperFactoryBean.checkDaoConfig(MapperFactoryBean.java:73)
    at org.springframework.dao.support.DaoSupport.afterPropertiesSet(DaoSupport.java:44)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1853)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1790)
    ... 47 more
复制代码

 

 

 

================================

©Copyright 蕃薯耀 2020-10-21