shardingsphere如何支持utf8mb4

我们在使用mysql的过程中,由于业务需求,在创建database中有时候需要支持特殊表情符号存储,比较直接简单的方式就是设置database的编码格式为utf8mb4

那么在使用shardingsphere进行分表分库的时候,如何让shardingsphere也支持utf8mb4呢?

查看shardingsphere的源码,我们可以看到shardingsphere对于datasource进行了二次封装,目前使用的shardingsphere-4.0.0-RC1这个版本中,如果需要对于utf8mb4需要我们对源码进行二次开发

1、创建springboot工程

可以直接使用Spring Initializr生成项目

2、添加依赖关系

<dependency>
    <groupId>org.apache.shardingsphere</groupId>
    <artifactId>sharding-jdbc-spring-boot-starter</artifactId>
    <version>4.0.0-RC1</version>
</dependency>

3、在application.properties文件中配置添加如下:

spring.shardingsphere.datasource.ds0.connection-init-sqls = set names utf8mb4;

4、重写org.apache.shardingsphere.shardingjdbc.spring.boot.util.DataSourceUtil

我们先把shardingsphere的DataSourceUtil的源码贴出来

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.shardingsphere.shardingjdbc.spring.boot.util;

import com.google.common.base.CaseFormat;
import com.google.common.collect.Sets;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

import javax.sql.DataSource;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.Map;
import java.util.Map.Entry;

/**
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值