资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

springboot+mybatis-plus怎样使用shardingsphere分库分表

spring boot+mybatis-plus怎样使用shardingsphere分库分表,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

创新互联是一家集网站建设,锡林浩特企业网站建设,锡林浩特品牌网站建设,网站定制,锡林浩特网站建设报价,网络营销,网络优化,锡林浩特网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。

一.关于

效果预览:

spring boot+mybatis-plus怎样使用shardingsphere分库分表

spring boot+mybatis-plus怎样使用shardingsphere分库分表

ShardingSphere是一个数据库中间件,可以实现分库分表、读写分离等,详见官网: https://shardingsphere.apache.org/

这里使用spring boot+mybatis-plus的方式来搭建demo。

二.项目搭建

使用vscode(java开发环境可以百度一下)新建一个spring boot项目,引入lombok、mybatis-plus等:

    
		11
		4.1.1
	

	
		
			org.springframework.boot
			spring-boot-starter-web
		
		
			org.mybatis.spring.boot
			mybatis-spring-boot-starter
			2.1.3
		

		
			org.springframework.boot
			spring-boot-devtools
			runtime
			true
		
		
			MySQL
			mysql-connector-java
			runtime
		
		
        
            com.baomidou
            mybatis-plus-boot-starter
            3.3.1
        
		
			org.projectlombok
			lombok
		
		
		
			org.apache.shardingsphere
			sharding-jdbc-spring-boot-starter
			${sharding-sphere.version}
		

		
		
			org.apache.shardingsphere
			sharding-jdbc-spring-namespace
			${sharding-sphere.version}
		
		
            com.alibaba
            druid
            1.1.22
		
		
            org.antlr
            antlr4-runtime
            4.7.2
        

项目结构:

spring boot+mybatis-plus怎样使用shardingsphere分库分表

application.yml配置:

spring:
  shardingsphere:
    datasource:
      names: ds0,ds1
      ds0: 
        type: com.alibaba.druid.pool.DruidDataSource
        driverClassName: com.mysql.jdbc.Driver
        url: jdbc:mysql://localhost:3306/ds0?useUnicode=true&characterEncoding=utf8&tinyInt1isBit=false&useSSL=false&serverTimezone=GMT
        username: root
        password: 123456
      ds1: 
        type: com.alibaba.druid.pool.DruidDataSource
        driverClassName: com.mysql.jdbc.Driver
        url: jdbc:mysql://localhost:3306/ds1?useUnicode=true&characterEncoding=utf8&tinyInt1isBit=false&useSSL=false&serverTimezone=GMT
        username: root
        password: 123456
    sharding:
      tables:
        t_order: 
          actualDataNodes: ds${0..1}.t_order_${0..1}
          databaseStrategy: 
              inline:
                shardingColumn: user_id
                algorithmExpression: ds${user_id % 2}
          tableStrategy: 
            inline:
              shardingColumn: order_id
              algorithmExpression: t_order_${order_id % 2}
          keyGenerator:
            type: SNOWFLAKE
            column: order_id
        t_order_item:
          actualDataNodes: ds${0..1}.t_order_item_${0..1}
          databaseStrategy:
            inline:
              shardingColumn: user_id
              algorithmExpression: ds${user_id % 2}
          tableStrategy:
            inline:
              shardingColumn: order_id
              algorithmExpression: t_order_item_${order_id % 2}  
      binding-tables: t_order,t_order_item
      broadcast-tables: t_config
      defaultDataSourceName: ds0
      defaultTableStrategy:
        none:
      defaultKeyGenerator:
        type: SNOWFLAKE
        column: order_id
    props:
      sql.shwo: true

db:分别建ds0和ds1库,并分别建t_order_0、t_order_1和t_order_item_0、t_order_item_1

# Host: localhost  (Version 5.7.17)
# Date: 2020-08-23 12:31:22
# Generator: MySQL-Front 6.0  (Build 2.29)


#
# Structure for table "t_order_0"
#

CREATE TABLE `t_order_0` (
  `order_id` bigint(11) NOT NULL DEFAULT '0',
  `user_id` bigint(1) DEFAULT NULL,
  `amount` decimal(18,2) DEFAULT NULL,
  `discount` decimal(10,2) DEFAULT NULL,
  `create_time` datetime DEFAULT NULL,
  PRIMARY KEY (`order_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

#
# Structure for table "t_order_item_0"
#

CREATE TABLE `t_order_item_0` (
  `order_id` bigint(11) NOT NULL DEFAULT '0',
  `product_id` bigint(11) DEFAULT NULL,
  `product_name` varchar(255) DEFAULT NULL,
  `price` decimal(10,2) DEFAULT NULL,
  `discount` decimal(10,2) DEFAULT NULL,
  `count` int(11) DEFAULT NULL,
  PRIMARY KEY (`order_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

entity:

Order.java

@Data
@TableName("t_order")
public class Order extends Model {
    /**
     *
     */
    private static final long serialVersionUID = 1L;
    private Long orderId;
    private Long userId;
    private BigDecimal amount;
    private BigDecimal discount;
    private LocalDateTime createTime;

    public Order(){}

    public Order(Long orderId,Long userId,BigDecimal amount,BigDecimal discount,LocalDateTime createTime){
        this.orderId=orderId;
        this.userId=userId;
        this.amount=amount;
        this.discount=discount;
        this.createTime=createTime;
    }
}

OrderItem.java

@Data
@TableName("t_order_item")
public class OrderItem extends Model {
    private Long orderId;
    private Long productId;
    private String productName;
    private BigDecimal price;
    private BigDecimal discount;
    @TableField("`count`")
    private int count;

    public OrderItem(){}

    public OrderItem(Long orderId,Long productId,String productName,BigDecimal price,BigDecimal discount,int count){
        this.orderId=orderId;
        this.productId=productId;
        this.productName=productName;
        this.price=price;
        this.discount=discount;
        this.count=count;
    }
}

mapper:
OrderMapper.java

public interface OrderMapper extends BaseMapper {
    
}

OrderItemMapper.java

public interface OrderItemMapper extends BaseMapper {
    
}

service:

OrderService.java

public interface OrderService extends IService {
    boolean save(Order order,List items);
}

OrderServiceImpl.java

@Service
public class OrderServiceImpl extends ServiceImpl implements OrderService {
    @Autowired
    private OrderItemService orderItemService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean save(Order order, List items) {
        save(order);
        orderItemService.saveBatch(items);
        return true;
    }
    
}

OrderItemService.java

public interface OrderItemService extends IService {
    
}

OrderItemServiceImpl.java

@Service
public class OrderItemServiceimpl extends ServiceImpl implements OrderItemService {
    
}

spring boot启动类:

@MapperScan("com.example.sharding.mapper")
@SpringBootApplication
public class ShardingDemoApplication {

	public static void main(String[] args) {
		SpringApplication.run(ShardingDemoApplication.class, args);
	}

	@Bean
    public PaginationInterceptor paginationInterceptor() {
        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
        // 设置请求的页面大于最大页后操作, true调回到首页,false 继续请求  默认false
        // paginationInterceptor.setOverflow(false);
        // 设置最大单页限制数量,默认 500 条,-1 不受限制
        // paginationInterceptor.setLimit(500);
        // 开启 count 的 join 优化,只针对部分 left join
        paginationInterceptor.setCountSqlParser(new JsqlParserCountOptimize(true));
        return paginationInterceptor;
    }

}

三.测试效果

建一个测试类:OrderServiceImplTest.java  并编写测试语句

package com.example.sharding.service.impl;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.sharding.entity.Order;
import com.example.sharding.entity.OrderItem;
import com.example.sharding.service.OrderItemService;
import com.example.sharding.service.OrderService;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
public class OrderServiceImplTest {
    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderItemService orderItemService;

    @Test
    public void testSave(){
        var order1=new Order(1L,1L,new BigDecimal(22.5),new BigDecimal(1),LocalDateTime.now());
        var orderItemList1=new ArrayList();
        orderItemList1.add(new OrderItem(1L,1L,"苹果",new BigDecimal(6),new BigDecimal(1),2));
        orderItemList1.add(new OrderItem(1L,2L,"香蕉",new BigDecimal(3.5),new BigDecimal(1),3));

        var order2=new Order(2L,1L,new BigDecimal(799),new BigDecimal(1),LocalDateTime.now());
        var orderItemList2=new ArrayList();
        orderItemList2.add(new OrderItem(2L,3L,"鞋子1",new BigDecimal(600),new BigDecimal(1),1));
        orderItemList2.add(new OrderItem(2L,4L,"衬衣1",new BigDecimal(199),new BigDecimal(1),1));

        var order3=new Order(3L,1L,new BigDecimal(399),new BigDecimal(1),LocalDateTime.now());
        var orderItemList3=new ArrayList();
        orderItemList3.add(new OrderItem(3L,5L,"鞋子2",new BigDecimal(200),new BigDecimal(1),1));
        orderItemList3.add(new OrderItem(3L,6L,"衬衣2",new BigDecimal(199),new BigDecimal(1),1));

        var order4=new Order(4L,1L,new BigDecimal(499),new BigDecimal(1),LocalDateTime.now());
        var orderItemList4=new ArrayList();
        orderItemList4.add(new OrderItem(4L,7L,"鞋子3",new BigDecimal(300),new BigDecimal(1),1));
        orderItemList4.add(new OrderItem(4L,8L,"衬衣3",new BigDecimal(199),new BigDecimal(1),1));

        var order5=new Order(5L,1L,new BigDecimal(899),new BigDecimal(1),LocalDateTime.now());
        var orderItemList5=new ArrayList();
        orderItemList5.add(new OrderItem(5L,9L,"鞋子4",new BigDecimal(600),new BigDecimal(1),1));
        orderItemList5.add(new OrderItem(5L,10L,"衬衣4",new BigDecimal(299),new BigDecimal(1),1));
        assertTrue(orderService.save(order1,orderItemList1));
        assertTrue(orderService.save(order2,orderItemList2));
        assertTrue(orderService.save(order3,orderItemList3));
        assertTrue(orderService.save(order4,orderItemList4));
        assertTrue(orderService.save(order5,orderItemList5));
    }

    @Test
    public void testQuery(){
        var page=new Page(1,4);
        var queryWrapper=new QueryWrapper();
        queryWrapper.orderByAsc("order_id");
        
        var itemPage=orderItemService.page(page,queryWrapper);
        assertEquals(4, itemPage.getRecords().size());
        assertEquals("苹果", itemPage.getRecords().get(0).getProductName());
    }
}

测试通过,看一下数据库的数据:

spring boot+mybatis-plus怎样使用shardingsphere分库分表

spring boot+mybatis-plus怎样使用shardingsphere分库分表

看完上述内容,你们掌握spring boot+mybatis-plus怎样使用shardingsphere分库分表的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注创新互联行业资讯频道,感谢各位的阅读!


网页标题:springboot+mybatis-plus怎样使用shardingsphere分库分表
URL标题:http://cdkjz.cn/article/gihddo.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220