Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
820 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
...gsphere-jdbc-example/sharding-example/sharding-java-configuration/mybatis-example/pom.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
36 changes: 36 additions & 0 deletions
36
...e/shardingsphere/example/shardingjdbc/sharding/javaconfiguration/mybatis/ExampleMain.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |
78 changes: 78 additions & 0 deletions
78
...sharding/javaconfiguration/mybatis/algorithm/StandardModuloShardingDatabaseAlgorithm.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) { | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
...bc/sharding/javaconfiguration/mybatis/algorithm/StandardModuloShardingTableAlgorithm.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) { | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
.../shardingsphere/example/shardingjdbc/sharding/javaconfiguration/mybatis/entity/Order.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
31 changes: 31 additions & 0 deletions
31
...le/shardingjdbc/sharding/javaconfiguration/mybatis/factory/HikariCPDataSourceFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
|
||
} | ||
} |
Oops, something went wrong.