コード例 #1
0
 /**
  * 根据表的字段名 获取分库分表选择的数据库
  *
  * @param datasourceSchema
  * @param tableNameBean
  * @return
  */
 public DatabaseSchema getShardDataSoure(
     DatasourceSchema datasourceSchema, TableNameBean tableNameBean) {
   Map<String, DatabaseSchema> map = datasourceSchema.getMap();
   List<DatabaseSchema> dataBaseSchemas = new ArrayList<DatabaseSchema>();
   String tablename = "";
   Boolean flag = false;
   for (String key : map.keySet()) { // 找出跳出循环
     List<TableSchema> tableSchemas = map.get(key).getTableSchemas();
     for (TableSchema tableSchema : tableSchemas) {
       if (tableSchema.getTablefield().equals(tableNameBean.getTablefield())) {
         tablename = tableSchema.getTablename();
         flag = true;
         break;
       }
     }
     if (flag) {
       break;
     }
   }
   Map<String, Set<String>> tablenameMapingDatasources =
       datasourceSchema.collectTablenameMapingDatasource();
   Set<String> databases = tablenameMapingDatasources.get(tablename);
   for (String database : databases) {
     dataBaseSchemas.add(datasourceSchema.getMap().get(database));
   }
   // DatabaseShard baseSchemaHashShard=new DatabaseShard();
   // return baseSchemaHashShard.getShardInfo(tableNameBean.getTablefieldvalue());
   return loadBalance.select(dataBaseSchemas, tableNameBean.getTablefieldvalue());
 }
コード例 #2
0
  /**
   * 获取分库分表选择的表名
   *
   * @param datasourceSchema
   * @param tableNameBean
   * @param databsename
   * @return
   */
  public TableBean getShardTableName(
      DatasourceSchema datasourceSchema, TableNameBean tableNameBean, String databsename) {
    Map<String, List<TableSchema>> maps = datasourceSchema.collectDataBaseMappingTablename();
    List<TableSchema> tableSchemas = maps.get(databsename);
    TableSchema result = new TableSchema();
    for (TableSchema tableSchema : tableSchemas) {
      if (tableSchema.getTablefield().equals(tableNameBean.getTablefield())) {
        result = tableSchema;
        break;
      }
    }

    List<TableBean> tableBeans = new ArrayList<TableBean>();
    for (int i = 0; i < result.getCount(); i++) {
      TableBean tableBean = new TableBean();
      tableBean.setName(result.getTablename());
      tableBean.setPrefixname(result.getPrefixname() + i);
      tableBeans.add(tableBean);
    }

    return loadBalance.select(tableBeans, tableNameBean.getTablefieldvalue());
  }