当前位置 : 主页 > 编程语言 > c++ >

java工具类sql查询语句生成

来源:互联网 收集:自由互联 发布时间:2021-07-03
gistfile1.txt /**查询语句生成器*/public static String getSelectSql(List columns,Class c){StringBuffer sb=new StringBuffer();StringBuffer whereSB=new StringBuffer();sb.append("select ");for (String string : columns) {sb.append(string.
gistfile1.txt
/**查询语句生成器*/
	public static String getSelectSql(List
 
   columns,Class c){
		StringBuffer sb=new StringBuffer();
		StringBuffer whereSB=new StringBuffer();
		sb.append("select ");
		for (String string : columns) {
			sb.append(string.replace('_', '.'));
			sb.append(" as \""+string+"\",");
		}
		sb.delete(sb.length()-1, sb.length());
		sb.append(" from ");
		//生成关联表名和where条件
		createTablesAndWhere(c,sb,whereSB);
		//去掉尾巴
		sb.delete(sb.length()-1, sb.length());
		sb.append(" where "+c.getSimpleName()+".id=?");
		whereSB.delete(whereSB.length()-3, whereSB.length());
		sb.append(whereSB);
		//装载数据
		return sb.toString();
	}
 
网友评论