shardingsphere+druid+mybatis环境搭建遇到的问题

844 阅读2分钟

1.表库路由改写的位置

image.png

根据搭建的环境来看,执行顺序 mybatis->ShardingSphere-JDBC->druid
由此可以知道只需要配置正确ShardingSphere的参数就可以进行分库分表的一些操作。

2.数据库配置druid需要注意的地方

# 数据库连接池类修改
原有:type: com.zaxxer.hikari.HikariDataSource
调整  type: com.alibaba.druid.pool.DruidDataSource

错误信息: Caused by: com.mysql.cj.exceptions.WrongArgumentException: The database URL cannot be null.

原有 jdbc-url:
调整 url:  


错误信息: Consider defining a bean named 'entityManagerFactory' in your configuration.
包引入不用starter的,需要直接引用的
<!--
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid-spring-boot-starter</artifactId>
			<version>1.1.17</version>
		</dependency>
-->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid</artifactId>
			<version>1.2.15</version>
		</dependency>

3.mybatis-plus注意点

错误信息:org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): OrderMapper.selectById

需要指定@TableId("order_id")
还需要注意的是注意大小写和配置策略保持一致

用一些默认方法的表名转换需要 @TableName(value = "t_order") 
hibernate写习惯了用的是@Table注解

4.分片规则

对于一些复杂的分片策略,可以通过代码指定实现

这里需要思考的是分库、分表应该如何处理;做测试一般是取模了

  • 标准分片策略 (PreciseShardingAlgorithm、RangeShardingAlgorithm)
  • 复合分片策略 (ComplexKeysShardingAlgorithm)
  • Hint分片策略  (HintShardingAlgorithm)
  • 行表达式分片策略 
  • 不分片策略

5.分页查询不生效

 * MybatisPlus 配置类
 */
@Configuration
public class MybatisPlusConfig {
	@Bean
	public MybatisPlusInterceptor mybatisPlusInterceptor() {
		MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
		interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
		return interceptor;
	}
}
@Override
    public List<Order> pageList(int pageIndex, int size) {
        Page<Order> page = new Page<>(pageIndex, size);
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
    	queryWrapper.orderByAsc("order_id");
        return orderMapper.selectPage(page, queryWrapper).getRecords();
    }

image.png

6.配置信息demo

server:
  port: 8089
  address: 0.0.0.0

#mybatis文件         
mybatis-plus:
  mapper-locations: classpath:/mapper/*Mapper.xml           
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
      
spring:
  shardingsphere:
    # 数据源配置
    datasource:
      # 数据源名称,多数据源以逗号分隔
      names: db0,db1
      db0:
        # 数据库连接池类名称
        #type: com.zaxxer.hikari.HikariDataSource
        type: com.alibaba.druid.pool.DruidDataSource
        # 数据库驱动类名
        driver-class-name: com.mysql.cj.jdbc.Driver
        #jdbc-url: jdbc:mysql://127.0.0.1:3306/db0?useUnicode=true&characterEncoding=UTF8&zeroDateTimeBehavior=convertToNull&useSSL=false
        url: jdbc:mysql://127.0.0.1:3306/db0?useUnicode=true&characterEncoding=UTF8&zeroDateTimeBehavior=convertToNull&useSSL=false        
        username: root
        password: password
      db1:
        # 数据库连接池类名称
        #type: com.zaxxer.hikari.HikariDataSource
        type: com.alibaba.druid.pool.DruidDataSource
        # 数据库驱动类名
        driver-class-name: com.mysql.cj.jdbc.Driver
        #jdbc-url: jdbc:mysql://127.0.0.1:3306/db1?useUnicode=true&characterEncoding=UTF8&zeroDateTimeBehavior=convertToNull&useSSL=false
        url: jdbc:mysql://127.0.0.1:3306/db1?useUnicode=true&characterEncoding=UTF8&zeroDateTimeBehavior=convertToNull&useSSL=false        
        username: root
        password: password
    # 规则配置
    rules:
      sharding:
        # 分片算法配置
        sharding-algorithms:
          database-inline:
            type: INLINE
            props:
              # 分片算法的行表达式(算法自行定义,此处为方便演示效果)
              algorithm-expression: db$->{order_id > 4?1:0}
          table-inline:
            type: INLINE
            props:
              # 分片算法的行表达式
              algorithm-expression: t_order$->{order_id % 4}
        # 分布式序列算法配置
        key-generators:
          snowflake:
            type: SNOWFLAKE
            # 分布式序列算法属性配置
            props:
              # 工作机器唯一标识
              worker-id: 1
        tables:
          # 逻辑表名称
          t_order:
            # 行表达式标识符可以使用 ${...} 或 $->{...},但前者与 Spring 本身的属性文件占位符冲突,因此在 Spring 环境中使用行表达式标识符建议使用 $->{...}
            actual-data-nodes: db${0..1}.t_order${0..3}
            # 分库策略
            database-strategy:
              standard:
                # 分片列名称
                sharding-column: order_id
                # 分片算法名称
                sharding-algorithm-name: database-inline
            # 分表策略
            table-strategy:
              standard:
                # 分片列名称
                sharding-column: order_id
                # 分片算法名称
                sharding-algorithm-name: table-inline
    # 属性配置
    props:
      # 展示修改以后的sql语句
      sql-show: true