spring+mybatis+druid数据源+sharding-jdbc分库分表

首先我们看下如何让spring与mybatis集成,我使用的是mysql数据库,建库建表语句如下:

drop database if exists demodb00;
CREATE database demodb00 DEFAULT CHARACTER SET utf8;

CREATE TABLE demodb00.user (
  id int(11) NOT NULL AUTO_INCREMENT,
  name varchar(100) DEFAULT NULL,
  age int(11) DEFAULT NULL,
  PRIMARY KEY (id),
  UNIQUE KEY id_UNIQUE (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;


编写实体类UserEntity.java
package net.aty.spring.entity;


public class UserEntity {

    private int id;

    private String name;

    private int age;

    public UserEntity() {

    }

    public UserEntity(int id, String name, int age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {

        return name;
    }

    public void setName(String name) {
        this.name = name;
    }


    public int getId() {
        return id;
    }

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

    @Override
    public String toString() {
        return "UserEntity{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}


编写mapper类UserMapper.java
package net.aty.spring.mapper;


import net.aty.spring.entity.UserEntity;

public interface UserMapper {

    int insertOne(UserEntity user);

    UserEntity selectByPk(int id);
}


编写mapper映射文件:UserMapper.xml





    
        INSERT INTO user(id, name,age) VALUE(#{id},#{name},#{age})
    

    



编写mybatis配置文件:mybatis.xml



    
    
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        

    

  


配置druid数据源,以及整合mybatis的spring-mybatis.xml



	
    
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
            
                
                
            
        
    

    
        
        
    
    
        
        
        
    



	

    
        
        
        
        
    
    
        
        
    


编写测试代码,可以发现能够正常读取和修改数据库记录
package net.aty.spring;

import net.aty.spring.entity.UserEntity;
import net.aty.spring.mapper.UserMapper;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("META-INF/spring-mybatis.xml");

        UserMapper mapper = context.getBean(UserMapper.class);
        mapper.insertOne(new UserEntity(1, "xx", 11));
        mapper.insertOne(new UserEntity(2, "xxzzz", 11));

        System.out.println(mapper.selectByPk(1));
        System.out.println(mapper.selectByPk(2));
    }
}
spring+mybatis+druid数据源+sharding-jdbc分库分表_第1张图片
spring+mybatis+druid数据源+sharding-jdbc分库分表_第2张图片



上面我们完成了spring与mybatis的整合,随着业务的变化,数据越来越多,我们需要分库分表。我们项目使用的当当中间件:sharding-jdbc,github地址是:https://github.com/dangdangdotcom/sharding-jdbc。假设我们分为demodb00和demodb01这2个数据库,每个库分user_0和user_1这2张表。

sharding-jdbc.xml配置如下:




    
        
        
    
    
        
        
        
    

    
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
            
                
                
            
        
    

    
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
            
                
                
            
        
    

    
    

    

    

    
        
            
                
            
        
    



这个文件中定义了2个库的数据源,以及我们的分库分表规则,我是按照id分库分表的:
import com.dangdang.ddframe.rdb.sharding.api.ShardingValue;
import com.dangdang.ddframe.rdb.sharding.api.strategy.database.SingleKeyDatabaseShardingAlgorithm;

import java.util.Collection;

public class DbAlgorithm implements SingleKeyDatabaseShardingAlgorithm {

    @Override
    public String doEqualSharding(Collection collection, ShardingValue shardingValue) {
        int id = shardingValue.getValue();

        int index = id % 2;

        for (String each : collection) {
            if (each.endsWith(index + "")) {
                return each;
            }
        }
        throw new UnsupportedOperationException();
    }

    @Override
    public Collection doInSharding(Collection collection, ShardingValue shardingValue) {
        return null;
    }

    @Override
    public Collection doBetweenSharding(Collection collection, ShardingValue shardingValue) {
        return null;
    }
}
import com.dangdang.ddframe.rdb.sharding.api.ShardingValue;
import com.dangdang.ddframe.rdb.sharding.api.strategy.table.SingleKeyTableShardingAlgorithm;

import java.util.Collection;

public class TbAlgorithm implements SingleKeyTableShardingAlgorithm {


    @Override
    public String doEqualSharding(Collection availableTargetNames, ShardingValue shardingValue) {
        int id = shardingValue.getValue();

        int index = id % 2;

        for (String each : availableTargetNames) {
            if (each.endsWith(index + "")) {
                return each;
            }
        }
        throw new UnsupportedOperationException();
    }

    @Override
    public Collection doInSharding(Collection availableTargetNames, ShardingValue shardingValue) {
        return null;
    }

    @Override
    public Collection doBetweenSharding(Collection availableTargetNames, ShardingValue shardingValue) {
        return null;
    }
}


接下来我们只需要修改上面的spring-mybatis.xml,就可以使用分库分表了,不需要改动已有的代码。可以看到只需要修改数据源,就可以完成sharding-jdbc与mybatis、spring的集成,非常方便。
spring+mybatis+druid数据源+sharding-jdbc分库分表_第3张图片

修改了这个数据源后,直接运行上面的测试代码,可以看到分库分表生效了。id=1的记录插入了rbb_1这个数据源,user_1这张表;id=2的记录插入了rbb_0这个数据源,user_0这张表。


spring+mybatis+druid数据源+sharding-jdbc分库分表_第4张图片


spring+mybatis+druid数据源+sharding-jdbc分库分表_第5张图片


spring+mybatis+druid数据源+sharding-jdbc分库分表_第6张图片



至此就完成了spring与mybatis以及sharding-jdbc分库分表的集成,最后给出项目使用的pom配置:


    4.0.0
    net.aty.mybatis
    mybatis-demo
    jar
    1.0-SNAPSHOT
    mybatis-demo
    http://maven.apache.org

    
        4.3.3.RELEASE
    
    
        
            org.slf4j
            slf4j-log4j12
            1.7.21
        

        
            log4j
            log4j
            1.2.17
        
        
            org.mybatis
            mybatis
            3.4.1
        
        
            mysql
            mysql-connector-java
            5.1.26
        

        
            com.alibaba
            druid
            1.0.26
        


        
            org.mybatis
            mybatis-spring
            1.3.0
        


        
            com.dangdang
            sharding-jdbc-core
            1.3.3
        
        
            com.dangdang
            sharding-jdbc-config-spring
            1.3.3
        


        
            org.springframework
            spring-jdbc
            ${spring.version}
        
        
            org.springframework
            spring-context
            ${spring.version}
        

    

    
        
            
                org.apache.maven.plugins
                maven-compiler-plugin
                2.5.1
                
                    UTF-8
                    1.8
                    1.8
                
            
        
    



你可能感兴趣的:(分布式系统)