自动生成查询dao方法

/**
     * 自动生成查询方法
     *
     * @param clazz 传入的持久化类 如:User.class
     * @param list 需要查询的属性  如:userId,userName
     *
     */
    public static void generateSelectDao(Class clazz,List list){
        Field[] fs = clazz.getDeclaredFields();
        StringBuffer methodBuf = new StringBuffer();
        methodBuf.append("/**/n");
        methodBuf.append(" * 根据条件查找匹配的"+clazz.getSimpleName()+"列表/n");
        methodBuf.append(" * add by 孙钢/n");
        methodBuf.append(" */n");
        methodBuf.append(" * @param o 查询条件 /n");
        methodBuf.append(" * @param start 开始的记录/n");
        methodBuf.append(" * @param fetchSize 需要查出的记录数/n");
        methodBuf.append(" * @return 查出的集合/n");
        methodBuf.append(" *//n");
        methodBuf.append("public List get"+clazz.getSimpleName()+"List("+"Map o,int start,int fetchSize){/n");
        methodBuf.append("/tStringBuffer hqlBuf = new StringBuffer();/n");
        methodBuf.append("/thqlBuf.append(/"select ");
        for(int i=0;i<list.size();i++){
            if(i!=list.size()-1){
                methodBuf.append("t."+list.get(i)+",");
            }else{
                methodBuf.append("t."+list.get(i)+" ");
            }
        }
        methodBuf.append("/");/n");
        methodBuf.append("/thqlBuf.append(/"from "+clazz.getSimpleName()+" t where 1=1 /");/n");
        methodBuf.append("/tList paraValues = new ArrayList();/n");
        methodBuf.append("/tif(o!=null){/n");
        for(int i = 0;i<fs.length;i++){
            fs[i].setAccessible(true);
            String fieldName = fs[i].getName();
            methodBuf.append("/t/tif(o.get(/""+fieldName+"/")!=null){/n");
            methodBuf.append("/t/t/thqlBuf.append(/"and t."+fs[i].getName()+" = ? /");/n");
            methodBuf.append("/t/t/tparaValues.add(o.get(/""+fieldName+"/"));/n");
            methodBuf.append("/t/t}/n");
        }
        methodBuf.append("/t}/n");
        methodBuf.append("/t//根据某个属性排序/n");
        methodBuf.append("/thqlBuf.append(/"order by t.*** desc /");/n");
        methodBuf.append("/tQuery query = createQuery(hqlBuf.toString());/n");
        methodBuf.append("/tfor(int i=0;i<paraValues.size();i++){/n");
        methodBuf.append("/t/tquery.setParameter(i,paraValues.get(i));/n");
        methodBuf.append("/t}/n");
        methodBuf.append("/t//从第start条记录开始,一共fetchSize条记录/n");
        methodBuf.append("/tquery.setFirstResult(start).setMaxResults(fetchSize);/n");
        methodBuf.append("/treturn query.list();/n");
        methodBuf.append("}/n");
        System.out.println(methodBuf.toString());
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值