java自动生成mybatis新增sql语句

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;
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值