Defining database queries inside Velocity templates

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-04 07:37:29

I would recommend something like http://velosurf.sourceforge.net/ instead of directly embedding queries.

Velocity is a very lightweight templating engine, it has very little functionality by itself. It works by interpolating variables defined (by you) in a context into a template file. By default, the context is empty, meaning that there are no variables for the template to use. Velocity doesn't allow instantiating new objects of custom classes, all it allows you to use is string and number literals, plus maps and lists:

#set ($n = 5)
#set ($s = 'Hello New World!')
#set ($m = {'four' : 4, 'five' : $n})
#set ($a = ['x', 'y', 'z'])

You're responsible for making Velocity "smart" by populating the context with useful objects before interpolating. As a starting point, you can use some of the Velocity Tools. You can add your own tools, like an SQL tool that lets you run queries. Or you can use a higher level framework that uses velocity and which already offers a rich set of tools available in templates.

So, to answer your question, you can execute any SQL statement you want, as long as you add in the context an object that can execute such statements. You should read more about how to properly use Velocity in their developer guide.

This following is for HQL (you can try with others using this as example)

General example showing how to display the first 5 results of a given query:

#set($hql = "<query here>")
#set($results = $xwiki.searchDocuments($hql, 5, 0))
#foreach ($item in $results)
 * $item
#end

The examples below will show you various HQL queries that you can write. Simple Query

Displays all documents who have been created by the user XWiki.JohnDoe

#set($hql = "where doc.creator='XWiki.JohnDoe'")

Additonal Info

you may want to look at this link

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!