我们在使用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;
/**