欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

通过JDBC获取表结构生成javaBean

程序员文章站 2024-03-21 08:50:40
...

闲着没事,写了一个从表生成javabean的工具类,大家可以在此基础进行发散。

 

第一步建立jdbc数据库连接

/**
	 * 获取连接
	 * @return
	 */
	public static Connection getConnection(){
		try {
			Class.forName("oracle.jdbc.OracleDriver");
			String url = "******";
			String user = "******";
			String password = "******";
			Properties properties = new Properties();
			properties.put("user", user);
			properties.put("password", password);
			properties.put("remarksReporting","true");//想要获取数据库结构中的注释,这个值是重点
			return DriverManager.getConnection(url,properties);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

 第二步:获取表结构信息

/**
	 * 获取表结构
	 * @param tableName
	 * @return
	 */
	public static List<ColumnModel> getTableStructure(String tableName){
		List<ColumnModel> columnModelList = new ArrayList<ColumnModel>();
		try {
			//TODO 表相关
			//ResultSet tableSet = metaData.getTables(null, "%",tableName,new String[]{"TABLE"}); 
			//TODO 字段相关
			ResultSet columnSet = getConnection().getMetaData().getColumns(null,"%",tableName,"%");
			ColumnModel columnModel = null;
			while(columnSet.next()){
				columnModel = new ColumnModel();
				columnModel.setColumnName(columnSet.getString("COLUMN_NAME"));
				columnModel.setColumnSize(columnSet.getInt("COLUMN_SIZE"));
				columnModel.setDataType(columnSet.getString("DATA_TYPE"));
				columnModel.setRemarks(columnSet.getString("REMARKS"));
				columnModel.setTypeName(columnSet.getString("TYPE_NAME"));
				String columnClassName = ColumnTypeEnum.getColumnTypeEnumByDBType(columnModel.getTypeName());
				String fieldName = getFieldName(columnModel.getColumnName());
				String fieldType = Class.forName(columnClassName).getSimpleName();
				columnModel.setFieldName(fieldName);
				columnModel.setColumnClassName(columnClassName);
				columnModel.setFieldType(fieldType);
    			columnModelList.add(columnModel);
    			//System.out.println(columnModel.toString());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return columnModelList;
	}

	/**
	 * 将数据库字段转换成bean属性
	 * @param columnName
	 * @return
	 */
	private static String getFieldName(String columnName) {
		char[]  columnCharArr = columnName.toLowerCase().toCharArray();
		StringBuffer sb = new StringBuffer();
		int ad = -1;
		for (int i = 0; i < columnCharArr.length; i++) {
			  char cur = columnCharArr[i];
			  if(cur=='_'){
				  ad = i;
			  }else{
				  if((ad+1)==i&&ad!=-1){
					  sb.append(Character.toUpperCase(cur));
				  }else{
					  sb.append(cur);
				  }
				  ad=-1;
			  }
		}
		return sb.toString();
	}
	
	

	public static void main(String[] args) {
		getTableStructure("T_user");
	}

 第三,生成javaBean

/**
	 * 从表结构中去生成javabean
	 * @param structureList
	 * @param beanName
	 * @return
	 */
	public static String genJavaBeanFromTableStructure(List<ColumnModel> columnModelList,String beanName){
		StringBuffer sb = new StringBuffer();
		try {
			sb.append("public class "+toFirstCharUpCase(beanName)+" {\r\n");
			for (ColumnModel columnModel : columnModelList) {
				if(StringUtils.isNotBlank(columnModel.getRemarks())){
					sb.append("	//"+columnModel.getRemarks()+" \r\n");
				}
				sb.append("	private "+columnModel.getFieldType()+" "+columnModel.getFieldName()+";\r\n");
			}
			sb.append("\r\n");
			//get set
			for (ColumnModel columnModel : columnModelList) {
				sb.append(
						"\tpublic String get"+toFirstCharUpCase((String) columnModel.getFieldName())+"() {\r\n" +
						"\t\treturn "+columnModel.getFieldName()+";\r\n" + 
						"\t}\r\n" + 
						"\r\n" + 
						"\tpublic void set"+toFirstCharUpCase((String) columnModel.getFieldName())+"(String "+columnModel.getFieldName()+") {\r\n" + 
						"\t\t"+columnModel.getFieldName()+" = "+columnModel.getFieldName()+";\r\n" + 
						"\t}\r\n\r\n");
			}
			sb.append("}\r\n");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return sb.toString();
	}
	
	/**
	 * 将首字母变大写
	 * @param str
	 * @return
	 */
	public static String toFirstCharUpCase(String str){
		char[]  columnCharArr = str.toCharArray();
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < columnCharArr.length; i++) {
			  char cur = columnCharArr[i];
			  if(i==0){
				  sb.append(Character.toUpperCase(cur));
			  }else{
				  sb.append(cur);
			  }
		}
		return sb.toString();
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		List<ColumnModel> columnModelList = JdbcUtil.getTableStructure("T_User");
		System.out.println(genJavaBeanFromTableStructure(columnModelList,"User"));

	}

 列模型:ColumnModel.java

package com.pingan.bean;

/**
 * 列模型
 * @author LUSHUIFA
 */
public class ColumnModel {
	private String columnName;
	private String dataType;
	private String typeName;
	private String columnClassName;
	private String fieldName;
	private String fieldType;
	private int columnSize;
	private String columnDef;
	private String remarks;

	public String getColumnName() {
		return columnName;
	}

	public void setColumnName(String columnName) {
		this.columnName = columnName;
	}

	public String getDataType() {
		return dataType;
	}

	public void setDataType(String dataType) {
		this.dataType = dataType;
	}

	public String getTypeName() {
		return typeName;
	}

	public void setTypeName(String typeName) {
		this.typeName = typeName;
	}

	public int getColumnSize() {
		return columnSize;
	}

	public void setColumnSize(int columnSize) {
		this.columnSize = columnSize;
	}

	public String getRemarks() {
		return remarks;
	}

	public void setRemarks(String remarks) {
		this.remarks = remarks;
	}

	@Override
	public String toString() {
		return "ColumnModel [columnName=" + columnName + ", dataType="
				+ dataType + ", typeName=" + typeName + ", columnClassName="
				+ columnClassName + ", fieldName=" + fieldName + ", fieldType="
				+ fieldType + ", columnSize=" + columnSize + ", columnDef="
				+ columnDef + ", remarks=" + remarks + "]";
	}

	public String getColumnDef() {
		return columnDef;
	}

	public void setColumnDef(String columnDef) {
		this.columnDef = columnDef;
	}

	public String getColumnClassName() {
		return columnClassName;
	}

	public void setColumnClassName(String columnClassName) {
		this.columnClassName = columnClassName;
	}

	public String getFieldName() {
		return fieldName;
	}

	public void setFieldName(String fieldName) {
		this.fieldName = fieldName;
	}

	public String getFieldType() {
		return fieldType;
	}

	public void setFieldType(String fieldType) {
		this.fieldType = fieldType;
	}

}

 数据类型枚举:ColumnTypeEnum.java

package com.pingan.Enum;


/**
 * 数据库类型枚举
 * @author LUSHUIFA
 *
 */
public enum ColumnTypeEnum {
	 VARCHAR2("VARCHAR2","java.lang.String"),
	 NUMBER("NUMBER","java.lang.Double"),
	 DATE("DATE","java.lang.String"),
	 CHAR("CHAR","java.lang.String");
     
     private String dbType;
     private String javaType;
     
     ColumnTypeEnum(String dbType,String javaType){
    	 this.dbType = dbType;
    	 this.javaType = javaType;
     }
     
     public static String getColumnTypeEnumByDBType(String dbType){
         for(ColumnTypeEnum columnTypeEnum:ColumnTypeEnum.values()){
             if(columnTypeEnum.getDbType().equals(dbType)){
                 return columnTypeEnum.getJavaType();
             }
         }
		return "";
     }

	public String getDbType() {
		return dbType;
	}

	public void setDbType(String dbType) {
		this.dbType = dbType;
	}

	public String getJavaType() {
		return javaType;
	}

	public void setJavaType(String javaType) {
		this.javaType = javaType;
	}
}