Hibernate实现xml的分页查询

第二种:读取xml文件的sql数据


1.特征:


String SQL = "";
String fileName = "/sql/Authorize.xml";


String realPath = getRequest().getSession().getServletContext()
.getRealPath(fileName);
SQL = getSqlText("sql", realPath, "GetAuthorize");


QueryResult<Object[]> q = q = authorizedsignatureManager.getScrollDateByXmlSQL(startIndex,maxIndex, SQL, paramValues.toArray())




2.实现类


/**
 * 
 * 解析xml
 * @param nodeName
 * @param fileName
 * @param xmlId
 * @return
 */
public static String getSqlText(String nodeName,String fileName,String xmlId){
String text = "";
try{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
File file= new File(fileName);
Document document = db.parse(file);
// Document document = db.parse(fileName);
NodeList nodes = document.getElementsByTagName(nodeName);

for(int i=0;i<nodes.getLength();i++){
NodeList childList = nodes.item(i).getChildNodes();
for(int j=0;j<childList.getLength();j++){
Node child = childList.item(j);
if("id".toUpperCase().equals(child.getNodeName().toUpperCase())){
String childIDStr = child.getTextContent();
if(childIDStr.equals(xmlId)){
for(int k=0;k<childList.getLength();k++){
if("Text".toUpperCase().equals(childList.item(k).getNodeName().toUpperCase())){
Node childText = nodes.item(i).getChildNodes().item(k);
text =  childText.getTextContent();
return text;
}
}

}
}else{
continue;
}
}
}
}catch(Exception e){
}
return text;
}




@SuppressWarnings("unchecked")
@Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
public QueryResult<Object[]> getScrollDataByXmlSQL(int startIndex,
int maxResult, String SQL, Object[] params) throws Exception {


QueryResult<Object[]> qr = new QueryResult<Object[]>();


SQLQuery query = null;
query = createSQLQuery(SQL, params);
if (startIndex != -1 && maxResult != -1)
query.setFirstResult(startIndex).setMaxResults(maxResult);
qr.setResultlist(query.list());


String countQueryString = "select count(*) from (" + SQL + " )";
query = createSQLQuery(countQueryString, params);
qr.setTotalrecord(new Long(query.uniqueResult().toString()));
return qr;
}


/**
 * SQL方式查询
 * 
 * @param sql
 *            符合SQL语法的查询语句
 * @param values
 *            数量可变的条件值,按顺序绑定
 */
public SQLQuery createSQLQuery(final String sql, final Object... values) {
SQLQuery query = getSession().createSQLQuery(sql);


if (values != null) {
for (int i = 0; i < values.length; i++) {
query.setParameter(i, values[i]);
}
}
return query;
}


3.xml文件内容


<?xml version="1.0" encoding="UTF-8"?>
<sqlList>
<!--add by wjy 2012-9-11 获得授权签字信息    oracle-->
<sql>
<id>GetAuthorize</id>
<text>select a, b, a.signatureuser, c, a.authorizedid, a.monitorpointtypeid
  from (with test as
        (select m.monitortypeid a, m.monitortypename b, m.monitorpointtype c
           from MonitorType m)  select a, b,
        substr(t.ca,  
               instr(t.ca, &apos;,&apos;, 1, c.lv) + 1,
               instr(t.ca, &apos;,&apos;, 1, c.lv + 1) -
               (instr(t.ca, &apos;,&apos;, 1, c.lv) + 1)) AS c from
        (select a,
                b,
                &apos;,&apos; || c || &apos;,&apos; AS ca,
                length(c || &apos;,&apos;) - nvl(length(REPLACE(c, &apos;,&apos;)) , 0) AS cnt
           FROM test) t, (select LEVEL lv from dual CONNECT BY LEVEL &lt;= 100) c
        where c.lv &lt;= t.cnt)
  left join authorizedsignature a on a = a.monitortypeid
                                 and c = a.monitorpointtypeid
                                where b like ? order by a, c desc
</text>
</sql>
 
</sqlList>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值