编写自己的代码生成工具四:数据库信息查询实现

来源:互联网 发布:wincc 用c语言? 编辑:程序博客网 时间:2024/06/04 18:51

数据库信息的查询,无非就是查询表拥有的列,列的备注以及表的主键,实现起来十分简单。

这里主要考虑的就是,如何能有一个好的扩展,在更换数据库的时候可以方便的切换而不用修改源代码。

我们先来定义一个接口,这个接口只有一个方法,就是查询表的列信息:

  1. /**
  2. * 数据库操作接口
  3. *
  4. * User: liyd
  5. * Date: 13-12-6
  6. * Time: 上午10:30
  7. */
  8. public interface DatabaseProvider {
  9. /**
  10. * 获取数据库表的信息
  11. *
  12. * @param tableName 表名
  13. * @return meta data
  14. */
  15. public List<Column> getTableMetaData(String tableName);
  16. }

Column类代码如下:

  1. /**
  2. * 列信息
  3. *
  4. * User: liyd
  5. * Date: 13-11-28
  6. * Time: 下午5:19
  7. */
  8. public class Column {
  9. /** 列名 */
  10. private String name;
  11. /** 是否主键 */
  12. private boolean isPrimaryKey;
  13. /** 列备注 */
  14. private String comment;
  15. /** 数据库类型 */
  16. private String dbType;
  17. /** jdbc类型 */
  18. private String jdbcType;
  19. /** java类型 例:String */
  20. private String javaType;
  21. /** java类型class名称例:java.lang.String */
  22. private String javaClass;
  23. getter and setter...
  24. }

定义了接口,接下来就是要实现它了,但是如果我们直接进行实现,那么数据库连接的获取关闭等操作都要在这个方法里面完成,而且每实现一个数据查询类都需要这些,将会比较麻烦。因此,我们先来一个抽象的实现:

  1. /**
  2. * 数据库操作抽象类
  3. *
  4. * User: liyd
  5. * Date: 13-12-6
  6. * Time: 上午11:13
  7. */
  8. public abstract class AbstractDatabaseProvider implements DatabaseProvider {
  9. /**
  10. * 获取数据库表的信息
  11. *
  12. * @param tableName 表名
  13. * @return meta data
  14. */
  15. @Override
  16. public List<Column> getTableMetaData(String tableName) {
  17. Connection connection = DBUtils.getDefaultConnection();
  18. return getMetaData(tableName, connection);
  19. }
  20. /**
  21. * 获取数据库表元信息
  22. *
  23. * @param tableName the table name
  24. * @param connection the connection
  25. * @return meta data
  26. */
  27. public abstract List<Column> getMetaData(String tableName, Connection connection);
  28. }

在这个抽象类中,我们统一对数据库连接进行了获取,然后再调用getMetaData(String tableName, Connection connection)方法将表名和连接对象作为参数传入,由子类来实现。

这里获取的连接即是前面在代码生成组织者GenerationOrganizer中创建打开的连接,这里只进行获取,由外围负责打开和关闭。这样做的好处是一次性生成多张表的时候,连接只打开和关闭一次。还有就是具体的实现者不用关心这个连接是哪里来的,不会迷惑于连接到底是自己创建还是从哪里获取,清晰明了。

下面贴上具体的mysql实现类:

  1. /**
  2. * mysql操作类
  3. *
  4. * User: liyd
  5. * Date: 14-1-13
  6. * Time: 上午11:40
  7. */
  8. public class MysqlProvider extends AbstractDatabaseProvider {
  9. /**
  10. * 获取数据库表元信息
  11. *
  12. * @param tableName the table name
  13. * @param connection the connection
  14. * @return meta data
  15. */
  16. @Override
  17. public List<Column> getMetaData(String tableName, Connection connection) {
  18. List<Column> columnList = new ArrayList<Column>();
  19. PreparedStatement pst = null;
  20. ResultSet rs = null;
  21. ResultSetMetaData rsd = null;
  22. try {
  23. //查询时没有数据,只返回表头信息
  24. pst = connection.prepareStatement("select * from " + tableName + " where 1=2");
  25. rsd = pst.executeQuery().getMetaData();
  26. //查询主键
  27. String primaryKey = null;
  28. pst = connection
  29. .prepareStatement("SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE WHERE CONSTRAINT_NAME='PRIMARY' and TABLE_NAME = ?");
  30. pst.setString(1, tableName.toUpperCase());
  31. rs = pst.executeQuery();
  32. if (rs.next()) {
  33. primaryKey = rs.getString(1);
  34. }
  35. //查询列备注
  36. pst = connection
  37. .prepareStatement("select column_name, column_comment from information_schema.columns where table_name = ?");
  38. pst.setString(1, tableName.toUpperCase());
  39. rs = pst.executeQuery();
  40. //先将注释放入到map再获取,防止有些列没有注释获取不对应的问题
  41. Map<String, String> commentMap = new HashMap<String, String>();
  42. while (rs.next()) {
  43. commentMap.put(rs.getString("COLUMN_NAME"), rs.getString("column_comment"));
  44. }
  45. for (int i = 1; i <= rsd.getColumnCount(); i++) {
  46. String name = rsd.getColumnName(i);
  47. String dbType = rsd.getColumnTypeName(i);
  48. String javaClass = rsd.getColumnClassName(i);
  49. String comment = commentMap.get(name);
  50. boolean b = StringUtils.equalsIgnoreCase(primaryKey, name) ? true : false;
  51. Column column = new Column();
  52. column.setName(name);
  53. column.setDbType(dbType);
  54. column.setJdbcType(dbType);
  55. column.setJavaClass(javaClass);
  56. column.setComment(comment);
  57. column.setIsPrimaryKey(b);
  58. columnList.add(column);
  59. }
  60. } catch (SQLException e) {
  61. throw new EasyCodeException(e);
  62. } finally {
  63. try {
  64. if (pst != null) {
  65. pst.close();
  66. }
  67. if (rs != null) {
  68. rs.close();
  69. }
  70. } catch (SQLException e) {
  71. //ignore
  72. }
  73. }
  74. return columnList;
  75. }
  76. }

oracle的实现类:

  1. /**
  2. * Oracle 操作类
  3. *
  4. * User: liyd
  5. * Date: 13-12-6
  6. * Time: 上午11:11
  7. */
  8. public class OracleProvider extends AbstractDatabaseProvider {
  9. /**
  10. * 获取数据库表元信息
  11. *
  12. * @param tableName the table name
  13. * @param connection the connection
  14. * @return meta data
  15. */
  16. @Override
  17. public List<Column> getMetaData(String tableName, Connection connection) {
  18. List<Column> columnList = new ArrayList<Column>();
  19. PreparedStatement pst = null;
  20. ResultSet rs = null;
  21. ResultSetMetaData rsd = null;
  22. try {
  23. //查询时没有数据,只返回表头信息
  24. pst = connection.prepareStatement("select * from " + tableName + " where 1=2");
  25. rsd = pst.executeQuery().getMetaData();
  26. //查询主键
  27. String primaryKey = null;
  28. pst = connection
  29. .prepareStatement("select col.column_name from user_constraints con, user_cons_columns col where con.constraint_name = col.constraint_name and con.constraint_type = 'P' and col.table_name = ?");
  30. pst.setString(1, tableName.toUpperCase());
  31. rs = pst.executeQuery();
  32. if (rs.next()) {
  33. primaryKey = rs.getString(1);
  34. }
  35. //查询列备注
  36. pst = connection
  37. .prepareStatement("SELECT * FROM USER_COL_COMMENTS WHERE TABLE_NAME = ?");
  38. pst.setString(1, tableName.toUpperCase());
  39. rs = pst.executeQuery();
  40. //先将注释放入到map再获取,防止有些列没有注释获取不对应的问题
  41. Map<String, String> commentMap = new HashMap<String, String>();
  42. while (rs.next()) {
  43. commentMap.put(rs.getString("COLUMN_NAME"), rs.getString("COMMENTS"));
  44. }
  45. for (int i = 1; i <= rsd.getColumnCount(); i++) {
  46. String name = rsd.getColumnName(i);
  47. String dbType = rsd.getColumnTypeName(i);
  48. String javaClass = rsd.getColumnClassName(i);
  49. String comment = commentMap.get(name);
  50. boolean b = StringUtils.equalsIgnoreCase(primaryKey, name) ? true : false;
  51. Column column = new Column();
  52. column.setName(name);
  53. column.setDbType(dbType);
  54. column.setJdbcType(dbType);
  55. column.setJavaClass(javaClass);
  56. column.setComment(comment);
  57. column.setIsPrimaryKey(b);
  58. columnList.add(column);
  59. }
  60. } catch (SQLException e) {
  61. throw new EasyCodeException(e);
  62. } finally {
  63. try {
  64. if (pst != null) {
  65. pst.close();
  66. }
  67. if (rs != null) {
  68. rs.close();
  69. }
  70. } catch (SQLException e) {
  71. }
  72. }
  73. return columnList;
  74. }
0 0
原创粉丝点击