birt动态SQL实现有三种方式:拼接SQL、绑定变量和让应用程序拼接,birt得到返回结果集方式。
1.拼接SQL方式
在数据集中写SQL,如下:
select id ,code,name,type from type
选中数据集,点script方式,在beforeOpen事件中写如下SQL:
var type = reportContext.getParameterValue("type"); var name = reportContext.getParameterValue("name"); var query = this.queryText; if(type!=null){ query = query + " and type = "+type; } if(name!=null&&name!=""){ query = query + " and name = '"+name+"'"; } this.queryText = query;
然后就可以了,当然,也可以不写第一步,直接所有的SQL都在beforeOpen中拼接。
但是,拼接SQL方式不仅复杂容易错,还会导致SQL注入风险。
2.绑定变量方式
在数据集的SQL中写如下SQL
select id ,code,name,type from location where (type = ? or ? is null ) and ( name = ? or ? is null )
然后配置数据集的参数,如下图:
最后一项是链接报表的参数。倒数第二项是默认值,填null
这种方式不会有SQL注入风险。
要注意参数与问号个数要相同
当采用绑定变量时,数据集参数和报表参数关联时,可能会出现这样的异常:
org.eclipse.birt.report.data.oda.jdbc.JDBCException: Cannot set preparedStatement null value.
原因是数据集参数中不能将Intger 类型设置为null值,要解决这个问题,可以将
Intger 类型的参数全部改成DECIMAL,原因待研究。
另外如果想要查看执行的SQL,可以在数据集的SCRIPT的beforeOpen事件写如下脚本:
importPackage( Packages.java.io ); out = new PrintWriter( new FileWriter("c:/debuginfo.txt", true ) ); out.println("queryText:-->"+ this.queryText); out.close();
第三种是在应该程序处理查询,BIRT负责取出结果集展示,采用的是BIRT+SPRING+MYBATIS。
当然这里MYBATIS也可以是其它框架,思路一样。
BirtBeanFactory.java
public class BirtBeanFactory { private static BeanFactory ctx; public synchronized static void setBeanFactory(BeanFactory beanFactory) { ctx = beanFactory; } public static Object getBean(String str) { return ctx.getBean(str); } public static <T> T getBean(Class<T> clazz) { return ctx.getBean(clazz); } }
GlobalInitializer.java
public class GlobalInitializer implements InitializingBean ,BeanFactoryAware{ private BeanFactory beanFactory; @Override public void afterPropertiesSet() throws Exception { BirtBeanFactory.setBeanFactory(beanFactory); } @Override public void setBeanFactory(BeanFactory beanFactory) throws BeansException { this.beanFactory = beanFactory; } }
BirtDataFactory.java
public class BirtDataFactory { private TestService manager = (TestService) BirtBeanFactory.getBean(TestService.class); public List<Location> findListByDto(LocationDto dto){ return manager.findListByDto(dto); } }
spring配置文件配置下:
<bean id="globalInitializer" class="com.test.birt.core.GlobalInitializer"/>
这样代码层就完成了,
在birt中创建脚本数据源
创建脚本数据集,在数据集的open事件中写这样的方法:
importPackage(Packages.com.test.birt.core); importPackage(Packages.com.test.birt.report.dto); factory = new BirtDataFactory(); dto = new TestDto(); dto.setCreateTimeStart(reportContext.getParameterValue("createTimeStart")); dto.setCreateTimeEnd(reportContext.getParameterValue("createTimeEnd")); if(reportContext.getParameterValue("dcId")!=null){ dto.setDcId(reportContext.getParameterValue("dcId").longValue()); } collectList=factory.findListByDto(dto); iterator = collectList.iterator();
在fecth阶段,写这样 的代码:
if(iterator.hasNext() == false ){ return false; } else{ var collectBean = iterator.next(); row["id"]=collectBean.getId(); row["name"] = collectBean.getName(); return true; }
这样就可以了。
要显示的字段(row["id"]等)可以在数据集中编辑,选中数据集--EDIT--OUTPUT COLUMNS---ADD
评论
select id ,code,name,type
from location
where (type = ? or ? is null )
and ( name = ? or ? is null )
这里的?如type =?or ?is null,绑定2个参数 预览的是就需要填写2个参数 不然提示变量没有被指定。 能不能把这个做法的文件代码发给我参考一下 谢谢 32216166@qq.com
额,没有文件,你有什么问题么
select id ,code,name,type
from location
where (type = ? or ? is null )
and ( name = ? or ? is null )
这里的?如type =?or ?is null,绑定2个参数 预览的是就需要填写2个参数 不然提示变量没有被指定。 能不能把这个做法的文件代码发给我参考一下 谢谢 32216166@qq.com
select id ,code,name,type
from location
where (type = ? or ? is null )
and ( name = ? or ? is null )
这一段sql,每一个?代表一个参数,那么type = ? or ? is null,这两个问好应该代表的同一个参数,会不会需要两个参数呢
需要两个参数
select id ,code,name,type
from location
where (type = ? or ? is null )
and ( name = ? or ? is null )
这一段sql,每一个?代表一个参数,那么type = ? or ? is null,这两个问好应该代表的同一个参数,会不会需要两个参数呢