欢迎加入讨论群:241532755 一花一世界,一叶一菩提。码农日志开始记载键盘上的舞蹈。

第一个python

  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;\n'
 45             'import java.util.Date;\n'
 46             'import java.util.HashMap;\n'
 47             'import java.util.Map;\n'
 48             'import data.BaseDto;\n'
 49             'public class '+ changeName(tableName)+'Info extends BaseDto {\n'
 50             '    public '+ changeName(tableName) +'Info(){\n' 
 51             '        this.setTbName("'+tableName+'");\n'
 52             '    }\n')
 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])+';\n')
 59             getsetStr.append('    public String get'+changeName(convertColum(row1[0]))+'() {\n'
 60                            '        return '+row1[0]+';\n'
 61                            '    }\n'
 62                            '    public void set'+changeName(convertColum(row1[0]))+'('+changeType(row1[1])+' '+convertColum(row1[0])+') {\n'
 63                            '        this.'+convertColum(row1[0])+' = '+convertColum(row1[0])+';\n'
 64                            '    }\n')   
 65         getsetStr.append('}')   
 66         creatrFile(dto,changeName(tableName)+".java",''.join(filestr)+''.join(getsetStr))
 67       
 68          #dao
 69         filestr=[]
 70         filestr.append('package dao;\nimport data.BaseDao;\n'
 71                        'import dto.*;\n'
 72                        'public interface I'+changeName(tableName)+'Dao implements IRepository<'+ changeName(tableName)+'Info> { \n'
 73                        '}')
 74         creatrFile(dao,"I"+changeName(tableName)+"Dao.java",''.join(filestr))
 75      
 76       
 77         filestr=[]
 78         filestr.append('package dao.impl;\nimport data.BaseDao;\n'
 79                        'import dto.*;\n'
 80                        'public class '+changeName(tableName)+'DaoImpl extends BaseDao<'+ changeName(tableName)+'Info> implements I'+changeName(tableName)+'Dao { \n'
 81                        '}')
 82         creatrFile(idao,""+changeName(tableName)+"DaoImpl.java",''.join(filestr))
 83         
 84         #service
 85         filestr=[]
 86         filestr.append('package Service;\n'
 87                        'import dto.*;\n'
 88                        'public interface I'+changeName(tableName)+'Service { \n'
 89                        '}')
 90         creatrFile(service,"I"+changeName(tableName)+"Service.java",''.join(filestr))
 91         filestr=[]
 92         filestr.append('package Service.impl;\nimport data.BaseDao;\n'
 93                        'import Service.*;\nimport com.google.inject.Inject;\n'
 94                        'public class '+changeName(tableName)+'ServiceImpl  { \n'
 95                        '     @Inject\n'
 96                        '     private I'+changeName(tableName)+'Dao '+changeName(tableName)+'Dao;\n'
 97                        '}')
 98         creatrFile(iservice,""+changeName(tableName)+"ServiceImpl.java",''.join(filestr))
 99 
100         filestr=[]        
101         filestr.append('package cn.uc.custom.wallpaper.config;\n'
102                         'import com.google.inject.*;\n'
103                         'import play.modules.guice.GuiceSupport;\n'
104                         
105                         'public class GuicyDummy extends GuiceSupport {\n'
106                         '    protected Injector configure() {\n'
107                         '        Injector injector = Guice.createInjector(new GuicyDummyModel());\n'
108                         '        return injector;\n'
109                         '    }\n'
110                         '}\n')
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);\n");
114         configstr.append("        bind(I"+changeName(tableName)+"Service.class).to("+changeName(tableName)+"ServiceImpl.class).in(Singleton.class);\n");
115     filestr=[]        
116     filestr.append('package cn.uc.custom.wallpaper.config;\n'
117                         'import com.google.inject.*;\n'
118                         'import play.modules.guice.GuiceSupport;\n'
119                         
120                         'public class GuicyDummy extends GuiceSupport {\n'
121                         '    protected Injector configure() {\n'
122                         '        Injector injector = Guice.createInjector(new GuicyDummyModel());\n'
123                         '        return injector;\n'
124                         '    }\n'
125                         '}\n')
126     creatrFile(config,"GuicyDummy.java",''.join(filestr))    
127     filestr=[]        
128     filestr.append('package cn.uc.custom.wallpaper.config;\n'
129                         'import com.google.inject.*;\n'
130                         'import play.modules.guice.GuiceSupport;\n'
131                         
132                         'public class GuicyDummyModel extends AbstractModule {\n'
133                         '       public void configure() {\n')
134     filestr.append(''.join(configstr))                   
135     filestr.append('    }\n'
136                         '}\n')
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  

 欢迎访问我的新站:学习树教育的第二入口

posted @ 2013-07-23 18:30  码农日志  阅读(225)  评论(0编辑  收藏  举报