ShardingSphere快速开始与核心概念

本文介绍了ShardingSphere作为数据库中间件的功能,包括Sharding-JDBC的客户端直连特性、Sharding-Proxy的透明代理,以及如何在Spring Boot和Mybatis环境中快速集成。涵盖了分库分表、读写分离、主从策略和配置示例。
摘要由CSDN通过智能技术生成

定位

ShardingSphere定位为关系型数据库中间件

功能列表:

 

 核心三套件

Sharding-JDBC

客户端直连数据库,以jar包形式提供服务,无需额外部署和依赖,可理解为增强版的 JDBC驱动,完全兼容JDBC和各种ORM框架。

  • 适用于任何基于JDBC的ORM框架:JPA, Hibernate, Mybatis, Spring JDBC Template或直 接使用JDBC。
  • 支持任何第三方的数据库连接池:DBCP, C3P0, BoneCP, Druid, HikariCP等。 支持任意实现JDBC规范的数据库。
  • 支持MySQL,Oracle,SQLServer,PostgreSQL等遵循 SQL92标准的数据库。 

Sharding-Proxy

透明化的数据库代理端,兼容所有MySQL/PostgreSQL协议的访问客户端。

向应用程序完全透明,可直接当做MySQL/PostgreSQL使用。

核心概念 

逻辑表

水平拆分的数据库(表)的相同逻辑和数据结构表的总称。例:订单数据根据主键尾数 拆分为10张表,分别是t_order_0到t_order_9,他们的逻辑表名为t_order。

真实表

在分片的数据库中真实存在的物理表。即上个示例中的t_order_0到t_order_9。

数据节点

数据分片的最小单元。由数据源名称和数据表组成,例:ds_0.t_order_0。

绑定表

分片规则一致的主表和子表。例如:t_order表和t_order_item表,均按照order_id分 片,则此两张表互为绑定表关系。绑定表之间的多表关联查询不会出现笛卡尔积关联,关联 查询效率将大大提升。

广播表

指所有的分片数据源中都存在的表,表结构和表中的数据在每个数据库中均完全一致。 适用于数据量不大且需要与海量数据的表进行关联查询的场景。字典表就是典型的场景。

ShardingSphere快速启动

shardingsphere+springboot+mybatis集成使用

1、引入项目依赖

<dependencies>
        <!-- springboot-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.0.5.RELEASE</version>
        </dependency>
        <!-- mybatis -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.0.1</version>
            <exclusions>
                <exclusion>
                    <artifactId>spring-boot-starter</artifactId>
                    <groupId>org.springframework.boot</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <!-- shardingsphere-jdbc,这里使用的版本为apache孵化版本,4.0之前
        都是没有捐献给apache基金会的版本,之前的版本都在-->
        <dependency>
            <groupId>org.apache.shardingsphere</groupId>
            <artifactId>sharding-jdbc-spring-boot-starter</artifactId>
            <version>4.1.1</version>
        </dependency>
        <!-- mysql 驱动 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.28</version>
        </dependency>
        <!-- 可选,工具类 -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.20</version>
            <scope>provided</scope>
            分库不分表配置
        </dependency>
    </dependencies>

分库不分表配置

# 配置ds0 和ds1两个数据源,这里有个坑(使用下划线可能会有异常产生,字符不支持,如:ds_0)
spring.shardingsphere.datasource.names=ds0,ds1

#ds0 配置
spring.shardingsphere.datasource.ds0.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.ds0.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.ds0.jdbc-url=jdbc:mysql://192.168.241.198:3306/shop_ds_0?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8
spring.shardingsphere.datasource.ds0.username=root
spring.shardingsphere.datasource.ds0.password=root
#ds1 配置
spring.shardingsphere.datasource.ds1.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.ds1.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.ds1.jdbc-url=jdbc:mysql://192.168.241.198:3306/shop_ds_1?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8
spring.shardingsphere.datasource.ds1.username=root
spring.shardingsphere.datasource.ds1.password=root

# 分库策略 根据id取模确定数据进哪个数据库
spring.shardingsphere.sharding.default-database-strategy.inline.sharding-column=user_id
spring.shardingsphere.sharding.default-database-strategy.inline.algorithm-expression=ds$->{user_id % 2}
spring.shardingsphere.sharding.binding-tables=t_order,t_order_item
spring.shardingsphere.sharding.broadcast-tables=t_address

# t_order表策略
spring.shardingsphere.sharding.tables.t_order.actual-data-nodes=ds$->{0..1}.t_order
# 使用SNOWFLAKE算法生成主键
spring.shardingsphere.sharding.tables.t_order.key-generator.column=order_id
spring.shardingsphere.sharding.tables.t_order.key-generator.type=SNOWFLAKE
#spring.shardingsphere.sharding.tables.t_order.key-generator.props.worker.id=123
# t_order_item表策略
spring.shardingsphere.sharding.tables.t_order_item.actual-data-nodes=ds$->{0..1}.t_order_item
# 使用SNOWFLAKE算法生成主键
spring.shardingsphere.sharding.tables.t_order_item.key-generator.column=order_item_id
spring.shardingsphere.sharding.tables.t_order_item.key-generator.type=SNOWFLAKE
#spring.shardingsphere.sharding.tables.t_order_item.key-generator.props.worker.id=123

分库分表

# 配置ds0 和ds1两个数据源
spring.shardingsphere.datasource.names=ds0,ds1

#ds0 配置
spring.shardingsphere.datasource.ds0.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.ds0.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.ds0.jdbc-url=jdbc:mysql://127.0.0.1:3306/shop_ds_0?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8
spring.shardingsphere.datasource.ds0.username=root
spring.shardingsphere.datasource.ds0.password=root
#ds1 配置
spring.shardingsphere.datasource.ds1.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.ds1.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.ds1.jdbc-url=jdbc:mysql://127.0.0.1:3306/shop_ds_1?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8
spring.shardingsphere.datasource.ds1.username=root
spring.shardingsphere.datasource.ds1.password=root

# 分库策略 根据id取模确定数据进哪个数据库
spring.shardingsphere.sharding.default-database-strategy.inline.sharding-column=user_id
spring.shardingsphere.sharding.default-database-strategy.inline.algorithm-expression=ds$->{user_id % 2}
spring.shardingsphere.sharding.binding-tables=t_order,t_order_item
spring.shardingsphere.sharding.broadcast-tables=t_address

# 具体分表策略
# 节点 ds0.t_order_0,ds0.t_order_1,ds1.t_order_0,ds1.t_order_1
spring.shardingsphere.sharding.tables.t_order.actual-data-nodes=ds$->{0..1}.t_order_$->{0..1}
# 分表字段id
spring.shardingsphere.sharding.tables.t_order.table-strategy.inline.sharding-column=order_id
# 分表策略 根据id取模,确定数据最终落在那个表中
spring.shardingsphere.sharding.tables.t_order.table-strategy.inline.algorithm-expression = t_order_$->{order_id % 2}
# 使用SNOWFLAKE算法生成主键
spring.shardingsphere.sharding.tables.t_order.key-generator.column=order_id
spring.shardingsphere.sharding.tables.t_order.key-generator.type=SNOWFLAKE
#spring.shardingsphere.sharding.tables.t_order.key-generator.props.worker.id=123

# 节点 ds0.t_order_item_0,ds0.t_order_item_1,ds1.t_order_item_0,ds1.t_order_item_1
spring.shardingsphere.sharding.tables.t_order_item.actual-data-nodes=ds$->{0..1}.t_order_item_$->{0..1}
# 分表字段id
spring.shardingsphere.sharding.tables.t_order_item.table-strategy.inline.sharding-column=order_id
# 分表策略 根据id取模,确定数据最终落在那个表中
spring.shardingsphere.sharding.tables.t_order_item.table-strategy.inline.algorithm-expression=t_order_item_$->{order_id % 2}
# 使用SNOWFLAKE算法生成主键
spring.shardingsphere.sharding.tables.t_order_item.key-generator.column=order_item_id
spring.shardingsphere.sharding.tables.t_order_item.key-generator.type=SNOWFLAKE
#spring.shardingsphere.sharding.tables.t_order_item.key-generator.props.worker.id=123

读写分离

#shardingsphere 读写分离,master-slave,可以一主多从
spring.shardingsphere.datasource.names=ds-master,ds-slave0
#主库
spring.shardingsphere.datasource.ds-master.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.ds-master.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.ds-master.jdbc-url=jdbc:mysql://192.168.241.198:3306/shop_ds_master?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8
spring.shardingsphere.datasource.ds-master.username=root
spring.shardingsphere.datasource.ds-master.password=root
#从库0
spring.shardingsphere.datasource.ds-slave0.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.ds-slave0.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.ds-slave0.jdbc-url=jdbc:mysql://192.168.241.199:3306/shop_ds_slave?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8
spring.shardingsphere.datasource.ds-slave0.username=root
spring.shardingsphere.datasource.ds-slave0.password=root
#从库1
#spring.shardingsphere.datasource.ds-slave1.type=com.zaxxer.hikari.HikariDataSource
#spring.shardingsphere.datasource.ds-slave1.driver-class-name=com.mysql.jdbc.Driver
#spring.shardingsphere.datasource.ds-slave1.jdbc-url=jdbc:mysql://192.168.241.199:3306/shop_ds_slave1?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8
#spring.shardingsphere.datasource.ds-slave1.username=root
#spring.shardingsphere.datasource.ds-slave1.password=root

#读写分离主从规则设置,当有2个以上从库时,从库读采用轮询的负载均衡机制
spring.shardingsphere.masterslave.load-balance-algorithm-type=round_robin
spring.shardingsphere.masterslave.name=ms
spring.shardingsphere.masterslave.master-data-source-name=ds-master
spring.shardingsphere.masterslave.slave-data-source-names=ds-slave0

读写分离+分库分表

#shardingsphere 读写分离+分库分表,master-slave-sharding,可以一主多从
spring.shardingsphere.datasource.names=ds-master0,ds-slave0,ds-master1,ds-slave1
#主库0
spring.shardingsphere.datasource.ds-master0.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.ds-master0.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.ds-master0.jdbc-url=jdbc:mysql://192.168.241.198:3306/shop_ds_master?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8
spring.shardingsphere.datasource.ds-master0.username=root
spring.shardingsphere.datasource.ds-master0.password=root
#从库0
spring.shardingsphere.datasource.ds-slave0.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.ds-slave0.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.ds-slave0.jdbc-url=jdbc:mysql://192.168.241.199:3306/shop_ds_slave?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8
spring.shardingsphere.datasource.ds-slave0.username=root
spring.shardingsphere.datasource.ds-slave0.password=root
#主库1
spring.shardingsphere.datasource.ds-master1.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.ds-master1.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.ds-master1.jdbc-url=jdbc:mysql://192.168.241.198:3306/shop_ds_master1?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8
spring.shardingsphere.datasource.ds-master1.username=root
spring.shardingsphere.datasource.ds-master1.password=root
#从库1
spring.shardingsphere.datasource.ds-slave1.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.ds-slave1.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.ds-slave1.jdbc-url=jdbc:mysql://192.168.241.199:3306/shop_ds_slave1?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8
spring.shardingsphere.datasource.ds-slave1.username=root
spring.shardingsphere.datasource.ds-slave1.password=root

# 分库策略 根据id取模确定数据进哪个数据库
spring.shardingsphere.sharding.default-database-strategy.inline.sharding-column=user_id
spring.shardingsphere.sharding.default-database-strategy.inline.algorithm-expression=ds_$->{user_id % 2}
spring.shardingsphere.sharding.binding-tables=t_order,t_order_item
spring.shardingsphere.sharding.broadcast-tables=t_address

#分表策略
spring.shardingsphere.sharding.tables.t_order.actual-data-nodes=ds_$->{0..1}.t_order_$->{0..1}
spring.shardingsphere.sharding.tables.t_order.table-strategy.inline.sharding-column=order_id
spring.shardingsphere.sharding.tables.t_order.table-strategy.inline.algorithm-expression=t_order_$->{order_id % 2}
spring.shardingsphere.sharding.tables.t_order.key-generator.column=order_id
spring.shardingsphere.sharding.tables.t_order.key-generator.type=SNOWFLAKE
#spring.shardingsphere.sharding.tables.t_order.key-generator.props.worker.id=123
spring.shardingsphere.sharding.tables.t_order_item.actual-data-nodes=ds_$->{0..1}.t_order_item_$->{0..1}
spring.shardingsphere.sharding.tables.t_order_item.table-strategy.inline.sharding-column=order_id
spring.shardingsphere.sharding.tables.t_order_item.table-strategy.inline.algorithm-expression=t_order_item_$->{order_id % 2}
spring.shardingsphere.sharding.tables.t_order_item.key-generator.column=order_item_id
spring.shardingsphere.sharding.tables.t_order_item.key-generator.type=SNOWFLAKE
#spring.shardingsphere.sharding.tables.t_order_item.key-generator.props.worker.id=123

#读写分离数据源0
spring.shardingsphere.sharding.master-slave-rules.ds_0.master-data-source-name=ds-master0
spring.shardingsphere.sharding.master-slave-rules.ds_0.slave-data-source-names=ds-slave0
#读写分离主从规则设置,当有2个以上从库时,从库读采用轮询的负载均衡机制
spring.shardingsphere.sharding.master-slave-rules.ds_0.load-balance-algorithm-type=ROUND_ROBIN

#读写分离数据源1
spring.shardingsphere.sharding.master-slave-rules.ds_1.master-data-source-name=ds-master1
spring.shardingsphere.sharding.master-slave-rules.ds_1.slave-data-source-names=ds-slave1
#读写分离主从规则设置,当有2个以上从库时,从库读采用轮询的负载均衡机制
spring.shardingsphere.sharding.master-slave-rules.ds_1.load-balance-algorithm-type=ROUND_ROBIN

主配置

server.port=8080

# 实体类路径
mybatis.type-aliases-package=com.tl.it.edu.entity

#打印出sql语句
#spring.shardingsphere.props.sql.show = true
#加载配置-分库不分表
#spring.profiles.active=sharding-databases
#分表不分库
#spring.profiles.active=sharding-tables
#加载配置-分库也分表
spring.profiles.active=sharding-databases-tables
# 主从master-slave,读写分离
#spring.profiles.active=sharding-master-slave
# 读写分离+数据分片
#spring.profiles.active=sharding-masterslave-databases-tables
#使用后端代理-实现分库分表
#spring.profiles.active=sharding-proxy

mybatis相关代码

@Configuration
public class CommonConfiguration {

    //注册到容器中
    @Bean
    public ConfigurationCustomizer configurationCustomizer(){
        return new ConfigurationCustomizer(){
            @Override
            public void customize(org.apache.ibatis.session.Configuration configuration) {
                //开启驼峰命名规则
                configuration.setMapUnderscoreToCamelCase(true);
            }
        };
    }

}

Mapper

@Mapper
public interface OrderMapper {

    @Options(useGeneratedKeys = true,keyProperty = "orderId",keyColumn = "order_id")
    @Insert("INSERT INTO t_order (user_id, address_id, status) VALUES (#{userId,jdbcType=INTEGER}," +
            " #{addressId,jdbcType=BIGINT}, #{status,jdbcType=VARCHAR})")
    long insert(Order order) throws SQLException;

    @Delete("DELETE FROM t_order WHERE order_id = #{orderId}")
    void delete(long orderId) throws SQLException;

    @Select("SELECT * FROM t_order")
    List<Order> selectAll() throws SQLException;

    @Select("SELECT * FROM t_order WHERE order_id BETWEEN #{start} AND #{end}")
    List<Order> selectRange(@Param("start") long start,@Param("end") long end) throws SQLException;

    @Select("SELECT * FROM t_order order by order_id limit #{offset},#{size}")
    List<Order> selectPageList(@Param("offset") long offset,@Param("size") long size) throws SQLException;

    @Update("update t_order set status = #{status} where order_id = #{orderId}")
    int update(@Param("orderId") long orderId, @Param("status") String status) throws SQLException;
}
@Mapper
public interface OrderItemMapper {

    @Options(useGeneratedKeys = true,keyProperty = "orderItemId",keyColumn = "order_item_id")
    @Insert("INSERT INTO t_order_item (order_id, user_id, status) VALUES (#{orderId,jdbcType=INTEGER}," +
            " #{userId,jdbcType=INTEGER}, #{status,jdbcType=VARCHAR})")
    long insert(OrderItem order) throws SQLException;

    @Delete("DELETE FROM t_order_item WHERE order_id = #{orderId}")
    void delete(long orderId) throws SQLException;

    /**
     * 查询所有数据
     * @return
     * @throws SQLException
     */
    @Select("SELECT i.* FROM t_order o, t_order_item i WHERE o.order_id = i.order_id")
    List<OrderItem> selectAll() throws SQLException;

    /**
     * 绑定表,避免无笛卡尔积查询现象,
     * @param start
     * @param end
     * @return
     * @throws SQLException
     */
    @Select("SELECT i.* FROM t_order o JOIN t_order_item i ON o.order_id=i.order_id WHERE o.order_id = 0 or o.order_id = 1")
    List<OrderItem> selectWithInCondition(@Param("start") long start,@Param("end") long end) throws SQLException;

    /**
     * 不支持该路由规则
     */
    @Select("SELECT i.* FROM t_order o, t_order_item i WHERE o.order_id = i.order_id" +
            " AND o.user_id BETWEEN #{start} AND #{end}")
    List<OrderItem> selectRange(@Param("start") int start,@Param("end") int end) throws SQLException;

    @Update("update t_order_item set status = #{status} where order_id = #{orderId}")
    int update(@Param("orderId") long orderId,@Param("status") String status) throws SQLException;
}
@Mapper
public interface AddressMapper {
}
@Mapper
public interface UserMapper {
}

service

@Slf4j
@Service
public class OrderServiceImpl {

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    OrderItemMapper orderItemMapper;

    @Transactional
    public long confirmOrder(int sequenceId){
        //创建订单
        Order order = new Order();
        order.setAddressId(sequenceId);
        order.setUserId(sequenceId);
        order.setStatus("创建订单");
        try {
            orderMapper.insert(order);
            //订单对应产品
            OrderItem item = new OrderItem();
            item.setOrderId(order.getOrderId());
            item.setUserId(sequenceId);
            item.setOrderItemId(sequenceId);
            item.setStatus("创建订单");
            orderItemMapper.insert(item);
        } catch (SQLException e) {
            log.info(e.getMessage(),e.getCause());
            throw new RuntimeException("SQLException",e.getCause());
        }
        return order.getOrderId();
    }

    public OrderInfoDto selectAll(){
        try {
            return new OrderInfoDto(orderMapper.selectAll(),orderItemMapper.selectAll());
        } catch (SQLException e) {
            log.info(e.getMessage(),e.getCause());
        }
        return null;
    }

    @Transactional
    public String deleteData(long orderId){
        try {
            orderMapper.delete(orderId);
            orderItemMapper.delete(orderId);
            return "delete data success";
        } catch (SQLException e) {
            log.info(e.getMessage(),e.getCause());
        }
        return "failure";
    }

    @Transactional
    public int updateOrder(long orderId,String status) {
        try {
            return orderMapper.update(orderId,status);
        } catch (SQLException e) {
            log.info(e.getMessage(),e.getCause());
            throw new RuntimeException("exception is happenning, tx will be rollback",e.getCause());
        }
    }

    public OrderInfoDto selectOrderRange(long start,long end){
        try {
            return new OrderInfoDto(orderMapper.selectRange(start,end),null);
        } catch (SQLException e) {
            log.info(e.getMessage(),e.getCause());
        }
        return null;
    }

    public OrderInfoDto selectOrderItemRange(int start,int end){
        try {
            return new OrderInfoDto(null,orderItemMapper.selectRange(start,end));
        } catch (SQLException e) {
            log.info(e.getMessage(),e.getCause());
        }
        return null;
    }

    public OrderInfoDto selectOrderItemWithIn(long start,long end){
        try {
            return new OrderInfoDto(null,orderItemMapper.selectWithInCondition(start,end));
        } catch (SQLException e) {
            log.info(e.getMessage(),e.getCause());
        }
        return null;
    }

    public OrderInfoDto selectOrderPageList(long offset,long size){
        try {
            return new OrderInfoDto(orderMapper.selectRange(offset,size),null);
        } catch (SQLException e) {
            log.info(e.getMessage(),e.getCause());
        }
        return null;
    }

}

entity

public class Order implements Serializable {
    
    private static final long serialVersionUID = 661434701950670670L;
    
    private long orderId;
    
    private int userId;
    
    private long addressId;
    
    private String status;
    
    public long getOrderId() {
        return orderId;
    }
    
    public void setOrderId(final long orderId) {
        this.orderId = orderId;
    }
    
    public int getUserId() {
        return userId;
    }
    
    public void setUserId(final int userId) {
        this.userId = userId;
    }
    
    public String getStatus() {
        return status;
    }
    
    public void setStatus(final String status) {
        this.status = status;
    }
    
    public long getAddressId() {
        return addressId;
    }
    
    public void setAddressId(final long addressId) {
        this.addressId = addressId;
    }
    
    @Override
    public String toString() {
        return String.format("order_id: %s, user_id: %s, address_id: %s, status: %s", orderId, userId, addressId, status);
    }
}
public class OrderItem implements Serializable {
    
    private static final long serialVersionUID = 263434701950670170L;
    
    private long orderItemId;
    
    private long orderId;
    
    private int userId;
    
    private String status;
    
    public long getOrderItemId() {
        return orderItemId;
    }
    
    public void setOrderItemId(final long orderItemId) {
        this.orderItemId = orderItemId;
    }
    
    public long getOrderId() {
        return orderId;
    }
    
    public void setOrderId(final long orderId) {
        this.orderId = orderId;
    }
    
    public int getUserId() {
        return userId;
    }
    
    public void setUserId(final int userId) {
        this.userId = userId;
    }
    
    public String getStatus() {
        return status;
    }
    
    public void setStatus(final String status) {
        this.status = status;
    }
    
    @Override
    public String toString() {
        return String.format("order_item_id:%s, order_id: %s, user_id: %s, status: %s", orderItemId, orderId, userId, status);
    }
}
public class Address {
    
    private static final long serialVersionUID = 661434701950670670L;
    
    private Long addressId;
    
    private String addressName;
    
    public Long getAddressId() {
        return addressId;
    }
    
    public void setAddressId(final Long addressId) {
        this.addressId = addressId;
    }
    
    public String getAddressName() {
        return addressName;
    }
    
    public void setAddressName(final String addressName) {
        this.addressName = addressName;
    }
}
public class User implements Serializable {
    
    private static final long serialVersionUID = 263434701950670170L;
    
    private int userId;
    
    private String userName;
    
    private String userNamePlain;
    
    private String pwd;
    
    private String assistedQueryPwd;
    
    public int getUserId() {
        return userId;
    }
    
    public void setUserId(final int userId) {
        this.userId = userId;
    }
    
    public String getUserName() {
        return userName;
    }
    
    public void setUserName(final String userName) {
        this.userName = userName;
    }
    
    public String getUserNamePlain() {
        return userNamePlain;
    }
    
    public void setUserNamePlain(final String userNamePlain) {
        this.userNamePlain = userNamePlain;
    }
    
    public String getPwd() {
        return pwd;
    }
    
    public void setPwd(final String pwd) {
        this.pwd = pwd;
    }
    
    public String getAssistedQueryPwd() {
        return assistedQueryPwd;
    }
    
    public void setAssistedQueryPwd(final String assistedQueryPwd) {
        this.assistedQueryPwd = assistedQueryPwd;
    }
    
    @Override
    public String toString() {
        return String.format("user_id: %d, user_name: %s, user_name_plain: %s, pwd: %s, assisted_query_pwd: %s", userId, userName, userNamePlain, pwd, assistedQueryPwd);
    }
}

dto

@AllArgsConstructor
@RequiredArgsConstructor
@Setter
@Getter
public class OrderInfoDto {

    private List<Order> order;

    private List<OrderItem> item;

}

controller

@Slf4j
@RestController
public class ShardingController {

    @Autowired
    OrderServiceImpl orderService;

    @GetMapping("/confirm_order")
    public String confirmOrder(int sequenceId){
        long id = orderService.confirmOrder(sequenceId);
        return "创建订单成功:订单ID = " + id;
    }

    @GetMapping("/order_histroy_list")
    public OrderInfoDto orderHistoryList(){
        return orderService.selectAll();
    }

    /**
     * 删除历史订单
     * @param orderId
     * @return
     */
    @GetMapping("/delete_histroy_order")
    public String deleteHistroyOrder(long orderId){
        return orderService.deleteData(orderId);
    }

    /**
     * 更改历史订单状态
     * @param orderId
     * @param status
     * @return
     */
    @GetMapping("/update_histroy_order")
    public int updateHistoryOrderStatus(long orderId,String status){
        return orderService.updateOrder(orderId,status);
    }

    /**
     * range orderid {200000000000000000 - 400000000000000000}
     * @param start
     * @param end
     * @return
     */
    @GetMapping("/order_range_list")
    public OrderInfoDto orderRangeList(long start,long end){
        return orderService.selectOrderRange(start,end);
    }

    /**
     * range userid {1-20}
     * @param start
     * @param end
     * @return
     */
    @GetMapping("/item_range_list")
    public OrderInfoDto orderItemRangeList(int start,int end){
        return orderService.selectOrderItemRange(start,end);
    }

    /**
     * 笛卡尔积测试
     * @param start
     * @param end
     * @return
     */
    @GetMapping("/item_range_in_list")
    public OrderInfoDto orderItemRangeInList(long start,long end){
        return orderService.selectOrderItemWithIn(start,end);
    }

    @GetMapping("/item_page_list")
    public OrderInfoDto orderPageList(long offset,long size){
        return orderService.selectOrderPageList(offset,size);
    }
}

启动类

@MapperScan(basePackages = "com.tl.it.edu.mapper")
@SpringBootApplication
public class TlShopApplication {

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

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

小强同志

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

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

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

打赏作者

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

抵扣说明:

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

余额充值