努力好了,时间会给你答案。--------magic_guo
对于分库分表的概念,现在一搜一大堆,这里不做过多的赘述,只将分库分表的思路拿出来和大家分享一下;
我所整合的是spring + mybatis-plus,值得注意的是,我们既然要分库,意味着数据库肯定有很多个,所以数据源是动态的;数据具体要插入那个数据库的那张表,是通过规则计算得来的;
我使用的是很普通的分库分表规则:
即平均分配数据:
要插入的数据库编号 = 用户id后四位 % 数据库的数量;
要插入的数据表编号 = 用户id后四位 / 数据库的数量 % 单数据库中该业务表的数量;
首先我们既然使用了动态的数据源,那么我们就不再使用mybatis-plus的数据源,因此我们需要在启动类上排除掉spring默认的数据源:
@SpringBootApplication(scanbasePackages = "com.guo", exclude = DataSourceAutoConfiguration.class)
@MapperScan("com.guo.mapper1")
public class DatasourceDemoApplication {
public static void main(String[] args) {
SpringApplication.run(DatasourceDemoApplication.class, args);
}
}
接下来 我们要在配置文件中配置关于数据库的信息:
server:
port: 8014
spring:
application:
name: shop-datasource
datasource1:
driver-class-name: com.mysql.jdbc.Driver
username: root
password: root
url: jdbc:mysql://localhost:3306/2008-shop-copy?characterEncoding=utf-8
aliaes: db1
datasource2:
driver-class-name: com.mysql.jdbc.Driver
username: root
password: root
url: jdbc:mysql://localhost:3306/2008-shop-copy-02?characterEncoding=utf-8
aliaes: db2
以上,我们可以看到配置了两个数据库;
然后再写一些配置类,来读取这些数据库信息,在这里我做了一个抽取,因为后面我们是通过别名aliaes来设置数据源的,就抽取了driver-class-name、
username、password、url这些信息;
@Data
public class baseDataSourceConfig {
private String driverClassName;
private String username;
private String password;
private String url;
public HikariDataSource getDataSource() {
HikariDataSource hikariDataSource = new HikariDataSource();
hikariDataSource.setDriverClassName(this.driverClassName);
hikariDataSource.setUsername(this.username);
hikariDataSource.setPassword(this.password);
hikariDataSource.setJdbcUrl(this.url);
return hikariDataSource;
}
}
由于spring使用默认数据是HikariDataSource,所以我直接在基础类中写了一个getDataSource()方法,来返回数据源,这样两个子类也会继承此方法;
- @EqualsAndHashCode(callSuper = false) :
- 此注解表示DataSourceProperties1生成equals和hashCode方法时,要带上父类的属性;这个的注意点挺多的,要想详细了解,问问度娘;
@Data
@Configuration
@EqualsAndHashCode(callSuper = false)
@ConfigurationProperties(prefix = "spring.datasource1")
public class DataSourceProperties1 extends baseDataSourceConfig{
private String aliaes;
}
@Data
@Configuration
@EqualsAndHashCode(callSuper = false)
@ConfigurationProperties(prefix = "spring.datasource2")
public class DataSourceProperties2 extends baseDataSourceConfig{
private String aliaes;
}
然后我们开始配置数据源:
mybatis-plus操作数据库的句柄是:MybatisSqlSessionFactoryBean
此类有一个方法setDataSource(),就是设置一个确定的数据源,参数也是一个数据源;然而此时我们有两个数据源,而此方法只能接收一个数据源,那么问题了来了,怎么才能将两个数据源同时传进去?
java中,我们传递多个参数,一般使用Map或者List集合;
在spring整合jdbc中,有一个抽象类AbstractRoutingDataSource,此类本身就是一个数据源,其中有一个方法叫做setTargetDataSources(),可以传进去一个Map;这样问题就解决了,我们可以把多个数据源通过Map放到此数据源中,然后再将此数据源传递给mybatis-plus即可;
我们自定义一个动态数据源,继承此抽象类,然后再自定义一些自己的方法;
思路有了,代码很简单:
public class DynamicDataSource extends AbstractRoutingDataSource {
private static ThreadLocal threadLocal = new ThreadLocal<>();
@Override
protected Object determineCurrentLookupKey() {
return threadLocal.get();
}
public static Integer setDbName(Integer userId) {
Integer userIdSuffix = Integer.parseInt(getUserIdSuffix(userId));
// 分库规则:用户id后四位 % 数据库的数量
int num = userIdSuffix % 2;
// threadLocal.set("db" + (num + 1));
// 分表的规则:用户id后四位 / 数据库的数量 % 单个数据库中该业务表的数量
int dbIndex = userIdSuffix / 2 % 2;
return dbIndex;
}
public static String getUserIdSuffix(Integer userId) {
StringBuffer buffer = new StringBuffer(String.valueOf(userId));
if (buffer.length() < 4) {
for (int i = buffer.length(); i < 4; i++) {
buffer.insert(0, "0");
}
return buffer.toString();
} else {
return buffer.substring(buffer.length()-4);
}
}
}
解决了传递数据源的问题之后,那么问题又来了,我们怎么才能确定数据是插入那个数据库?插入那个数据表?
继承类此抽象类,我们发现必须要实现determineCurrentLookupKey()方法,此方法便是我们设置最终要程序将数据插入确定的那个数据库的,返回值与我们传进去的Map中的key息息相关;
比如此方法返回的是数据源1,则数据就插入到一号库;即此方法返回的结果决定着数据的存储走向;
那么完整的思路来了:
插入数据库之前,我们设置数据库编号-----> 到此动态数据源,进行设置------->到Mybatis-plus,通过map中的key确定数据库--------->数据插入数据库
配置动态数据源代码:
@Configuration
public class DataSourceConfig {
@Autowired
private DataSourceProperties1 dataSourceProperties1;
@Autowired
private DataSourceProperties2 dataSourceProperties2;
@Bean
public HikariDataSource dataSource1() {
return dataSourceProperties1.getDataSource();
}
@Bean
public HikariDataSource dataSource2() {
return dataSourceProperties2.getDataSource();
}
@Bean
public DynamicDataSource dynamicDataSource() {
DynamicDataSource dynamicDataSource = new DynamicDataSource();
// 设置默认的数据源
dynamicDataSource.setDefaultTargetDataSource(dataSource1());
// 将两个数据源存到一个map中,此Map类型必须是
数据库的问题解决了,那么数据表的选择则是直接传入都sql语句中的,可以参考上面的setDbName()方法,返回了要插入的数据表的编号;
测试:
实体类:
@Data
public class Order {
// 根据用户id后四位生成订单,这里使用字符串
@TableId(type = IdType.AUTO)
private String id;
private Integer uid;
private Date createTime;
private String address;
private String phone;
private String username;
private BigDecimal totalPrice;
// 支付方式:1:支付宝,2:微信,3:银行卡支付
private Integer payType;
// 订单状态:1:未支付,2:已支付,3:已取消,4:已超时,5:已发货,6:已签收
private Integer orderStatus;
@TableField(exist = false)
private List orderDetailList;
}
@Data
public class OrderDetail {
@TableId(type = IdType.AUTO)
private Integer id;
private String oid;
private Integer gid;
private Integer gcount;
private BigDecimal gprice;
private BigDecimal subtotal;
private String gname;
private String gdesc;
private String gpng;
}
Dao层:
@Component public interface OrderMapper extends baseMapper{ Integer addOrder(@Param("order") Order order, @Param("tableIndex") Integer tableIndex); void batchDelOrderDetail(@Param("odList")List orderDetailList, @Param("tableIndex") Integer tableIndex); }
mapper文件:
insert into t_order_${tableIndex} ( id, uid, create_time, address, phone, username, total_price, pay_type, order_status ) values ( #{order.id}, #{order.uid}, #{order.createTime}, #{order.address}, #{order.phone}, #{order.username}, #{order.totalPrice}, #{order.payType}, #{order.orderStatus} ) insert into t_order_detail_${tableIndex} ( oid, gid, gcount, gprice, subtotal, gname, gdesc, gpng ) values ( #{odList.oid}, #{odList.gid}, #{odList.gcount}, #{odList.gprice}, #{odList.subtotal}, #{odList.gname}, #{odList.gdesc}, #{odList.gpng} )
单元测试:
@SpringBootTest
class DatasourceDemoApplicationTests {
@Autowired
private OrderMapper orderMapper;
@Test
void contextLoads() {
Integer userId = 1234;
// 1.设置数据源, 计算出插入到那个表
Integer tableIndex = DynamicDataSource.setDbName(userId);
// 2.插入订单
com.guo.entity.Order order = new com.guo.entity.Order();
order.setId("202110161235456701");
order.setUid(userId);
order.setCreateTime(new Date());
order.setAddress("xx市xx区xx街道");
order.setPhone("13245678954");
order.setUsername("张三");
order.setTotalPrice(new BigDecimal(8888.88));
order.setPayType(1);
order.setOrderStatus(1);
Integer insert = orderMapper.addOrder(order, tableIndex);
// 3.插入订单详情
List orderDetailList =new ArrayList<>();
OrderDetail orderDetail1 = new OrderDetail();
orderDetail1.setOid(order.getId());
orderDetail1.setGid(userId);
orderDetail1.setGcount(0);
orderDetail1.setGprice(new BigDecimal(0));
orderDetail1.setSubtotal(new BigDecimal(0));
orderDetail1.setGname("Iphone13");
orderDetail1.setGdesc("256G内存 8G运存");
orderDetail1.setGpng("1.png");
OrderDetail orderDetail2 = new OrderDetail();
orderDetail2.setOid(order.getId());
orderDetail2.setGid(userId);
orderDetail2.setGcount(0);
orderDetail2.setGprice(new BigDecimal(0));
orderDetail2.setSubtotal(new BigDecimal(0));
orderDetail2.setGname("香奈儿口红");
orderDetail2.setGdesc("男人涂上,迷死女人");
orderDetail2.setGpng("2.png");
orderDetailList.add(orderDetail1);
orderDetailList.add(orderDetail2);
orderMapper.batchDelOrderDetail(orderDetailList, tableIndex);
System.out.println("插入数据成功!");
}
}
当然,解决一个问题,那么就意味着有另一个问题随之而生,虽然我们分库分表,解决了数据库的压力,优化了性能;但是在数据分库分表,对于增删改查,就会有更多的业务逻辑;
问题:
1、分库之后,跨库执行SQL语句,牵涉到分布式事务问题;
2、分库分表后,表之间的关联操作将受到限制,就无法join位于不同分库的表,也无法join分表粒度不同的表, 结果原本一次查询能够完成的业务,可能需要多次查询才能完成;
3、分表分库之后,数据迁移的问题等,这些都是要面临的问题;
那么我们生来,又何尝不是在一直解决问题呢?加油!
就这样吧。
本文章教学视频来自:https://www.bilibili.com/video/BV1tb4y1Q74E?p=3&t=125
静下心,慢慢来,会很快!



