Skip to content

Commit

Permalink
Update spring namespace and yaml config examples and docs.
Browse files Browse the repository at this point in the history
  • Loading branch information
haocao committed Apr 13, 2016
1 parent aff0fee commit b02b266
Show file tree
Hide file tree
Showing 21 changed files with 417 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,21 @@
<xsd:import namespace="http://www.springframework.org/schema/beans" schemaLocation="http://www.springframework.org/schema/beans/spring-beans.xsd"/>
<xsd:element name="data-source">
<xsd:complexType>
<xsd:sequence>
<xsd:all>
<xsd:element ref="sharding-rule" minOccurs="1" maxOccurs="1"/>
<xsd:element ref="props" minOccurs="0" maxOccurs="1"/>
</xsd:sequence>
</xsd:all>
<xsd:attribute name="id" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
<xsd:element name="sharding-rule">
<xsd:complexType>
<xsd:sequence>
<xsd:all>
<xsd:element ref="table-rules" minOccurs="1" maxOccurs="1"/>
<xsd:element ref="binding-table-rules" minOccurs="0" maxOccurs="1"/>
<xsd:element ref="default-database-strategy" minOccurs="0" maxOccurs="1"/>
<xsd:element ref="default-table-strategy" minOccurs="0" maxOccurs="1"/>
</xsd:sequence>
</xsd:all>
<xsd:attribute name="data-sources" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
Expand Down
24 changes: 12 additions & 12 deletions sharding-jdbc-doc/content/post/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ dataSource: 数据源配置

tables: 分库分表配置,可配置多个logic_table_name
<logic_table_name>: 逻辑表名
actualTables: 真实表名,多个表以逗号分隔,支持inline表达式
actualTables: 真实表名,多个表以逗号分隔,支持inline表达式,指定数据源需要加前缀,不加前缀为默认数据源
databaseStrategy: 分库策略
shardingColumns: 分片列名,多个列以逗号分隔
algorithmClassName: 分库算法全类名,该类需使用默认的构造器或者提供无参数的构造器,与algorithmExpression出现一个即可
Expand Down Expand Up @@ -216,9 +216,9 @@ props: 属性配置(可选)
| *名称* | 类型 | *数据类型* | *必填*| *说明* |
| -------------------- | -------- | --------- | ------| ----- |
| logic-table | 属性 | String | 是 | 逻辑表名 |
| sharding-columns | 属性 | String | 是 | 分片列名,多个列以逗号分隔 |
| database-strategy | 属性 | String | | 分库策略,对应`<rdb:strategy>`中分库策略id |
| table-strategy | 属性 | String | | 分表策略,对应`<rdb:strategy>`中分表策略id |
actual-tables | 属性 | String | 是 | 真实表名,多个表以逗号分隔,支持inline表达式,指定数据源需要加前缀,不加前缀为默认数据源 指定数据源需要加前缀,不加前缀为默认数据源 |
| database-strategy | 属性 | String | | 分库策略,对应`<rdb:strategy>`中分库策略id, 如果不填需配置`<rdb:default-database-strategy/>` |
| table-strategy | 属性 | String | | 分表策略,对应`<rdb:strategy>`中分表策略id, 如果不填需配置`<rdb:default-table-strategy/>` |

### \<rdb:binding-table-rules/>

Expand Down Expand Up @@ -261,14 +261,14 @@ props: 属性配置(可选)

### \<rdb:props/\>

| *名称* | 类型 | *数据类型* | *必填*| *说明* |
| ------------------------------------ | ------------ | ---------- | -----| -------------------------------- |
| metrics.enable | 属性 | boolean | 否 | 是否开启度量采集,默认为false不开启 |
| metrics.millisecond.period | 属性 | String | 否 | 度量输出周期,单位为秒 |
| metrics.package.name | 属性 | String | 否 | 度量输出在日志中的标识名称 |
| executor.min.idle.size | 属性 | int | 否 | 最小空闲工作线程数量 |
| executor.max.size | 属性 | int | 否 | 最大工作线程数量 |
| executor.max.idle.timeout.millisecond| 属性 | int | 否 | 工作线程空闲时超时时间,默认以秒为单位 |
| *名称* | 类型 | *数据类型* | *必填*| *说明* |
| ------------------------------------ | ------------ | ---------- | -----| ------------------------------------|
| metrics.enable | 属性 | boolean | 否 | 是否开启度量采集,默认为false不开启 |
| metrics.millisecond.period | 属性 | String | 否 | 度量输出周期,单位为毫秒 |
| metrics.package.name | 属性 | String | 否 | 度量输出在日志中的标识名称 |
| executor.min.idle.size | 属性 | int | 否 | 最小空闲工作线程数量 |
| executor.max.size | 属性 | int | 否 | 最大工作线程数量 |
| executor.max.idle.timeout.millisecond| 属性 | int | 否 | 工作线程空闲时超时时间,默认以毫秒为单位 |

### Spring格式特别说明
如需使用inline表达式,需配置`ignore-unresolvable`为`true`,否则placeholder会把inline表达式当成属性key值导致出错.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,26 @@
* </p>
*/

package com.dangdang.ddframe.rdb.sharding.example.config;
package com.dangdang.ddframe.rdb.sharding.example.config.spring;

import java.sql.SQLException;
import com.dangdang.ddframe.rdb.sharding.example.config.service.FooService;
import com.dangdang.ddframe.rdb.sharding.example.config.spring.service.ConfigService;
import com.dangdang.ddframe.rdb.sharding.example.config.spring.service.OrderService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public final class SpringNamespaceMain {
import java.sql.SQLException;

public final class SpringNamespaceWithAssignedDataSourceMain {

public static void main(final String[] args) throws SQLException {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("META-INF/applicationContext.xml");
FooService service = applicationContext.getBean(FooService.class);
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("META-INF/applicationContextWithAssignedDataSource.xml");
OrderService service = applicationContext.getBean(OrderService.class);
service.insert();
service.select();
service.delete();
service.select();

ConfigService configService = applicationContext.getBean(ConfigService.class);
configService.select();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* Copyright 1999-2015 dangdang.com.
* <p>
* Licensed 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.
* </p>
*/

package com.dangdang.ddframe.rdb.sharding.example.config.spring;

import java.sql.SQLException;

import com.dangdang.ddframe.rdb.sharding.example.config.spring.service.ConfigService;
import com.dangdang.ddframe.rdb.sharding.example.config.spring.service.OrderService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public final class SpringNamespaceWithDefaultDataSourceMain {

public static void main(final String[] args) throws SQLException {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("META-INF/applicationContextWithDefaultDataSource.xml");
OrderService orderService = applicationContext.getBean(OrderService.class);
orderService.insert();
orderService.select();
orderService.delete();
orderService.select();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* </p>
*/

package com.dangdang.ddframe.rdb.sharding.example.config.algorithm;
package com.dangdang.ddframe.rdb.sharding.example.config.spring.algorithm;

import com.dangdang.ddframe.rdb.sharding.api.ShardingValue;
import com.dangdang.ddframe.rdb.sharding.api.strategy.table.SingleKeyTableShardingAlgorithm;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* Copyright 1999-2015 dangdang.com.
* <p>
* Licensed 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.
* </p>
*/

package com.dangdang.ddframe.rdb.sharding.example.config.spring.repository;

public interface ConfigRepository {

void select();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* Copyright 1999-2015 dangdang.com.
* <p>
* Licensed 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.
* </p>
*/

package com.dangdang.ddframe.rdb.sharding.example.config.spring.repository;

import com.dangdang.ddframe.rdb.sharding.spring.datasource.SpringShardingDataSource;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

@Repository
public class ConfigRepositoryImpl implements ConfigRepository {

@Resource
private SpringShardingDataSource shardingDataSource;

@Override
public void select() {
String sql = "SELECT c.* FROM t_config c";
try (
Connection conn = shardingDataSource.getConnection();
PreparedStatement preparedStatement = conn.prepareStatement(sql)) {
try (ResultSet rs = preparedStatement.executeQuery()) {
while(rs.next()) {
System.out.println("configName:" + rs.getString(2) + ",configValue:" + rs.getString(3));
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
* </p>
*/

package com.dangdang.ddframe.rdb.sharding.example.config.service;
package com.dangdang.ddframe.rdb.sharding.example.config.spring.repository;

public interface FooService {
public interface OrderRepository {

void insert();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* </p>
*/

package com.dangdang.ddframe.rdb.sharding.example.config.repository;
package com.dangdang.ddframe.rdb.sharding.example.config.spring.repository;

import com.dangdang.ddframe.rdb.sharding.spring.datasource.SpringShardingDataSource;
import org.springframework.stereotype.Repository;
Expand All @@ -25,7 +25,7 @@
import java.sql.ResultSet;

@Repository
public class FooRepositoryImpl implements FooRepository {
public class OrderRepositoryImpl implements OrderRepository {

@Resource
private SpringShardingDataSource shardingDataSource;
Expand Down Expand Up @@ -76,7 +76,7 @@ public void delete() {
}

@Override
public void select(){
public void select() {
String sql = "SELECT i.* FROM t_order o JOIN t_order_item i ON o.order_id=i.order_id WHERE o.user_id=? AND o.order_id=?";
try (
Connection conn = shardingDataSource.getConnection();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* Copyright 1999-2015 dangdang.com.
* <p>
* Licensed 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.
* </p>
*/

package com.dangdang.ddframe.rdb.sharding.example.config.spring.service;

public interface ConfigService {

void select();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* Copyright 1999-2015 dangdang.com.
* <p>
* Licensed 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.
* </p>
*/

package com.dangdang.ddframe.rdb.sharding.example.config.spring.service;

import com.dangdang.ddframe.rdb.sharding.example.config.spring.repository.ConfigRepository;
import com.dangdang.ddframe.rdb.sharding.example.config.spring.repository.OrderRepository;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class ConfigServiceImpl implements ConfigService {

@Resource
private ConfigRepository configRepository;

@Override
public void select() {
configRepository.select();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
* </p>
*/

package com.dangdang.ddframe.rdb.sharding.example.config.repository;
package com.dangdang.ddframe.rdb.sharding.example.config.spring.service;

public interface FooRepository {
public interface OrderService {

void insert();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,35 +15,35 @@
* </p>
*/

package com.dangdang.ddframe.rdb.sharding.example.config.service;
package com.dangdang.ddframe.rdb.sharding.example.config.spring.service;

import com.dangdang.ddframe.rdb.sharding.example.config.repository.FooRepository;
import com.dangdang.ddframe.rdb.sharding.example.config.spring.repository.OrderRepository;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class FooServiceImpl implements FooService {
public class OrderServiceImpl implements OrderService {

@Resource
private FooRepository fooRepository;
private OrderRepository orderRepository;

@Override
public void insert() {
try {
fooRepository.insert();
orderRepository.insert();
} catch (Exception ex) {
ex.printStackTrace();
}
}

@Override
public void delete() {
fooRepository.delete();
orderRepository.delete();
}

@Override
public void select() {
fooRepository.select();
orderRepository.select();
}
}
Loading

0 comments on commit b02b266

Please sign in to comment.