Use java reflection to generate various SQL statements for javabean

Source: Internet
Author: User

Java reflection is used to generate various SQL statements for javabean. Please comment and change the Java code: package com. pdt. util; import java. lang. reflect. field; import java. util. arrayList; import java. util. list; public class BeanUtil {/** original address: http://www.software8.co/wzjs/java/2875.html * @ param args */public static void main (String [] args) {System. out. println (getBeanFilesList ("com. pdt. bean. dictionary "); System. out. println (genCreateTableSql ("com. pdt. bean. Dictionary "); System. out. println (genInsertSql ("com. pdt. bean. dictionary ");} public static String getBeanName (String bean) {try {Class clz = Class. forName (bean); String clzStr = clz. toString (); // obtain the class name String beanName = clzStr. substring (clzStr. lastIndexOf (". ") + 1 ). toLowerCase (); return beanName;} catch (ClassNotFoundException e) {e. printStackTrace (); return "" ;}} public static List <String> ge TBeanPropertyList (String bean) {try {Class clz = Class. forName (bean); Field [] strs = clz. getDeclaredFields (); List <String> propertyList = new ArrayList <String> (); for (int I = 0; I <strs. length; I ++) {String protype = strs [I]. getType (). toString (); propertyList. add (protype. substring (protype. lastIndexOf (". ") + 1) +" '"+ strs [I]. getName ();} return propertyList;} catch (ClassNotFoundException e) {e. PrintStackTrace (); return null ;}} public static String getBeanFilesList (String bean) {try {Class clz = Class. forName (bean); Field [] strs = clz. getDeclaredFields (); StringBuffer sb = new StringBuffer (); for (int I = 0; I <strs. length; I ++) {String protype = strs [I]. getType (). toString (); if (! Strs [I]. getName (). equals ("tableName ")&&! Strs [I]. getType (). equals ("List") {sb. append (strs [I]. getName () + ",") ;}} sb. deleteCharAt (sb. toString (). lastIndexOf (","); return sb. toString ();} catch (ClassNotFoundException e) {e. printStackTrace (); return null ;}/ *** generate the tabulation statement * @ param bean * @ return */public static String genCreateTableSql (String bean) {List <String> beanPropertyList = getBeanPropertyList (bean); StringBuffer sb = new StringB Uffer ("create table wnk_pdt _" + getBeanName (bean) + "(\ n"); for (String string: beanPropertyList) {String [] propertys = string. split ("'"); if (! Propertys [1]. equals ("tableName ")&&! Propertys [1]. equals ("param ")&&! Propertys [0]. equals ("List") {if (propertys [1]. equals ("id") {sb. append ("id bigint primary key auto_increment, \ n");} else {if (propertys [0]. equals ("int") {sb. append ("" + propertys [1] + "int default 0 comment'', \ n ");} else if (propertys [0]. equals ("String") {sb. append ("" + propertys [1] + "varchar (2000) default ''comment', \ n");} else if (propertys [0]. equals ("double") {sb. append (" "+ Propertys [1] +" double (0.0) default comment '', \ n");} else if (propertys [0]. equals ("Date") {sb. append ("" + propertys [1] + "datetime comment'', \ n ") ;}}} sb. append (")"); sb. deleteCharAt (sb. lastIndexOf (","); return sb. toString ();}/*** generate query statement * @ param bean * @ return */public static String genSelectAllSql (String bean) {String filesList = getBeanFilesList (bean ); return "select \ N "+ filesList +" \ n from \ n wnk_pdt _ "+ getBeanName (bean) + "";} /*** generate the insert statement * @ param bean * @ return */public static String genInsertSql (String bean) {String filesList = getBeanFilesList (bean); int fl = DataUtil. getCountSonStr (filesList, ",") + 1; String wenhao = ""; for (int I = 0; I <fl; I ++) {if (I = fl-1) {wenhao = wenhao + "? ";} Else {wenhao = wenhao + "?, ";}} Return" insert into wnk_pdt _ "+ getBeanName (bean) +" ("+ filesList +") values ("+ wenhao + ")";}}

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.