DBUnithtml
// https://mvnrepository.com/artifact/org.dbunit/dbunit testCompile group: 'org.dbunit', name: 'dbunit', version: '2.5.4'
import org.dbunit.DBTestCase; import org.dbunit.DatabaseUnitException; import org.dbunit.database.DatabaseConnection; import org.dbunit.database.IDatabaseConnection; import org.dbunit.database.QueryDataSet; import org.dbunit.dataset.DataSetException; import org.dbunit.dataset.IDataSet; import org.dbunit.dataset.xml.FlatXmlDataSet; import org.dbunit.dataset.xml.FlatXmlDataSetBuilder; import org.dbunit.operation.DatabaseOperation; @RunWith(SpringRunner.class) @SpringBootTest public class DBunit extends DBTestCase { @Resource DataSource dataSource; IDatabaseConnection iDatabaseConnection; @Override protected IDataSet getDataSet() throws Exception { return iDatabaseConnection.createDataSet(); } @Before public void before() throws Exception{ iDatabaseConnection = new DatabaseConnection(dataSource.getConnection()); } }
@Test public void testPartialExport() throws DataSetException, IOException { QueryDataSet queryDataSet = new QueryDataSet(iDatabaseConnection); queryDataSet.addTable("user", "select * from user"); FlatXmlDataSet.write(queryDataSet, new FileOutputStream("user.xml")); }
執行後,將會獲得一個user.xml文件,裏面記錄了數據庫user表的全部數據,看起來大概是這個樣子java
<?xml version='1.0' encoding='UTF-8'?> <dataset> <user id="1" username="mechanists" password="ABA3FC1EB2997E318E43CA099AE175CA"/> <user id="2" username="reporter" password="ABA3FC1EB2997E318E43CA099AE175CA" /> </dataset>
官網文檔地址數據庫