当前位置:   article > 正文

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

shardingsphere mybatisplus

1、导入依赖

  1. <dependency>
  2. <groupId>com.baomidou</groupId>
  3. <artifactId>mybatis-plus-boot-starter</artifactId>
  4. <version>3.4.2</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.springframework.boot</groupId>
  8. <artifactId>spring-boot-starter-jdbc</artifactId>
  9. </dependency>
  10. <dependency>
  11. <groupId>org.mybatis.spring.boot</groupId>
  12. <artifactId>mybatis-spring-boot-starter</artifactId>
  13. <version>2.1.3</version>
  14. </dependency>
  15. <dependency>
  16. <groupId>com.alibaba</groupId>
  17. <artifactId>druid</artifactId>
  18. <version>1.1.23</version>
  19. </dependency>
  20. <dependency>
  21. <groupId>org.apache.shardingsphere</groupId>
  22. <artifactId>sharding-jdbc-spring-boot-starter</artifactId>
  23. <version>4.1.1</version>
  24. </dependency>
  25. <dependency>
  26. <groupId>mysql</groupId>
  27. <artifactId>mysql-connector-java</artifactId>
  28. <scope>runtime</scope>
  29. </dependency>
  30. <dependency>
  31. <groupId>org.springframework.boot</groupId>
  32. <artifactId>spring-boot-starter-test</artifactId>
  33. <scope>test</scope>
  34. <exclusions>
  35. <exclusion>
  36. <groupId>org.junit.vintage</groupId>
  37. <artifactId>junit-vintage-engine</artifactId>
  38. </exclusion>
  39. </exclusions>
  40. </dependency>

2、新建数据表

3、添加实体类

  1. package com.example.sharding.domain;
  2. import com.baomidou.mybatisplus.annotation.TableName;
  3. @TableName("orders_t")
  4. public class Orders {
  5. private Integer id;
  6. private Integer orderType;
  7. private Integer customerId;
  8. private Double amount;
  9. public Integer getId() {
  10. return id;
  11. }
  12. public void setId(Integer id) {
  13. this.id = id;
  14. }
  15. public Integer getOrderType() {
  16. return orderType;
  17. }
  18. public void setOrderType(Integer orderType) {
  19. this.orderType = orderType;
  20. }
  21. public Integer getCustomerId() {
  22. return customerId;
  23. }
  24. public void setCustomerId(Integer customerId) {
  25. this.customerId = customerId;
  26. }
  27. public Double getAmount() {
  28. return amount;
  29. }
  30. public void setAmount(Double amount) {
  31. this.amount = amount;
  32. }
  33. @Override
  34. public String toString() {
  35. return "Orders{" +
  36. "id=" + id +
  37. ", orderType=" + orderType +
  38. ", customerId=" + customerId +
  39. ", amount=" + amount +
  40. '}';
  41. }
  42. }

4、添加mapper

  1. package com.example.sharding.mapper;
  2. import com.baomidou.mybatisplus.core.mapper.BaseMapper;
  3. import com.example.sharding.domain.Orders;
  4. import org.apache.ibatis.annotations.*;
  5. import org.springframework.stereotype.Repository;
  6. import java.util.List;
  7. @Repository
  8. @Mapper
  9. public interface OrdersMapper extends BaseMapper<Orders> {
  10. @Insert("insert into orders_t(id,order_type,customer_id,amount) values(#{id},#{orderType},#{customerId},#{amount})")
  11. public int insert(Orders orders);
  12. @Select("select * from orders_t where id = #{id}")
  13. @Results({
  14. @Result(property = "id",column = "id"),
  15. @Result(property = "orderType",column = "order_type"),
  16. @Result(property = "customerId",column = "customer_id"),
  17. @Result(property = "amount",column = "amount")
  18. })
  19. public Orders selectOne(Integer id);
  20. @Select("select * from orders_t")
  21. @Results({
  22. @Result(property = "id",column = "id"),
  23. @Result(property = "orderType",column = "order_type"),
  24. @Result(property = "customerId",column = "customer_id"),
  25. @Result(property = "amount",column = "amount")
  26. })
  27. List<Orders> selectAll();
  28. }

5、添加yml文件

  1. mybatis:
  2. type-aliases-package: com.example.mapper
  3. mybatis-plus:
  4. mapperLocations: classpath*:mapper/*Mapper.xml
  5. spring:
  6. main:
  7. allow-bean-definition-overriding: true # 需要配置否则加载数据源报错 是否允许定义重名的bean对象覆盖原有的bean
  8. profiles:
  9. active: database
  10. sharding-sphere:
  11. datasource:
  12. names: ds1
  13. # Configuring the second database
  14. datasource.ds1:
  15. type: com.alibaba.druid.pool.DruidDataSource
  16. driver-class-name: com.mysql.jdbc.Driver
  17. url: "jdbc:mysql://127.0.0.1:3306/lable7?serverTimezone=UTC"
  18. username: root
  19. password: root
  20. # Configuring the database sharding strategy
  21. sharding:
  22. tables:
  23. orders_t:
  24. actual-data-nodes: ds1.orders_t_$->{1..2}
  25. key-generator:
  26. column: id
  27. type: SNOWFLAKE
  28. table-strategy:
  29. inline:
  30. sharding-column: id
  31. algorithm-expression: orders_t_${id%2+1}

6、添加测试方法

  1. package com.example.sharding;
  2. import com.example.sharding.domain.Orders;
  3. import com.example.sharding.mapper.OrdersMapper;
  4. import org.junit.jupiter.api.Test;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.boot.test.context.SpringBootTest;
  7. import java.util.List;
  8. @SpringBootTest
  9. class ShardingsphereDemoApplicationTests {
  10. @Autowired
  11. private OrdersMapper ordersMapper;
  12. @Test
  13. public void addOrders(){
  14. for (int i = 1; i <=10 ; i++) {
  15. Orders orders = new Orders();
  16. orders.setId(i);
  17. orders.setCustomerId(i);
  18. orders.setOrderType(i);
  19. orders.setAmount(1000.0*i);
  20. ordersMapper.insert(orders);
  21. }
  22. }
  23. @Test
  24. public void queryOrders(){
  25. // Orders orders = ordersMapper.selectOne(1);
  26. List<Orders> orderss = ordersMapper.selectAll();
  27. Orders orders = ordersMapper.selectById(1);
  28. System.out.println(orders);
  29. }
  30. }

6.1、插入数据

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

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

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读