您的位置:首页 > 数据库 > Oracle

Oracle 数据库脚本解析生成相应的bean

2010-10-28 13:13 501 查看
最近工作遇到一个问题

用power design 生成的oracle sql脚本中,有几张表字段特别多,其中有一张表字段高达一百多个,平时开发需求中,我们一般都创建一个与数据库字段相对应的java bean对象,对于字段特别多的表,如果手工去建这样一个对象的话,工作量很大,很枯燥,浪费时间.

为了解决一个问题,我写了一段代码去解析oracle sql生成java bean.代码如下:

package util;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.regex.Pattern;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.SystemUtils;
import org.apache.commons.lang.WordUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class SqlScriptParser {

private static Log log = LogFactory.getLog(SqlScriptParser.class);

private static final String CREATE_TABLE_EXPRESSSION= ".*create//s+table//s+.*";

private static final String END_TABLE_EXPRESSSION= ".*//);//s*";

private static final String CLASS_NAME= "@class_name@";

private static final String FILED_LIST= "@filed_list@";

private static final String FIELD_TYPE = "@field_type@";

private static final String FIELD_NAME = "@file_name@";

private static final String FIELD_TEMPLATE = "private @field_type@ @file_name@;";

@SuppressWarnings("unchecked")
public static void parse(String filename,String output){

File file = new File(filename);
try {
List<String> list = FileUtils.readLines(file, "utf-8");

boolean active = false;
String template = getClassTpl();
StringBuilder filedbuilder = null;
String tablename = null;
for(String line : list){

if(line.contains("constraint")) continue;

line = StringUtils.strip(line);
String[] fieldProperty = line.split("//s+");
if(Pattern.matches(CREATE_TABLE_EXPRESSSION, line)){
active = true;
filedbuilder = new StringBuilder();

tablename = line.replaceAll(".*create//s+table//s+", "");
tablename = StringUtils.strip(tablename);

log.info(tablename);
tablename = tablename.replace("T_LOT_", "");//去掉前缀
tablename = tablename.toLowerCase();
tablename = WordUtils.capitalize(tablename ,new char[]{'_'});//首字线大写
template = getClassTpl().replaceAll(CLASS_NAME, tablename);
}
else if(active && fieldProperty.length >= 2){
String fieldName= fieldProperty[0].toLowerCase();
String fieldType = fieldProperty[1].toLowerCase();
if(fieldType.matches(".*number//(//d+,//d+//).*")){
fieldType = "double";
}else if(fieldType.matches(".*number//(//d//).*")){
fieldType = "int";
}else if(fieldType.matches(".*number//(//d{2,}//).*")){
fieldType = "long";
}else if(fieldType.matches(".*varchar.*") || fieldType.matches(".*char.*")){
fieldType = "String";
}else if(fieldType.matches(".*date.*")){
fieldType = "Date";
}
filedbuilder.append(FIELD_TEMPLATE.replaceAll(FIELD_TYPE, fieldType).replaceAll(FIELD_NAME, fieldName));
filedbuilder.append(SystemUtils.LINE_SEPARATOR);
}
else if(active && Pattern.matches(END_TABLE_EXPRESSSION, line)){
log.info(line);
active = false;
File outdir = new File(output);
if(!outdir.exists()){
outdir.mkdirs();
}
template = template.replaceAll(FILED_LIST, filedbuilder.toString());
File outfile = new File(output+File.separator+tablename+".java");
FileUtils.writeStringToFile(outfile, template,"utf-8");
}
}
} catch (IOException e) {
log.error(e.getMessage());
}
}

public static void main(String[] args){
parse("d://report//report.sql","d://report//bean");
}

private static String getClassTpl(){
InputStream in = SqlScriptParser.class.getClassLoader().getResourceAsStream("util/ClassTpl.xdt");
String template = null;
try {
template = IOUtils.toString(in);
} catch (IOException e) {
log.error(e.getMessage());
}finally{
IOUtils.closeQuietly(in);
}
return template;
}

}


解析过程中用到的模板文件ClassTpl.xdt如下

package com.joyveb.lottery.domain;

import java.util.*;

public class @class_name@ {

@filed_list@

}


最后,我们把代码拷贝到eclipse下,生成getter,setter就可以了,当然代码很多地方还有待改进
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐