标签:har 也有 img w3c nts 黄色 jsb 插件 iterator
grails在查询方面也保留了hibernate的hql和criteria查询功能。hql自然不必多说基本不会有什么变化,grails的criteria查询在hibernate上面做了微调使用起来更加方便了,这次就这两个特点分享下我这几天的所学
1.grails的criteria查询
先来个简单的,通过criteria来构造一个in条件查询
def useCriteria2(){
def c=CityInfo.createCriteria()
def result=c.list{
‘in‘("id",[1,2,3])
}
Iterator<CityInfo> it=result.iterator()
def buf=new StringBuffer();
while(it.hasNext()){
CityInfo info=it.next()
buf.append(info.getName()+"-")
}
render buf.toString()
}接下来弄个略微复杂一点的,弄个范围查询和模糊查询综合起来的样例
def useCriteria(){
def c=CityInfo.createCriteria()
def result=c.list{
gt("id",3)
and{
like("name","%兴%")
}
}
Iterator<CityInfo> it=result.iterator()
def buf=new StringBuffer();
while(it.hasNext()){
CityInfo info=it.next()
buf.append(info.getName()+"-")
}
render buf.toString()
}2.grails的hql查询
def useHql(){
def hql="from CityInfo where id>:id and code in:code"
def result=CityInfo.executeQuery(hql,["id":2,code:["NB","NJ"]])
response.setHeader("Content-Type","text/html; charset=UTF-8")
[result:result]
}因为grails并没有默认支持freemarker,因此要在项目里加入这个插件,因为版本号的差异安装插件的方式也有所不同
(1)grails 1.x的版本号
使用命令:grails install-plugin 插件的路径
(2)grails 2.x的版本号
打开BuildConfig.groovy,找到plugins节点,在花括号的最后加上 compile ":freemarker:0.1.1"。例如以下所看到的:
图上的0.1.2是为了能让大家看到那个黄色的提示改的,用0.1.1版本号就足够我们用了,加上之后点一下红色箭头所指的地方或者重新启动下项目。插件就成功安装了
接下来就是jar包了,我们要在lib以下加上freemarker的jar包,这个jar包到struts2里面能够找到,这步完毕后假设freemarker插件文件有编译错误的话就要把freemarker的jar包引入构建路径,例如以下所看到的:
到这一步为止。我们就能够使用freemarker了,上面的useHql的视图页面例如以下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>查询结果</title>
</head>
<body>
<#list result as item>
${item.id}, ${item.name}<br/>
</#list>
</body>
</html>
安装插件的时候要注意,假设版本号选不正确。那么一切工作都是徒劳。
官网说的那个0.4版本号我试过貌似用不成
标签:har 也有 img w3c nts 黄色 jsb 插件 iterator
原文地址:http://www.cnblogs.com/zhchoutai/p/6857675.html