在开发的时候,很多时候都遇到过需要动态拼写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.util public 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; } }
相关推荐
赠送jar包:velocity-engine-core-2.3.jar; 赠送原API文档:velocity-engine-core-2.3-javadoc.jar; 赠送源代码:velocity-engine-core-2.3-sources.jar; 赠送Maven依赖信息文件:velocity-engine-core-2.3.pom;...
赠送jar包:velocity-engine-core-2.3.jar 赠送原API文档:velocity-engine-core-2.3-javadoc.jar 赠送源代码:velocity-engine-core-2.3-sources.jar 包含翻译后的API文档:velocity-engine-core-2.3-javadoc-...
赠送jar包:velocity-tools-generic-3.1.jar; 赠送原API文档:velocity-tools-generic-3.1-javadoc.jar; 赠送源代码:velocity-tools-generic-3.1-sources.jar; 赠送Maven依赖信息文件:velocity-tools-generic-...
velocity-engine-core-2.1.jar
赠送jar包:velocity-tools-generic-3.1.jar 赠送原API文档:velocity-tools-generic-3.1-javadoc.jar 赠送源代码:velocity-tools-generic-3.1-sources.jar 包含翻译后的API文档:velocity-tools-generic-3.1-...
velocity-tools-view-2.0.jar
velocity-tools-view-1.1.jar
velocity(1.1---1.5所有包) velocity-1.4 velocity-1.5 velocity-dep-1.3.1 velocity-tools-1.1 velocity-tools-generic-1.2 velocity-tools-view-1.2 velocity-tools-view-1.3
velocity 2.0 包需要的 jar 包 官方下载的,测试过了,可以运行 Demo 网上都有,关键几行代码: // ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "file"); // 按文件加载 // ve.init(); // Template t = ...
velocity-tools-view-2.0-beta1.jar, 供大家使用
velocity-tools-generic-2.0.jar
velocity-tools-generic-1.2.jar
• commons-collections-3.2.jar • velocity-1.4.jar • velocity-tools-view-1.2.jar • avalon-logkit-2.1.jar
Velocity用户手册---中文版.doc
velocity-tools-view-1.4.jar
注:下文中的 *** 代表文件名中的组件名称。 # 包含: 中文-英文对照文档:【***-javadoc-API文档-中文(简体)-英语-对照版.zip】 jar包下载地址:【***.jar下载地址(官方地址+国内镜像地址).txt】 ...
velocity-tools-view-1.2.jar
velocity-tools-view-1.0.jar velocity-tools-view-1.0.jar
Velocity模板引擎Velocity模板引擎Velocity模板引擎Velocity模板引擎Velocity模板引擎Velocity模板引擎Velocity模板引擎Velocity模板引擎Velocity模板引擎Velocity模板引擎