生成数mysql据库表的备注comment

来源:互联网 发布:美特斯邦威淘宝官网 编辑:程序博客网 时间:2024/05/16 08:54

jpa虽然生成表,但不能生成备注。所以

1. 建立annotation

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;


@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyFieldAnno {
public String commont(); 
}

2.类中写法

@MyFieldAnno(commont = "出库编号")
   private String inputCode;


3.写个方法,得到dojo的包中的类,批量生成备注。




import java.io.File;
import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Set;

public class OptAnnotation {
public static Connection getConnection() throws ClassNotFoundException, SQLException {  
         return getConnection("localhost:3306", "", "","");  

  
public static Connection getConnection(String ipport, String dbName,  
           String username, String password)  
           throws ClassNotFoundException, SQLException {  
        String jdbcString = null;  
        jdbcString = "jdbc:mysql://" + ipport + "/" + dbName;  
        Class.forName("com.mysql.jdbc.Driver");  
        Connection connection = null;  
        connection = DriverManager.getConnection(jdbcString, username, password);  
        return connection;  
    }  
 
 
public static void priclass(){
 


String rootpath=System.getProperty("user.dir");
String jpath=rootpath+File.separator+"src"+File.separator+"main"+File.separator+"java"+File.separator+"com"+File.separator+"lfl"+File.separator+"model";
File jfile=new File(jpath);
if(jfile.exists()){
File[] clazzFile=jfile.listFiles();
for(int k=0;k<clazzFile.length;k++){
File cf=clazzFile[k];
if(cf.isFile()){
String tbname=cf.getName().replace(".java","");
System.out.println(tbname+" tb"+k+ " = new "+tbname+"();");
System.out.println("classes.add(tb"+k+");");
}
}
}
}
}
    
 
public static void altcommont() throws SQLException, ClassNotFoundException, InterruptedException{
Connection con = getConnection();
PreparedStatement ps = null;  
       ResultSet rs = null;  
Set<Object> classes = new LinkedHashSet<Object>();
//将生成的java代码粘贴此处


TableName1 tb1 = new TableName1 ();
classes.add(tb1);
TableName2 tb2 = new TableName2 ();
classes.add(tb2);




if(classes.size()>0){
Iterator it=classes.iterator();
int ii=0;
while(it.hasNext()){
ii++;
Object cls=(Object)it.next();
String packname=cls.getClass().getName();
String cname=packname.substring(packname.lastIndexOf('.')+1,packname.length());
Field[] fieldss = cls.getClass().getDeclaredFields();
String sql = "select * from " + cname + " where 1 <> 1";  
ps = con.prepareStatement(sql);  
       rs = ps.executeQuery();  
       ResultSetMetaData md = rs.getMetaData();
       int columnCount = md.getColumnCount(); 
       System.out.println("进度: "+ii+"/"+classes.size()+" 表名:"+cname);
       Thread.sleep(1000);
       for(int m=1;m<columnCount;m++){
        String shuxing=md.getColumnName(m);
        String stype=md.getColumnTypeName(m);
        int slength=md.getColumnDisplaySize(m);
        String execusql="";
        for(int i=0;i<fieldss.length;i++){
Field field=fieldss[i];
String fieldname=field.getName();
MyFieldAnno an4field = field.getAnnotation(MyFieldAnno.class); 
if(an4field!=null){

String comment=an4field.commont();
if(null==comment){
comment="";
}
if(shuxing.equalsIgnoreCase(fieldname)){
if("VARCHAR".equals(stype) ){
if(slength>21845){
slength=1000;
}
execusql="ALTER TABLE "+cname+" MODIFY COLUMN "+ shuxing +" "+stype+"("+slength+") COMMENT '"+an4field.commont()+"'";
}else{
execusql="ALTER TABLE "+cname+" MODIFY COLUMN "+ shuxing +" "+stype+" COMMENT '"+an4field.commont()+"'";
}
ps = con.prepareStatement(execusql);  
System.out.println(execusql);
       int effrow= ps.executeUpdate();
       break;
}

}
}
       }
}
}
}
 
 
public static void main(String[] args) throws NoSuchMethodException, NoSuchFieldException, ClassNotFoundException, SQLException, InterruptedException { 


//第一步:生成class的实例化语句OptAnnotation.priclass();
//OptAnnotation.priclass();

//第二步:将语句粘贴到红色字体位置。OptAnnotation.altcommont();
OptAnnotation.altcommont();
        
        


}