当前位置: 代码迷 >> SQL >> 透过velocity模板和Hibernate sql-query的实现动态hql
  详细解决方案

透过velocity模板和Hibernate sql-query的实现动态hql

热度:25   发布时间:2016-05-05 11:36:22.0
通过velocity模板和Hibernate sql-query的实现动态hql

在开发的时候,很多时候都遇到过需要动态拼写SQL,有的是在配置文件中写SQL,有的是在Java代码中拼写SQL,以配置文件拼SQL的可以拿IBatis为代表,但是很多时候是使用Hibernate的,这个时候就想要是Hibernate能像IBatis那样写就好了。

原创不易,转载请注明出处:通过velocity模板和Hibernate sql-query的实现动态hql

代码下载:http://www.zuidaima.com/share/1723627800005632.htm

这个时候就想到了模板语言和配置文件的结合。模板引擎可以选择Velocity,简单而不失强大,配置文件可以模仿Hibernate的sql-query?的XML文件。

Sql-query的示例代码如下(SQL?or?HQL):

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE dynamic-hibernate PUBLIC "-//ANYFRAME//DTD DYNAMIC-HIBERNATE//EN""http://www.anyframejava.org/dtd/anyframe-dynamic-hibernate-mapping-4.0.dtd"><dynamic-hibernate>    <query name="selectUserSQL">        <![CDATA[            SELECT  USER_ID,NAME            FROM zuidaima_com_user Where 1=1            #if($name && $name.length() > 1)             AND name =:name            #end    ]]>    </query>    <query name="selectUserHQL">    <![CDATA[         FROM zuidaima_com_users        Where 1=1            #if($name && $name.length() > 1)                AND name =:name            #end        ]]>    </query>

?在系统加载时,需要把配置文件加载到系统中。加载代码关键部分如下:

package com.zuidaima.utilpublic class DynamicHibernateImpl implements InitializingBean, ResourceLoaderAware, ApplicationContextAware{        public void afterPropertiesSet() throws Exception {           for (int i = 0; i < fileNames.size(); i++) {              String fileName = ((String) fileNames.get(i)).trim();              if (resourceLoader instanceof ResourcePatternResolver) {                                try {                     Resource[] resources=         ((ResourcePatternResolver) resourceLoader).getResources(fileName);                    buildHQLMap(resources);                  } catch (IOException ex) {                     throw new Exception("Could not resolve sql definition resource pattern ["                     + fileName + "]", ex);                 }             } else {                                Resource resource = resourceLoader.getResource(fileName);                 buildHQLMap(new Resource[] { resource });             }         }     }  protected void buildHQLMap(Resource[] resources) throws Exception {         for (int i = 0; i < resources.length; i++) {             buildHQLMap(resources[i]);         }     }  private void buildHQLMap(Resource resource) throws Exception {         try {             InputSource inputSource = new InputSource(resource.getInputStream());             org.w3c.dom.Document doc = this.documentLoader.loadDocument(inputSource, null, null, org.springframework.util.xml.XmlValidationModeDetector.VALIDATION_NONE, false);             Element root = doc.getDocumentElement();             List<Element> querys = DomUtils.getChildElements(root);             for(Element query:querys){                 String queryName = query.getAttribute("name");                 if (StringUtils.isEmpty(queryName)) {                     throw new Exception("DynamicHibernate Service : name is essential attribute in a <query>.");                 }                 if(statements.containsKey(queryName)){                     throw new Exception("DynamicHibernate Service : duplicated query in a <query>."+queryName);                 }                 statements.put(queryName, DomUtils.getTextValue(query));             }         } catch (SAXParseException se) {             throw se;         } catch (IOException ioe) {             throw ioe;         }     }

?

  相关解决方案