java自动生成mybatis新增sql语句
2013-09-30 16:40
781 查看
package com.hebrf.test.sql; import java.awt.Desktop; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.sql.Connection; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.sql.Statement; public class CreateSQl { /** * @param args */ public static void main(String[] args) { CreateSQl c = new CreateSQl(); c.createInsertSql("tablename"); } public void createInsertSql(String tableName) { String fileName = "D:" + File.separator + "sqlwork" + File.separator + tableName + ".txt"; File f = new File(fileName); try { if(f.exists()){ //如果文件存在则删除 f.delete(); } f.createNewFile();//创建sql文件 } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } OutputStream out; try { out = new FileOutputStream(f, true); String[][] strs = getFieldsNames(tableName); if(strs == null || strs.length == 0){ throw new Exception("不存在的表名"); } String str = "INSERT INTO "+ tableName +" "; byte[] b = str.getBytes(); for (int i = 0; i < b.length; i++) { out.write(b[i]); } String str2="("; //\r可以换行 b = str2.getBytes(); for (int i = 0; i < b.length; i++) { out.write(b[i]); } //////////添加字段 for(int i=0; i < strs.length; i++){ String temp = "\r\n\t"+strs[i][0]; if(i == (strs.length-1)){ temp+=") "; }else{ temp+=","; } b = temp.getBytes(); for (int j = 0; j < b.length; j++) { out.write(b[j]); } } String strValue=" \r\nvalues ("; //\r\n可以换行 b = strValue.getBytes(); for (int i = 0; i < b.length; i++) { out.write(b[i]); } //*******************添加字段值*************************************************************************** for(int i=0; i < strs.length; i++){ String temp = "#{"+strs[i][0]+"}"; if(strs[i][1].startsWith("DATE") || strs[i][1].startsWith("TIME")){ temp = "to_date("+temp+",'yyyy-mm-dd hh24:mi:ss')"; } String nullstr = "null"; if(i == (strs.length-1)){ temp += ") "; nullstr += ") "; }else{ temp+=","; nullstr+=","; } // if(strs[i][1].startsWith("DATE") || strs[i][1].startsWith("TIME")){ temp = "<when test=\" "+ strs[i][0]+"!=null and "+ strs[i][0]+"!='' \">"+temp+"</when>"; temp = "\r\n\t<choose>" + temp +"<otherwise>"+nullstr+"</otherwise></choose>"; // } b = temp.getBytes(); for (int j = 0; j < b.length; j++) { out.write(b[j]); } } out.close(); System.out.println(tableName + "新增语句生成成功-->"+fileName); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } try { Desktop.getDesktop().open(f); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public String[][] getFieldsNames(String tabname){ Connection conn = ConnUtil.getConn(); Statement stat = null; ResultSet rs = null; ResultSetMetaData data = null; //定义ResultSetMetaData对象 String[][] resultStrs = null; int coloumCount = 0; try { stat = conn.createStatement(); String sql = "select * from "+tabname; rs = stat.executeQuery(sql);// 查询数据 data = rs.getMetaData(); coloumCount = data.getColumnCount(); resultStrs = new String[coloumCount][2]; for(int i = 0; i < coloumCount; i++){ resultStrs[i][0] = data.getColumnName(i+1); resultStrs[i][1] = data.getColumnTypeName(i+1); } //关闭数据库资源 if(rs!=null){ rs.close(); } if(conn != null){ conn.close(); } } catch (SQLException e) { e.printStackTrace(); } return resultStrs; } }
相关文章推荐
- Java自动生成mybatis更新sql语句
- Mybatis自动生成sql语句
- SQL语句生成表结构对应的javabean及mybatis的resultMap
- JAVA自定义注解实现SQL语句自动生成DEMO
- JAVA自定义注解实现SQL语句自动生成DEMO(2)
- 关于SQL语句的自动生成
- 《关于SQL语句的自动生成!》一文的代码调用示例
- MyBatis之SQL语句映射文件增删改查和Java参数如何对应
- 【转载】利用"SQL"语句自动生成序号的两种方式。
- asp根据表单自动生成sql语句的函数
- MyBatis之SQL语句映射文件增删改查和Java参数如何对应
- 通过sql语句limit标签生成java分页
- MyBatis 自动生成mysql 表对应的javabean
- java利用反射进行简单的自动sql生成
- Java Web - SM(SpringMVC + Mybatis) 代码自动生成
- SQL SERVER 分页处理,自动识别标准SQL语句并生成适合分页的SQL语句
- 根据实体自动生成sql语句并且执行
- 利用反射自动生成SQL语句(仿Linq)
- shawl.qiu c# .net 自动生成 OleDb 数据操作语句小工具 Producing OleDbSql 1.1
- 利用Excel的vba脚本根据数据库表结构自动生成java的action,bean,dao,mode,service,xml,sql,jsp等