将sql写到xml里

<query name="hql"><!-- hql用标签 -->
    		from TClassification t where t.classificationPid=:parentid
    </query>
    
    <sql-query name="sql"><!-- sql用标签 -->
    	<return alias="ss" class="com.entity.Person"></return><!-- 约束返回的实体 -->
        <return-scalar column="c" type="int"/><!-- 约束返回的实体 -->
    	<![CDATA[
    		select  
    			{person.*}
    			count(*)over() c
    		from table_person person 

    	]]>	
    </sql-query>


basedao
@Override//也可以查sql-query标签里的sql语句但如果没用return标签约束的话返回的是Object数组的list
	public List<?> findByNamedQueryAndNamedParam(String queryName,  String[] paramNames,Object[] values){
		return getHibernateTemplate().findByNamedQueryAndNamedParam(queryName, paramNames, values);
	}
	@Override//适合查写在sql-query标签里的sql
	public List<?> nameQuery(final String queryName, final String[] paramNames,final Object[] values,final int first,final int step){
		return (List<?>) getHibernateTemplate().execute(new HibernateCallback(){
			
			@Override
			public Object doInHibernate(Session session)
			throws HibernateException, SQLException {
				Query query = session.getNamedQuery(queryName);
				if(paramNames!=null&&values!=null&&paramNames.length==values.length){
					for (int i = 0; i < paramNames.length; i++) {
						query  = query.setParameter(paramNames[i], values[i]);
					}
				}
				query.setFirstResult(first);
				query.setMaxResults(step);
				if(true){
					query.setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP);
				}
				return query.list();
			}
			
		});
	}
	
	public List<?> nameQuery(final String queryName, final String paramName,final Object value,final int first,final int step){
		if(StringUtils.isNotBlank(paramName)&&value!=null){
			String[] pars={paramName};
			Object[] values={value};
			return nameQuery(queryName,pars, values, first, step);
		}
		return null;
	}

你可能感兴趣的:(sql,C++,c,xml,C#)