sql-metadata-plugin
Execute SQL to return result-set metadata easily. It also provide template engine support for code-generator.
Usage
import dependency
<dependency>
<groupId>io.github.pleuvoir</groupId>
<artifactId>sql-metadata-plugin</artifactId>
<version>${latest.version}</version>
</dependency>
register in spring container
<bean class="io.github.pleuvoir.sql.MetaDataConfiguration">
<property name="ftlLocation" value="classpath:ftl" />
<!-- if you use dataSourceConfig, Be careful of database driver -->
<property name="dataSourceConfig">
<bean class="io.github.pleuvoir.sql.bean.DataSourceConfig">
<property name="driverClass" value="oracle.jdbc.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@192.168.100.8:1521:cacplat" />
<property name="username" value="auction_test" />
<property name="password" value="auction_test" />
</bean>
</property>
</bean>
junit test
@Autowired
private DBScriptRunner dBScriptRunner;
@Test
public void contextTest() throws FileNotFoundException, IOException, TemplateException {
// 1. get metaData
MetaData metaData = dBScriptRunner.excute("select * from pub_param");
String entityName = "pubParamPO";
// 2. convert to dataModel
DataModel dataModel = metaData.asDataModel().addData("entityName", entityName);
System.out.println(dataModel.toJSON());
// 3. write file ..
dataModel.write("po.ftl", "D:\\" + entityName + ".java");
}
you can realize your TypeHandlerFactory to support more database type. look like this:
dBScriptRunner.setTypeHandlerFactory(new MyTypeHandlerFactory());
dBScriptRunner.excute("select * from pub_param", "org.h2.Driver");