dbunit / unitils:如何导出多模式数据集?

时间:2015-07-08 12:18:10

标签: dbunit unitils

dbunit的官方教程已经为从单个数据库模式导出数据集提供了良好的example
有没有办法将不同模式的不同表导出到一个数据集中(比如schema_A中的Table_A,schema_B中的Table_B)?
当导入的数据集写入xml文件时,如下所示:

<?xml version='1.0' encoding='UTF-8'?>
<dataset schema:schemaA schema:schemaB>
    <schemaA:tableA ..... />
    <schemaA:tableA ..... />
    <schemaB:tableB ..... />
</dataset>

1 个答案:

答案 0 :(得分:2)

我刚遇到同样的问题并修复它,你需要设置FEATURE_QUALIFIED_TABLE_NAMES属性:

请参阅下面带有更改的相同示例代码(我删除了部分代码,因为我不需要完整的数据库导出):

    public static void main(String[] args) throws Exception
    {
        // database connection
        Class driverClass = Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        Connection jdbcConnection = DriverManager.getConnection(
                "jdbc:sqlserver://<server>:1433;DatabaseName=<dbName>", "<usr>", "<passwd>");
        IDatabaseConnection connection = new DatabaseConnection(jdbcConnection);

        Properties props = new Properties();
        props.put(DatabaseConfig.FEATURE_QUALIFIED_TABLE_NAMES, "true");
        connection.getConfig().setPropertiesByString(props);        


        // dependent tables database export: export table X and all tables that
        // have a PK which is a FK on X, in the right order for insertion
        String[] depTableNames = TablesDependencyHelper.getAllDependentTables( connection, "vehicle.Vehicle_Series_Model_SMA" );
        IDataSet depDataset = connection.createDataSet( depTableNames );
        FlatXmlDataSet.write(depDataset, new FileOutputStream("vehicle.Vehicle_Series_Model_SMA.xml"));          

    }