spring boot 3 整合 shardingsphere jdbc mybatis plus 成功例子

参考:SpringBoot3+ShardingJDBC5.5.0 读写分离配置_shardingsphere-jdbc 5.5.0-CSDN博客

建议先看上面这个文章

依赖:



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        3.4.0
         
    
    com.t
    demo5
    0.0.1-SNAPSHOT
    demo5
    demo5
    
    
        
    
    
        
    
    
        
        
        
        
    
    
        17
    
    
        
            
            
                org.springframework.boot
                spring-boot-dependencies
                3.4.0
                pom
                import
            
        
    
    

        
            org.springframework.boot
            spring-boot-starter
        

        
            org.projectlombok
            lombok
            true
        

        
            com.baomidou
            mybatis-plus-spring-boot3-starter
            3.5.9
        

        
            org.apache.shardingsphere
            shardingsphere-jdbc
            5.5.0
            
                
                    org.apache.shardingsphere
                    shardingsphere-test-util
                
            
        

        
            com.zaxxer
            HikariCP
            5.1.0
        
        
            com.alibaba
            druid
            1.2.18
        
        
            mysql
            mysql-connector-java
            8.0.33
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
                
                    
                        
                            org.projectlombok
                            lombok
                        
                    
                
            
        
    


application.yml文件

spring:
  datasource:
    driver-class-name: org.apache.shardingsphere.driver.ShardingSphereDriver
    url: jdbc:shardingsphere:classpath:sharding.yaml
mybatis-plus:
  type-aliases-package: com.t.demo5.dao
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

sharding.yaml文件:resources目录下创建该文件

dataSources:
  master:
    driverClassName: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://192.168.56.200:3306/rating?useUnicode=true&characterEncoding=utf-8&allowPublicKeyRetrieval=true&useSSL=false&serverTimezone=Asia/Shanghai
    username: root
    password: 123456
    dataSourceClassName: com.zaxxer.hikari.HikariDataSource
  slave1:
    driverClassName: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://192.168.56.201:3306/rating?useUnicode=true&characterEncoding=utf-8&allowPublicKeyRetrieval=true&useSSL=false&serverTimezone=Asia/Shanghai
    username: root
    password: 123456
    dataSourceClassName: com.zaxxer.hikari.HikariDataSource
  slave2:
    driverClassName: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://192.168.56.202:3306/rating?useUnicode=true&characterEncoding=utf-8&allowPublicKeyRetrieval=true&useSSL=false&serverTimezone=Asia/Shanghai
    username: root
    password: 123456
    dataSourceClassName: com.zaxxer.hikari.HikariDataSource

rules:
  - !SINGLE
    tables:
      - "*.*"
    defaultDataSource: master
  - !READWRITE_SPLITTING
    dataSources:
      readwrite_ds:
        writeDataSourceName: master
        readDataSourceNames:
          - slave1
          - slave2
        transactionalReadQueryStrategy: PRIMARY
        loadBalancerName: random
    loadBalancers:
      random:
        type: RANDOM

props:
  sql-show: true

创建实体类

@Setter
@Getter
@Data
@TableName("t_comment")
public class Comment {
    @TableId(value = "id", type = IdType.AUTO)
    private Long id;

    private Long userId;

    private Long elementId;

    private Integer likeCount;

    private BigDecimal score;

    private String content;

    private LocalDateTime createTime;

    private LocalDateTime updateTime;
}

Mapper接口

@Mapper
public interface CommentMapper extends BaseMapper {
}

测试类

@SpringBootTest
class Demo5ApplicationTests {
    @Autowired
    private CommentMapper commentMapper;
    @Test
    void test(){
        System.out.println("===================================");
        Comment comment = new Comment();
        comment.setUserId(1L);
        comment.setElementId(1L);
        comment.setLikeCount(1);
        comment.setScore(new BigDecimal(5.5));
        comment.setContent("测试数据");
        comment.setCreateTime(LocalDateTime.now());
        comment.setUpdateTime(LocalDateTime.now());
        commentMapper.insert(comment);
        System.out.println("comment == " + comment);
        System.out.println("===================================");
    }
}

你可能感兴趣的:(spring,boot,mybatis,后端)