1 import MySQLdb 2 import os,sys 3 import string 4 dto=os.getcwd()+"\dto" 5 dao=os.getcwd()+"\dao" 6 idao=os.getcwd()+"\dao\impl" 7 service=os.getcwd()+"\service" 8 iservice=os.getcwd()+"\service\impl" 9 config=os.getcwd()+"\config" 10 11 def convertColum(name): 12 a="_"+name.replace("_", " ") 13 word=string.capwords(a).replace(" ", "").replace("_", "") 14 return word 15 16 17 def changeType(type): 18 if(type=="varchar"): 19 return "String" 20 elif(type=="int"): 21 return "Integer" 22 return "String" 23 def changeName(name): 24 return name.lower().capitalize() 25 26 def creatrFile(fileP,fileN,fileS): 27 if(os.path.exists(fileP)!=True): 28 os.makedirs(fileP) 29 f=file(fileP+"\"+fileN,'w') 30 f.write(fileS) 31 f.close() 32 33 print("begining to connect Mysql.") 34 try: 35 dbName='db' 36 connection = MySQLdb.connect(user="root",passwd="123456",host="127.0.0.1",db=dbName) 37 cursor = connection.cursor() 38 cursor.execute( "SELECT TABLE_NAME,TABLE_COMMENT FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE='BASE TABLE' AND TABLE_SCHEMA='"+dbName+"' " ) 39 40 filestr=[] 41 configstr=[] 42 for row in cursor.fetchall(): 43 tableName=row[0] 44 filestr.append('package dto; ' 45 'import java.util.Date; ' 46 'import java.util.HashMap; ' 47 'import java.util.Map; ' 48 'import data.BaseDto; ' 49 'public class '+ changeName(tableName)+'Info extends BaseDto { ' 50 ' public '+ changeName(tableName) +'Info(){ ' 51 ' this.setTbName("'+tableName+'"); ' 52 ' } ') 53 54 cursor1 = connection.cursor() 55 cursor1.execute( "SELECT COLUMN_NAME as ColumnName,DATA_TYPE as dataType FROM INFORMATION_SCHEMA.COLUMNS where table_name='"+tableName+"' and TABLE_SCHEMA='"+dbName+"'" ) 56 getsetStr=[] 57 for row1 in cursor1.fetchall(): 58 filestr.append(' private '+changeType(row1[1])+' '+convertColum(row1[0])+'; ') 59 getsetStr.append(' public String get'+changeName(convertColum(row1[0]))+'() { ' 60 ' return '+row1[0]+'; ' 61 ' } ' 62 ' public void set'+changeName(convertColum(row1[0]))+'('+changeType(row1[1])+' '+convertColum(row1[0])+') { ' 63 ' this.'+convertColum(row1[0])+' = '+convertColum(row1[0])+'; ' 64 ' } ') 65 getsetStr.append('}') 66 creatrFile(dto,changeName(tableName)+".java",''.join(filestr)+''.join(getsetStr)) 67 68 #dao 69 filestr=[] 70 filestr.append('package dao; import data.BaseDao; ' 71 'import dto.*; ' 72 'public interface I'+changeName(tableName)+'Dao implements IRepository<'+ changeName(tableName)+'Info> { ' 73 '}') 74 creatrFile(dao,"I"+changeName(tableName)+"Dao.java",''.join(filestr)) 75 76 77 filestr=[] 78 filestr.append('package dao.impl; import data.BaseDao; ' 79 'import dto.*; ' 80 'public class '+changeName(tableName)+'DaoImpl extends BaseDao<'+ changeName(tableName)+'Info> implements I'+changeName(tableName)+'Dao { ' 81 '}') 82 creatrFile(idao,""+changeName(tableName)+"DaoImpl.java",''.join(filestr)) 83 84 #service 85 filestr=[] 86 filestr.append('package Service; ' 87 'import dto.*; ' 88 'public interface I'+changeName(tableName)+'Service { ' 89 '}') 90 creatrFile(service,"I"+changeName(tableName)+"Service.java",''.join(filestr)) 91 filestr=[] 92 filestr.append('package Service.impl; import data.BaseDao; ' 93 'import Service.*; import com.google.inject.Inject; ' 94 'public class '+changeName(tableName)+'ServiceImpl { ' 95 ' @Inject ' 96 ' private I'+changeName(tableName)+'Dao '+changeName(tableName)+'Dao; ' 97 '}') 98 creatrFile(iservice,""+changeName(tableName)+"ServiceImpl.java",''.join(filestr)) 99 100 filestr=[] 101 filestr.append('package cn.uc.custom.wallpaper.config; ' 102 'import com.google.inject.*; ' 103 'import play.modules.guice.GuiceSupport; ' 104 105 'public class GuicyDummy extends GuiceSupport { ' 106 ' protected Injector configure() { ' 107 ' Injector injector = Guice.createInjector(new GuicyDummyModel()); ' 108 ' return injector; ' 109 ' } ' 110 '} ') 111 creatrFile(config,"GuicyDummy.java",''.join(filestr)) 112 113 configstr.append(" bind(I"+changeName(tableName)+"Dao.class).to("+changeName(tableName)+"DaoImpl.class).in(Singleton.class); "); 114 configstr.append(" bind(I"+changeName(tableName)+"Service.class).to("+changeName(tableName)+"ServiceImpl.class).in(Singleton.class); "); 115 filestr=[] 116 filestr.append('package cn.uc.custom.wallpaper.config; ' 117 'import com.google.inject.*; ' 118 'import play.modules.guice.GuiceSupport; ' 119 120 'public class GuicyDummy extends GuiceSupport { ' 121 ' protected Injector configure() { ' 122 ' Injector injector = Guice.createInjector(new GuicyDummyModel()); ' 123 ' return injector; ' 124 ' } ' 125 '} ') 126 creatrFile(config,"GuicyDummy.java",''.join(filestr)) 127 filestr=[] 128 filestr.append('package cn.uc.custom.wallpaper.config; ' 129 'import com.google.inject.*; ' 130 'import play.modules.guice.GuiceSupport; ' 131 132 'public class GuicyDummyModel extends AbstractModule { ' 133 ' public void configure() { ') 134 filestr.append(''.join(configstr)) 135 filestr.append(' } ' 136 '} ') 137 creatrFile(config,"GuicyDummyModel.java",''.join(filestr)) 138 cursor.close() 139 except MySQLdb.Error,e: 140 print "Mysql Error %d: %s" % (e.args[0], e.args[1]) 141 142 143 144 145
欢迎访问我的新站:学习树教育的第二入口