MySQL分表之横向分割表(插入)

1、在数据库中创建两个表

1.1、两张表属性一样,名字不一样

2、在IDEA中创建一个新的项目,项目中有mapper、service、controller、实体类、启动类

mappers

 service

 controller

mapper.xml 

实体类

启动类 

 2.1、pom文件导包

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.2.2</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.22</version>
        </dependency>
<!--        分库分表-->
        <dependency>
            <groupId>org.apache.shardingsphere</groupId>
            <artifactId>sharding-jdbc-spring-boot-starter</artifactId>
            <version>4.0.0-RC2</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
        </dependency>
        <dependency>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-impl</artifactId>
            <version>2.3.0</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jaxb</groupId>
            <artifactId>jaxb-runtime</artifactId>
            <version>2.3.0</version>
        </dependency>
        <dependency>
            <groupId>javax.activation</groupId>
            <artifactId>activation</artifactId>
            <version>1.1.1</version>
        </dependency>

    </dependencies>

 2.2、application.properties配置文件

server.port=7007
spring.application.name=product-sk-server
#bean覆盖
spring.main.allow-bean-definition-overriding=true
#mtbatis
mybatis.mapper-locations=classpath:/mappers/*.xml
#设置数据源名称,多数据源以逗号隔开
spring.shardingsphere.datasource.names=m1
#数据库连接池类名称
spring.shardingsphere.datasource.m1.type=com.alibaba.druid.pool.DruidDataSource
#数据库驱动类名
spring.shardingsphere.datasource.m1.driver-class-name=com.mysql.cj.jdbc.Driver
#数据库url连接
spring.shardingsphere.datasource.m1.url=jdbc:mysql://127.0.0.1:3306/product?serverTimezone=Asia/Shanghai
#数据库用户名
spring.shardingsphere.datasource.m1.username=root
#数据库密码
spring.shardingsphere.datasource.m1.password=123456

#根据id进行表的划分
spring.shardingsphere.sharding.tables.product.keyGenerator.column=id
#根据雪花算法生成表的id
spring.shardingsphere.sharding.tables.product.keyGenerator.type=SNOWFLAKE
#节点有几个(分表)
spring.shardingsphere.sharding.tables.product.actualDataNodes=m1.product_$->{1..2}
#根据id进行计算
spring.shardingsphere.sharding.tables.product.tableStrategy.inline.shardingColumn=id
#
spring.shardingsphere.sharding.tables.product.tableStrategy.inline.algorithmExpression=product_$->{id % 2 + 1}

spring.shardingsphere.props.sql.show=true

logging.level.root = info
logging.level.org.springframework.web = info
logging.level.com.itheima.dbsharding  = debug
logging.level.druid.sql = debug

2.3、一切准备就绪,使用postman进行测试,路径一定要写对,我是post接口

2.4、成功了到Navicat中查看表中是否插入数据

到这里就结束了,希望对您有帮助 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小东很不戳

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值