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

代码生成工具的实现思路

程序员文章站 2022-04-06 20:19:22
...
1.利用 JDBC中的 ResultSetMetaData 和 DatabaseMetaData  来读取表字段的相关信息

DataSource dataSource = context.getBean("dataSource", DataSource.class);
		Connection conn = dataSource.getConnection();
		List<Column> lsColumns = new ArrayList<Column>(10);
		PreparedStatement stmt = conn.prepareStatement("select *  from "+tableName+" where 1=0 ");
		ResultSet resultSet = stmt.executeQuery();
		ResultSetMetaData rsmd = resultSet.getMetaData();
		int n = rsmd.getColumnCount();
		for (int i = 1; i <= n; i++)
		{
			String colName = rsmd.getColumnName(i);
			String fieldName = StringUtil.toBeanPatternStr(colName);
			Column column = new Column();
			column.setName(colName) ;
			column.setJavaName(fieldName) ;
			column.setDataType(rsmd.getColumnClassName(i));
			column.setPrecision(String.valueOf(rsmd.getPrecision(i)));
			column.setScale( String.valueOf(rsmd.getScale(i)) );
			column.setLength( String.valueOf(rsmd.getColumnDisplaySize(i)));
			column.setNullable(String.valueOf("1".equals(rsmd.isNullable(i))));

//获取列注释
			DatabaseMetaData dbmd = conn.getMetaData();
			ResultSet rs = dbmd.getColumns(null, null, tableName, null);
			while (rs.next()) {
				if (colName.equals(rs.getString("COLUMN_NAME")))
					column.setComments(rs.getString("REMARKS"));
			}
			//获取主键列
			ResultSet rs2 = dbmd.getPrimaryKeys(null, null, tableName);
			while (rs2.next()) {
				if (colName.equals(rs2.getString("COLUMN_NAME")))
					column.setColumnKey("TRUE");	
			}


2.讲表字段相关信息放入模板引擎的上下文中,根据模板内容生成JAVA文件;

	Velocity.addProperty("file.resource.loader.path", getClassPath()
				+ tmplDir);
		Template template = Velocity.getTemplate(tmplFile, encoding);
		VelocityContext tmplContext = new VelocityContext(context);
		FileUtil.createFile(absolutePath);
		PrintWriter writer = new PrintWriter(
				new FileOutputStream(absolutePath), true);
		template.merge(tmplContext, writer);
		writer.flush();
		writer.close();


3.tmplFile模板文件的内容如下:

 #if($pkResult.size()>1)
     @EmbeddedId
     #else
     @Id
     @Column(name = "$pkResult.get(0).name"  )
     @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="${TABLE_NAME}_SEQ") 
     #end
     private ${keyType}  ${keyVar} ;

     #foreach($prop in $columnResult)
        #set ($fieldName  = ${prop.javaName} )
        #set ($fieldType  = $prop.dataType )
        @Column(name = "${prop.name}" )
    	private $fieldType $fieldName ;
	
     #end


     public  ${keyType} get${KeyFieldUpper} () {
		return ${keyVar};
     }

     public void set${KeyFieldUpper} (${keyType}  ${keyVar}) {
		this.${keyVar} = ${keyVar};         
      }

     #foreach($prop in $columnResult)
        #set ($fieldName  = ${prop.javaName} )
        #set ($fieldType  =  $prop.dataType )
        #set ($innerFieldUpper  = $stringUtil.capitalize($fieldName) )
	public $fieldType get$innerFieldUpper () {
		return $fieldName;
	}
	public void set$innerFieldUpper ($fieldType $fieldName) {
		this.$fieldName = $fieldName;		
	}
     #end
  • 代码生成工具的实现思路
            
    
    博客分类: 代码生成工具JDBC jdbcresultsetmetadatadatabasemetadatacode 
  • 大小: 65.1 KB