shardingsphere集成mybatis/mybatis-plus快速实现简单分片

1、导入依赖


            com.baomidou
            mybatis-plus-boot-starter
            3.4.2
        
        
            org.springframework.boot
            spring-boot-starter-jdbc
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            2.1.3
        
        
            com.alibaba
            druid
            1.1.23
        
        
            org.apache.shardingsphere
            sharding-jdbc-spring-boot-starter
            4.1.1
        
        
            mysql
            mysql-connector-java
            runtime
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
            
                
                    org.junit.vintage
                    junit-vintage-engine
                
            
        

2、新建数据表

shardingsphere集成mybatis/mybatis-plus快速实现简单分片_第1张图片

3、添加实体类

package com.example.sharding.domain;

import com.baomidou.mybatisplus.annotation.TableName;

@TableName("orders_t")
public class Orders {
    private Integer id;
    private Integer orderType;
    private Integer customerId;
    private Double amount;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Integer getOrderType() {
        return orderType;
    }

    public void setOrderType(Integer orderType) {
        this.orderType = orderType;
    }

    public Integer getCustomerId() {
        return customerId;
    }

    public void setCustomerId(Integer customerId) {
        this.customerId = customerId;
    }

    public Double getAmount() {
        return amount;
    }

    public void setAmount(Double amount) {
        this.amount = amount;
    }

    @Override
    public String toString() {
        return "Orders{" +
                "id=" + id +
                ", orderType=" + orderType +
                ", customerId=" + customerId +
                ", amount=" + amount +
                '}';
    }
}

4、添加mapper

package com.example.sharding.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.sharding.domain.Orders;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@Mapper
public interface OrdersMapper extends BaseMapper {

    @Insert("insert into orders_t(id,order_type,customer_id,amount) values(#{id},#{orderType},#{customerId},#{amount})")
    public int insert(Orders orders);

    @Select("select * from orders_t where id = #{id}")
    @Results({
            @Result(property = "id",column = "id"),
            @Result(property = "orderType",column = "order_type"),
            @Result(property = "customerId",column = "customer_id"),
            @Result(property = "amount",column = "amount")
    })
    public Orders selectOne(Integer id);

    @Select("select * from orders_t")
    @Results({
            @Result(property = "id",column = "id"),
            @Result(property = "orderType",column = "order_type"),
            @Result(property = "customerId",column = "customer_id"),
            @Result(property = "amount",column = "amount")
    })
    List selectAll();
}

5、添加yml文件

mybatis:
  type-aliases-package: com.example.mapper

mybatis-plus:
  mapperLocations: classpath*:mapper/*Mapper.xml

spring:
  main:
    allow-bean-definition-overriding: true # 需要配置否则加载数据源报错 是否允许定义重名的bean对象覆盖原有的bean
  profiles:
    active: database
  sharding-sphere:
    datasource:
      names: ds1
    # Configuring the second database
    datasource.ds1:
      type: com.alibaba.druid.pool.DruidDataSource
      driver-class-name: com.mysql.jdbc.Driver
      url: "jdbc:mysql://127.0.0.1:3306/lable7?serverTimezone=UTC"
      username: root
      password: root

    # Configuring the database sharding strategy
    sharding:
      tables:
        orders_t:
          actual-data-nodes: ds1.orders_t_$->{1..2}
          key-generator:
            column: id
            type: SNOWFLAKE
          table-strategy:
            inline:
              sharding-column: id
              algorithm-expression: orders_t_${id%2+1}

6、添加测试方法

package com.example.sharding;

import com.example.sharding.domain.Orders;
import com.example.sharding.mapper.OrdersMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

@SpringBootTest
class ShardingsphereDemoApplicationTests {

    @Autowired
    private OrdersMapper ordersMapper;
    @Test
    public void addOrders(){
        for (int i = 1; i <=10 ; i++) {
            Orders orders = new Orders();
            orders.setId(i);
            orders.setCustomerId(i);
            orders.setOrderType(i);
            orders.setAmount(1000.0*i);
            ordersMapper.insert(orders);
        }
    }
    @Test
    public void queryOrders(){
//        Orders orders = ordersMapper.selectOne(1);
        List orderss = ordersMapper.selectAll();
        Orders orders = ordersMapper.selectById(1);
        System.out.println(orders);
    }

}

6.1、插入数据

shardingsphere集成mybatis/mybatis-plus快速实现简单分片_第2张图片

shardingsphere集成mybatis/mybatis-plus快速实现简单分片_第3张图片

shardingsphere集成mybatis/mybatis-plus快速实现简单分片_第4张图片

6.2、查询条数据与全部数据

shardingsphere集成mybatis/mybatis-plus快速实现简单分片_第5张图片

7、注意mybatis-plus要添加@TableName(“你的表”)不然有可能找不到表

shardingsphere集成mybatis/mybatis-plus快速实现简单分片_第6张图片

你可能感兴趣的:(java,yml,mybatis,mybatis-lus,shardingsphere)