您的位置:首页 > 编程语言 > Java开发

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;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: