java映射oracle生成一些映射文件

本人使用Spring + ibatis + oracle,但是该程序跟框架梅什么关系,除了使用了Spring读取一些配置路径(可以改为直接读取文件),数据库是oracle外.

代码如下:

1. ReadField.java (生成程序)

-----------------------------------------------------------------------------

package com.util;      //包名

import java.io.File;
import java.io.IOException;
import java.util.Locale;
import java.io.*;
import java.io.FileWriter;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import java.util.Vector;

import org.springframework.dao.DataAccessException;
import org.springframework.orm.ibatis.SqlMapClientTemplate;


public class ReadField {
 
 private String filePath = loadFromFile("filePath");         //读取一些配置
 
 private String filePathXml = loadFromFile("filePathXml");
 
 private String filePathService = loadFromFile("filePathService");
 
 private String filePathDao = loadFromFile("filePathDao");
 
 private static SqlMapClientTemplate sqlTemp;
   
    private String mapTablename = loadFromFile("TableName");
     
    private static SqlMapClientTemplate getSqlMapTempInstance() {
     String  sqlTemp1 = BeanLocator.getBean("dataSource").toString();
        return sqlTemp;
     }

 public String loadFromFile(String Filepath) {                  //读取文件配置
  MsgSource msgsourceimpl = new MsgSource();
  Locale aLocaleUS = Locale.US;
  String aFileName = msgsourceimpl.readMsg("", aLocaleUS, Filepath);     
  return aFileName;
 }
 public void getMetaData() throws DataAccessException {               //获取字段名
  try {       
  Connection conn = BeanLocator.GetDB();
  Statement stmt = conn.createStatement();
  List list = getTableNames();
  for ( int i=0 ;i< list.size();i++)  
  { 
  String element = (String) list.get(i); 
  ResultSet rs = stmt.executeQuery("select * from " + element);
  ResultSetMetaData rsmd = rs.getMetaData();
  File file = new File(filePath+"/");
  if (!file.exists()) {
   file.mkdirs();
   }
  file = new File(filePath + "/" + element + ".java");
   FileWriter writer = new FileWriter(file);
   PrintWriter out = new PrintWriter(writer);
   out.println("package "+filePath.replaceAll("/",".")+";");
   out.println("import java.util.Date;");
   out.println("/*** @author ***/");
   out.println("");
   out.println("public class "+element+" {");
  for(int j=1 ;j <= rsmd.getColumnCount();j++){ 
            String ColumnsName = rsmd.getColumnName(j);
            String ColumnClassName = rsmd.getColumnClassName(j);
            String ColumnClassName1 = "String";
            if(ColumnClassName.indexOf("math.BigDecimal")!=-1)
             ColumnClassName1 = "int";
            else if(ColumnClassName.indexOf("sql.Timestamp")!=-1)
             ColumnClassName1 = "Date";
            else
             ColumnClassName1 = "String";
            out.println("private "+ColumnClassName1+" "+ColumnsName+";");
            out.println("");
            out.println("public void Set"+ColumnsName+"("+ColumnClassName1+" "+ColumnsName.toLowerCase()+"){");
            out.println("this."+ColumnsName+" = "+ColumnsName.toLowerCase()+";");
            out.println("}"); 
            out.println("");
         out.println("public "+ColumnClassName1+" Get"+ColumnsName+"(){");
            out.println("return this."+ColumnsName+"");
         out.println("}");
         out.println("");
  }
  out.println("}");
  out.flush();
  out.close();
  writer.close(); 
  
  SetUpXml(element);
  SetUpService(element);
  SetUpDao(element);
  
  }  
  } catch (DataAccessException e) {
  e.printStackTrace();
  } catch (SQLException e) {

  e.printStackTrace();
  } catch (IOException e) {

  e.printStackTrace();
  }
  }
     private void SetUpXml(String XmlName) throws DataAccessException {   //xml文件生成
  try {
   File file = new File(filePathXml+"/");
   if (!file.exists()) {
   file.mkdirs();
   }
   file = new File(filePathXml + "/" + XmlName + ".xml"); 
   FileWriter writer = new FileWriter(file);
   PrintWriter out = new PrintWriter(writer);
   out.println("<?xml version=/"1.0/" encoding=/"UTF-8/"?>");
   out.println("<!DOCTYPE sqlMap");
   out.println("   PUBLIC /"-//iBATIS.com//DTD SQL Map 2.0//EN/"");
   out.println("   /"http://www.ibatis.com/dtd/sql-map-2.dtd/">");
   out.println("<sqlMap namespace=/""+filePath+"/">");
   out.println("<typeAlias alias=/""+XmlName+"/" type=/""+filePath+"."+XmlName+"/"/>");
   out.println("");
   out.println("");
   out.println("</sqlMap>");
   out.flush();
   out.close();
   writer.close();  
  
  } catch (DataAccessException e) {
   e.printStackTrace();
  } catch (IOException e) {

  e.printStackTrace();
  } 
  }
    
     private void SetUpService(String ServiceName) throws DataAccessException {     //java文件生成
   try {
    File file = new File(filePathService+"/");
    if (!file.exists()) {
    file.mkdirs();
    }
    file = new File(filePathService + "/" + ServiceName + "Service.java"); 
    FileWriter writer = new FileWriter(file);
    PrintWriter out = new PrintWriter(writer);
    out.println("/** @author wei ** /");
    out.println("package "+filePathService.replaceAll("/",".")+";");
    out.println("import java.util.List;");
    out.println("import "+filePath.replaceAll("/",".")+"."+ServiceName+";");            
    out.println("public interface "+ServiceName+"Service{ ");
    out.println("");
    out.println("");
    out.println("}");
    out.flush();
    out.close();
    writer.close();  
   
    File fileImpl = new File(filePathService+"/Impl/");
    if (!fileImpl.exists()) {
     fileImpl.mkdirs();
    }
    file = new File(filePathService + "/Impl/" + ServiceName + "ServiceImpl.java"); 
    FileWriter writerImpl = new FileWriter(file);
    PrintWriter outImpl = new PrintWriter(writerImpl);
    outImpl.println("/** @author wei ** /");
    outImpl.println("package "+filePathService.replaceAll("/",".")+".Impl;");
    outImpl.println("import java.util.List;");            
    outImpl.println("public "+ServiceName+"ServiceImpl implements "+ServiceName+"Servcie { ");
    outImpl.println("");
    outImpl.println("");
    outImpl.println("}");
    outImpl.flush();
    outImpl.close();
    writerImpl.close(); 
   } catch (DataAccessException e) {
    e.printStackTrace();
   } catch (IOException e) {
   e.printStackTrace();
   } 
   }
    
    
     private void SetUpDao(String DaoName) throws DataAccessException {
   try {
    File file = new File(filePathDao + "/");
    if (!file.exists()) {
    file.mkdirs();
    }
    file = new File(filePathDao + "/" + DaoName + "Service.java"); 
    FileWriter writer = new FileWriter(file);
    PrintWriter out = new PrintWriter(writer);
    out.println("/** @author wei ** /");
    out.println("package "+filePathDao.replaceAll("/",".")+";");
    out.println("import java.util.List;");
    out.println("import "+filePath.replaceAll("/",".")+"."+DaoName+";");            
    out.println("public interface "+DaoName+"Service{ ");
    out.println("");
    out.println("");
    out.println("}");
    out.flush();
    out.close();
    writer.close();  
   
    File fileImpl = new File(filePathDao + "/Impl/");
    if (!fileImpl.exists()) {
     fileImpl.mkdir();
    }
    file = new File(filePathDao + "/Impl/" + DaoName + "ServiceImpl.java"); 
    FileWriter writerImpl = new FileWriter(file);
    PrintWriter outImpl = new PrintWriter(writerImpl);
    outImpl.println("/** @author wei ** /");
    outImpl.println("package " + filePathDao.replaceAll("/",".") + ".Impl;");
    outImpl.println("import java.util.List;");            
    outImpl.println("public " + DaoName+"ServiceImpl implements "+DaoName+"Servcie { ");
    outImpl.println("");
    outImpl.println("");
    outImpl.println("}");
    outImpl.flush();
    outImpl.close();
    writerImpl.close(); 
   } catch (DataAccessException e) {
    e.printStackTrace();
   } catch (IOException e) {
   e.printStackTrace();
   } 
   }
    
     private List getTableNames() throws DataAccessException {   //获取表名
  List result = new Vector();
  Connection conn;
  try {
  conn = BeanLocator.GetDB();
  DatabaseMetaData dbmd = conn.getMetaData();
  String[] types = new String[1];
  types[0] = "TABLE";
  ResultSet rs = dbmd.getTables(null, dbmd.getUserName(),"%", types); 
  while (rs.next()) {
  result.add(rs.getString("TABLE_NAME"));
  }
  rs.close();
        rs = null;
        conn.close();
        conn = null;
  } catch (SQLException e) {

  e.printStackTrace();
  }

  return result;
  }
 
 public String getMapTablename() {
  return mapTablename;
  }

  public void setMapTablename(String mapTablename) {
  this.mapTablename = mapTablename;
  }
  
 public static void main(String[] args) {
  try {
   ReadField dd = new ReadField();
                dd.getMetaData(); 
  } catch (Exception e) {
   System.out.println("wrong:" + e);
  }
 }
}

2.MsgSource.java (读取默认配置文件messages_zh_CN.properties主要是读取一些包路径)

package com.util;

import java.util.Calendar;
import java.util.Locale;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MsgSource {
 private static final String[] locations = { "application-context.xml" };  //Spring 配置文件

 private static ApplicationContext app;

 public String readMsg(String str, Locale locale, String str1) {
  if (app == null) {
   app = new ClassPathXmlApplicationContext(locations);
  }
  Object[] arg = new Object[] { str, Calendar.getInstance().getTime() };
  // Locale aLocaleUS = Locale.US;
  return str1 = app.getMessage(str1, arg, locale);
  // System.out.println("Message is ======>" + msg);
 }

 public static void main(String[] args) {
  Locale aLocaleChina = Locale.CHINA;
  Locale aLocaleUS = Locale.US;
  MsgSource impl = new MsgSource();
  String aaa = impl.readMsg(" ", aLocaleUS, "ParamIniFile");
  System.out.print("=====================/n");
  System.out.print("内容>>>>>>>>"+aaa);
  System.out.print("/n=====================");
 }
}

3.BeanLocator.java(数据库连接)

package com.util;

/**
 * @author 
 *
 * TODO To change the template for this generated type comment go to Window -
 * Preferences - Java - Code Style - Code Templates
 */
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import java.sql.Connection;

import org.springframework.jdbc.datasource.DriverManagerDataSource;

public class BeanLocator extends SqlMapClientDaoSupport{
 
 private static final String[] locations = { "application-context.xml" };

 private static ApplicationContext app;


 public static Connection GetDB(){
  Connection conn = null;
    try{ 
   ApplicationContext aaa = new ClassPathXmlApplicationContext(locations);
   DriverManagerDataSource Db =(DriverManagerDataSource) aaa.getBean("dataSource");
   conn = Db.getConnection();
    } catch (Exception e1) {
 e1.printStackTrace();
 }
         return conn; 
  }
 public static void main(String[] args) {
  BeanLocator Db = new BeanLocator();
  System.out.println(Db.GetDB());
 }
}

最后运行ReadField.java 的main.

缺点:没办法分清字段里面大小写问题.(让人不好识别).

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值