码迷,mamicode.com
首页 > 其他好文 > 详细

2323

时间:2015-06-02 20:19:01      阅读:265      评论:0      收藏:0      [点我收藏+]

标签:232

#1234# mybatis;


#12345678# #123#.io.File;

#12345678# #123#.sql.Connection;

#12345678# #123#.sql.DriverManager;

#12345678# #123#.sql.SQLException;

#12345678# #123#.sql.Statement;

#12345678# #123#.util.ArrayList;

#12345678# #123#.util.List;


#12345678# org.dom4j.Document;

#12345678# org.dom4j.DocumentException;

#12345678# org.dom4j.Element;

#12345678# org.dom4j.io.SAXReader;

#12345678# org.xml.sax.SAXException;


#123456# #12345# DataMybatis {

#1234567# static String filePath = "D:"+File.separator+"wangwei"+File.separator+"project"+File.separator+"xiaoyu"+File.separator

+"src"+File.separator+"com"+File.separator+"ebiz"+File.separator+"xiaoyu"+File.separator+"dao"+File.separator+"ibatis"+File.separator+"maps"+File.separator+"";

#1234567# static final String DBDRIVER = "com.mysql.jdbc.Driver";   

#1234567# static final String DBURL = "jdbc:mysql://10.20.12.175:3306/test";   

#1234567# static final String USERNAME = "test";   

#1234567# static final String PASSWORD = "test123"; 


@SuppressWarnings("unchecked")

#1234567# static List<TableInfo> getElement(Element doc, String nodeName) {

List<Element> eleList = doc.elements("resultMap");

Element ele = eleList.get(0);

List<Element> resultList = ele.elements();

List<TableInfo> list = new ArrayList<TableInfo>();

for (Element result : resultList) {

// System.out.println(result.attributeValue("column") + ";" + result.attributeValue("jdbcType"));

TableInfo tableInfo = new TableInfo(result.attributeValue("column"),result.attributeValue("jdbcType"));

list.add(tableInfo);

}

return list;

}


#123456# static void main(String[] argv) throws SQLException, ClassNotFoundException {

 Class.forName(DBDRIVER); 

 Connection conn = DriverManager.getConnection(DBURL,USERNAME,PASSWORD);

 List<String> sqlList = getSqlList();

 for(String sql:sqlList){

 createTable(conn,sql.split(";")[0]);

 createTable(conn,sql.split(";")[1]);

 }

}

#1234567# static List<String> getSqlList(){

Document doc = null;

List<String> sqlList = new ArrayList<String>();

try {

SAXReader reader = new SAXReader();

reader.setValidation(false);

reader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd",false);

File file = new File(filePath);

File[] fileList = file.listFiles();

for(File f:fileList){

if(f.getName().endsWith("xml")){

// System.out.println("--------"+f.getName()+"-----------");

doc = reader.read(f);

Element root = doc.getRootElement();

List<TableInfo> list = getElement(root, "");

StringBuffer sb = new StringBuffer();

sb.append("DROP TABLE IF EXISTS " + f.getName().split("\\.")[0].replace("_SqlMap", "") + ";");

sb.append("CREATE TABLE " + f.getName().split("\\.")[0].replace("_SqlMap", "") + " ( ");

for(TableInfo tb:list){

sb.append("").append(tb.getColoumn()).append("");

if("ID".equals(tb.getColoumn().toUpperCase())){

sb.append(" int").append(" auto_increment primary key,");

}else if("VARCHAR".equals(tb.getColoumnType().toUpperCase())){

sb.append(" varchar(255)").append(",");

}else if("INTEGER".equals(tb.getColoumnType().toUpperCase())){

sb.append(" int").append(",");

}else if("INTEGER".equals(tb.getColoumnType().toUpperCase())){

sb.append(" int)").append(",");

}else if("LONGVARCHAR".equals(tb.getColoumnType().toUpperCase())){

sb.append(" text").append(",");

}else if("TIMESTAMP".equals(tb.getColoumnType().toUpperCase())){

sb.append(" date").append(",");

}else if("DECIMAL".equals(tb.getColoumnType().toUpperCase())){

sb.append(" decimal(10,5)").append(",");

}else{

System.err.println(tb.getColoumnType());

}

}

String sqlStr = sb.toString();

sqlStr = sqlStr.substring(0, sqlStr.length() - 1);

sqlStr += ");";

// System.out.println(sqlStr);

sqlList.add(sqlStr);

}

}

} catch (DocumentException e) {

e.printStackTrace();

} catch (SAXException e) {

e.printStackTrace();

}

return sqlList;

}

#1234567# static void createTable(Connection conn, String sql) throws SQLException {

Statement stmt = null;

stmt = conn.createStatement();

stmt.executeUpdate(sql);

stmt.close();

}


}


2323

标签:232

原文地址:http://4233497.blog.51cto.com/4223497/1657596

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!