Skip to content

Commit

Permalink
Merge 0bcd5e6 into a03a2b6
Browse files Browse the repository at this point in the history
  • Loading branch information
taojintianxia committed Jun 6, 2020
2 parents a03a2b6 + 0bcd5e6 commit 4b07ddc
Show file tree
Hide file tree
Showing 17 changed files with 820 additions and 0 deletions.
8 changes: 8 additions & 0 deletions examples/pom.xml
Expand Up @@ -56,6 +56,8 @@

<btm.version>2.1.3</btm.version>
<seata.version>1.0.0</seata.version>

<lombok.version>1.16.20</lombok.version>

<junit.version>4.12</junit.version>
<hamcrest.version>1.3</hamcrest.version>
Expand Down Expand Up @@ -255,6 +257,12 @@
<version>${spring-framework.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
</dependencyManagement>

Expand Down
Expand Up @@ -35,5 +35,6 @@
<module>sharding-spring-boot-mybatis-example</module>
<module>sharding-spring-namespace-mybatis-example</module>
<module>sharding-spring-namespace-jpa-example</module>
<module>sharding-java-configuration</module>
</modules>
</project>
@@ -0,0 +1,44 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ 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.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>sharding-java-configuration</artifactId>
<groupId>org.apache.shardingsphere.example</groupId>
<version>5.0.0-RC1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>mybatis-example</artifactId>

<dependencies>
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-jdbc-core</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,36 @@
/*
* 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.example.shardingjdbc.sharding.javaconfiguration.mybatis;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.Reader;
import java.sql.SQLException;

public class ExampleMain {

public static void main(String... args) throws IOException, SQLException {
Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
SqlSession sqlSession = sqlSessionFactory.openSession();
}
}
@@ -0,0 +1,78 @@
/*
* 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.example.shardingjdbc.sharding.javaconfiguration.mybatis.algorithm;

import com.google.common.collect.Range;
import org.apache.shardingsphere.sharding.api.sharding.standard.PreciseShardingValue;
import org.apache.shardingsphere.sharding.api.sharding.standard.RangeShardingValue;
import org.apache.shardingsphere.sharding.api.sharding.standard.StandardShardingAlgorithm;

import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.Properties;
import java.util.Set;

public final class StandardModuloShardingDatabaseAlgorithm implements StandardShardingAlgorithm<Integer> {

@Override
public String doSharding(final Collection<String> databaseNames, final PreciseShardingValue<Integer> shardingValue) {
for (String each : databaseNames) {
if (each.endsWith(shardingValue.getValue() % 2 + "")) {
return each;
}
}
throw new UnsupportedOperationException();
}

@Override
public Collection<String> doSharding(final Collection<String> databaseNames, final RangeShardingValue<Integer> shardingValueRange) {
Set<String> result = new LinkedHashSet<>();
if (Range.closed(1, 5).encloses(shardingValueRange.getValueRange())) {
for (String each : databaseNames) {
if (each.endsWith("0")) {
result.add(each);
}
}
} else if (Range.closed(6, 10).encloses(shardingValueRange.getValueRange())) {
for (String each : databaseNames) {
if (each.endsWith("1")) {
result.add(each);
}
}
} else if (Range.closed(1, 10).encloses(shardingValueRange.getValueRange())) {
result.addAll(databaseNames);
} else {
throw new UnsupportedOperationException();
}
return result;
}

@Override
public String getType() {
return "STANDARD_TEST_DB";
}

@Override
public Properties getProperties() {
return new Properties();
}

@Override
public void setProperties(final Properties properties) {
}
}
@@ -0,0 +1,70 @@
/*
* 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.example.shardingjdbc.sharding.javaconfiguration.mybatis.algorithm;

import com.google.common.collect.Range;
import org.apache.shardingsphere.sharding.api.sharding.standard.PreciseShardingValue;
import org.apache.shardingsphere.sharding.api.sharding.standard.RangeShardingValue;
import org.apache.shardingsphere.sharding.api.sharding.standard.StandardShardingAlgorithm;

import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.Properties;
import java.util.Set;

public final class StandardModuloShardingTableAlgorithm implements StandardShardingAlgorithm<Long> {

@Override
public String doSharding(final Collection<String> tableNames, final PreciseShardingValue<Long> shardingValue) {
for (String each : tableNames) {
if (each.endsWith(shardingValue.getValue() % 2 + "")) {
return each;
}
}
throw new UnsupportedOperationException();
}

@Override
public Collection<String> doSharding(final Collection<String> tableNames, final RangeShardingValue<Long> shardingValue) {
Set<String> result = new LinkedHashSet<>();
if (Range.closed(200000000000000000L, 400000000000000000L).encloses(shardingValue.getValueRange())) {
for (String each : tableNames) {
if (each.endsWith("0")) {
result.add(each);
}
}
} else {
throw new UnsupportedOperationException();
}
return result;
}

@Override
public String getType() {
return "STANDARD_TEST_TBL";
}

@Override
public Properties getProperties() {
return new Properties();
}

@Override
public void setProperties(final Properties properties) {
}
}
@@ -0,0 +1,32 @@
/*
* 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.example.shardingjdbc.sharding.javaconfiguration.mybatis.entity;

import lombok.Data;

@Data
public class Order {

private long orderId;

private int userId;

private long addressId;

private String status;
}
@@ -0,0 +1,31 @@
/*
* 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.example.shardingjdbc.sharding.javaconfiguration.mybatis.factory;

import org.apache.ibatis.datasource.unpooled.UnpooledDataSourceFactory;

import java.sql.SQLException;

public class HikariCPDataSourceFactory extends UnpooledDataSourceFactory {

public HikariCPDataSourceFactory() throws SQLException {
dataSource = ShardingDataBaseDataSourceFactory.getDataSource();
// dataSource = ShardingTablesDataSourceFactory.getDataSource();

}
}

0 comments on commit 4b07ddc

Please sign in to comment.