cn.enilu.common.code.TableDescLoader Maven / Gradle / Ivy
package cn.enilu.common.code;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.impl.NutDao;
import org.nutz.dao.sql.Sql;
import org.nutz.dao.sql.SqlCallback;
import org.nutz.ioc.Ioc;
import org.nutz.ioc.impl.NutIoc;
import org.nutz.ioc.loader.json.JsonLoader;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 根据数据库结构生成model,service,controller,views
* 作者: zhangtao
* 创建日期: 16-7-10
*/
public class TableDescLoader extends Loader{
@Override
public Map loadTables(Ioc ioc,
String basePackageName, String baseUri,String servPackageName,String modPackageName) throws SQLException {
DataSource ds = ioc.get(DataSource.class);
Dao dao = new NutDao(ds);
Sql sql = Sqls.create("select database()");
sql.setCallback(new SqlCallback() {
@Override
public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
if (rs.next()) {
return rs.getString(1);
}
return null;
}
});
dao.execute(sql);
String database = sql.getString();
Sql tableSchemaSql = Sqls.create("select * from INFORMATION_SCHEMA.COLUMNS where TABLE_SCHEMA = '"
+ database + "'");
tableSchemaSql.setCallback(new SqlCallback() {
@Override
public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
ResultSetMetaData metaData = rs.getMetaData();
int columnCount = metaData.getColumnCount();
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy