Python generates Java Entity script

Source: Internet
Author: User

Generate Scripts for Java entities

Import mysqldbimport sysdbhost= ' 192.168.2.200 ' dbport=3306dbuser= ' root ' dbpassword= ' zufangbao69fc ' dbname= ' Houseloan ' dbtypes = {' int ': ' Integer ', ' bigint ': ' Long ', ' bit ': ' Boolean ', ' datetime ': ' Date ', ' decimal ': ' BigDecimal ', ' VarChar ': ' String '}defvalues = {' int ': ' 0 ', ' bigint ': ' 0L ', ' bit ': ' false ', ' datetime ': ' New Date () ', ' decimal ': ' Bigdecimal.zero ', ' varchar ': ' Stringutils.empty '}def genentity (): If Len (sys.argv) <2:print ' pls input tablena Me,eg:python entity.py T_user ' return tablename=sys.argv[1] Try:conn=mysqldb.connect (host=dbhost,use r=dbuser,passwd=dbpassword,port=dbport,charset= "UTF8") cur=conn.cursor () conn.select_db (dbname) Count =cur.execute ("SELECT * from INFORMATION_SCHEMA. COLUMNS where table_schema= ' "+ dbname +" ' and table_name= ' "+ tablename +" ' ") Results=cur.fetchall () publ Icstr = ' \ r \ n ' print ' @Entity ' print ' @Table (name = '%s ') '% tablename print ' public class%s Impleme NTS Serializable {'% Getentityname (tablename) for R in Results:print ' \t/** ' print ' \ T *%s '% (' if r[19 ] is None else r[19].replace (' \ R ', ') '. replace (' \ n ', ') ') print ' \ T */' print ' \[email protected]                (name = "%s") '% r[3] if r[16] is not None and r[16] = = ' PRI ': print ' \[email protected] '  If R[17] is not None and r[17]== ' auto_increment ': print ' \[email protected] (strategy                = Generationtype.auto) ' print ' \tprivate%s%s; '% (GetType (r[7]), GetFieldName (R[3]) Else: print ' \tprivate%s%s =%s; '% (GetType (r[7]), GetFieldName (R[3]), Getdefaultvalue (R[7], r[5])) p UBLICSTR = '%s\r\n\t/**\r\n\t * get <tt>%s</tt>%s\r\n\t */\r\n\tpublic%s get%s () {\r\n\t\treturn%s;\r\n\t} \r\n\r\n\t/**\r\n\t * Set <tt>%s</tt>%s\r\n\t */\r\n\tpublic void set%s (%s%s) {\r\n\t\tthis.%s =%s;\r\n\t} \ r \ n '% (publicstr,getfieldnAme (R[3]), "If r[19" is None else r[19].replace (' \ R ', '). replace (' \ n ', '), GetType (R[7]), Getpublicfieldname (R[3]), GetFieldName (R[3]), GetFieldName (R[3]), "If r[19" is None else r[19].replace (' \ R ', '). replace (' \ n ', '), Getpublicfieldname (R[3]), GetType (R[7]), GetFieldName (R[3]), GetFieldName (R[3]), GetFieldName (r[3])) Print PUBLICST R print '} ' Cur.close () conn.close () except Mysqldb.error, E:print "Mysql Error%d:%s"% ( E.args[0], e.args[1]) def getentityname (tablename): Entityname=tablename if Tablename.startswith (' T_ '): Entity Name = tablename[2:] entityname = Entityname.replace (' _ ', ') return '%sentity '% entitynamedef getType (type): RET    Urn dbtypes[type]def getfieldname (field): Return "%s%s"% (Field[0].lower (), Field[1:]) def getpublicfieldname (field): Return '%s%s '% (Field[0].upper (), Field[1:]) def getdefaultvalue (Type, defval): if type = = ' int ' and defval are not Non E and Len (defval) >0:return Defval    Else:return defvalues[type]genentity () 

  

Python generates Java Entity script

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.