通过java反射实现对javabean生成各种sql语句

通过java反射实现对javabean生成各种sql语句,有请大家评论,更改

Java代码 复制代码 收藏代码
  1. package com.pdt.util;
  2.  
  3. import java.lang.reflect.Field;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6.  
  7.  
  8. public class BeanUtil {
  9. /**
  10. * @param args
  11. */
  12. public static void main(String[] args) {
  13.  
  14. System.out.println(getBeanFilesList("com.pdt.bean.Dictionary"));
  15.  
  16. System.out.println(genCreateTableSql("com.pdt.bean.Dictionary"));
  17.  
  18. System.out.println(genInsertSql("com.pdt.bean.Dictionary"));
  19. }
  20.  
  21. public static String getBeanName(String bean){
  22. try {
  23. Class clz = Class.forName(bean);
  24. String clzStr = clz.toString();
  25. //得到类名
  26. String beanName = clzStr.substring(clzStr.lastIndexOf(".")+1).toLowerCase();
  27. return beanName;
  28. } catch (ClassNotFoundException e) {
  29. e.printStackTrace();
  30. return "";
  31. }
  32. }
  33.  
  34. public static List<String> getBeanPropertyList(String bean){
  35. try {
  36. Class clz = Class.forName(bean);
  37. Field[] strs = clz.getDeclaredFields();
  38. List<String> propertyList = new ArrayList<String>();
  39. for (int i = 0; i < strs.length; i++) {
  40. String protype = strs[i].getType().toString();
  41. propertyList.add(protype.substring(protype.lastIndexOf(".")+1)+"`"+strs[i].getName());
  42. }
  43. return propertyList;
  44. } catch (ClassNotFoundException e) {
  45. e.printStackTrace();
  46. return null;
  47. }
  48. }
  49.  
  50. public static String getBeanFilesList(String bean){
  51. try {
  52. Class clz = Class.forName(bean);
  53. Field[] strs = clz.getDeclaredFields();
  54. StringBuffer sb = new StringBuffer();
  55. for (int i = 0; i < strs.length; i++) {
  56. String protype = strs[i].getType().toString();
  57. if (!strs[i].getName().equals("tableName")&&!strs[i].getType().equals("List")) {
  58. sb.append(strs[i].getName()+",");
  59. }
  60. }
  61. sb.deleteCharAt(sb.toString().lastIndexOf(","));
  62. return sb.toString();
  63. } catch (ClassNotFoundException e) {
  64. e.printStackTrace();
  65. return null;
  66. }
  67. }
  68.  
  69. /**
  70. * 生成建表�Z句
  71. * @param bean
  72. * @return
  73. */
  74. public static String genCreateTableSql(String bean){
  75. List<String> beanPropertyList = getBeanPropertyList(bean);
  76. StringBuffer sb = new StringBuffer("create table wnk_pdt_"+getBeanName(bean)+"(\n");
  77. for (String string : beanPropertyList) {
  78. String[] propertys = string.split("`");
  79. if (!propertys[1].equals("tableName")&&!propertys[1].equals("param")&&!propertys[0].equals("List")) {
  80. if (propertys[1].equals("id")) {
  81. sb.append(" id bigint primary key auto_increment,\n");
  82. } else {
  83. if (propertys[0].equals("int")) {
  84. sb.append(" " + propertys[1] + " int default 0 comment '',\n");
  85. } else if (propertys[0].equals("String")) {
  86. sb.append(" " + propertys[1] + " varchar(2000) default '' comment '',\n");
  87. } else if (propertys[0].equals("double")) {
  88. sb.append(" " + propertys[1] + " double(10,2) default 0.0 comment '',\n");
  89. } else if (propertys[0].equals("Date")) {
  90. sb.append(" " + propertys[1] + " datetime comment '',\n");
  91. }
  92. }
  93. }
  94. }
  95. sb.append(")");
  96. sb.deleteCharAt(sb.lastIndexOf(","));
  97. return sb.toString();
  98. }
  99.  
  100. /**
  101. * 生成查询语句
  102. * @param bean
  103. * @return
  104. */
  105. public static String genSelectAllSql(String bean){
  106. String filesList = getBeanFilesList(bean);
  107. return "select \n "+filesList+" \n from \n wnk_pdt_"+getBeanName(bean)+"";
  108. }
  109.  
  110. /**
  111. * 生成插入语句
  112. * @param bean
  113. * @return
  114. */
  115. public static String genInsertSql(String bean){
  116. String filesList = getBeanFilesList(bean);
  117. int fl = DataUtil.getCountSonStr(filesList,",")+1;
  118. String wenhao = "";
  119. for (int i = 0; i < fl; i++) {
  120. if(i==fl-1){
  121. wenhao = wenhao+"?";
  122. }else{
  123. wenhao = wenhao+"?,";
  124. }
  125. }
  126. return "insert into wnk_pdt_"+getBeanName(bean)+"("+filesList+") values("+wenhao+")";
  127. }
  128. }
package com.pdt.util;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;


public class BeanUtil {
	/**
	 * @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();
			//得到类名
			String beanName = clzStr.substring(clzStr.lastIndexOf(".")+1).toLowerCase();
			return beanName;
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
			return "";
		}
	}
	
	public static List<String> getBeanPropertyList(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;
		}
	}
	
	/**
	 * 生成建表�Z句
	 * @param bean
	 * @return
	 */
	public static String genCreateTableSql(String bean){
		List<String> beanPropertyList =  getBeanPropertyList(bean);
		StringBuffer sb = new StringBuffer("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(10,2) default 0.0 comment '',\n");
					} else if (propertys[0].equals("Date")) {
						sb.append("   " + propertys[1] + " datetime comment '',\n");
					}
				}
			}
		}
		sb.append(")");
		sb.deleteCharAt(sb.lastIndexOf(","));
		return sb.toString();
	}
	
	/**
	 * 生成查询语句
	 * @param bean
	 * @return
	 */
	public static String genSelectAllSql(String bean){
		String filesList =  getBeanFilesList(bean);
		return "select \n "+filesList+" \n from \n wnk_pdt_"+getBeanName(bean)+"";
	}
	
	/**
	 * 生成插入语句
	 * @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+")";
	}
}

 

 

你可能感兴趣的:(java,sql语句)