Spring MVC & Birt & Mybatis
Birt与spring MVC和Mybatis的集成相对简单。
事先准备:
首先,下载官方给出的运行包,下载Latest BIRT Runtime Release Build那个
http://download.eclipse.org/birt/downloads/
不过因为官方给的包都是在最新jdk环境下的,所以下面给个老点的版本,方便某些遇到运行环境版本过低又不想换jdk的小伙伴
http://down.51cto.com/data/1903232
我这里使用的是spring-framework-4.3.9.RELEASE-dist
http://maven.springframework.org/release/org/springframework/spring/
再去下载个commons-logging-1.1.1.jar
mybatis相关
mybatis-3.3.0.jar
mybatis-spring-1.2.3.jar
sqljdbc4.jar 因为我用的是sql server,这里下载自己所用的数据库对应的JDBC包就行了
运行包下载后解压
一般结构如下
新建web项目,搭建spring mvc框架(含简单示例)
eclipse在中信建一个web工程,dynamic web project就行,先放张搭建完的目录结构图
一般来说结构如上图,mybatis-config.xml和spring-common.xml可以先无视(mybatis集成相关),把下载的spring文件解压,把lib中的jar包和commons-logging-1.1.1.jar都丢到web工程的lib下
配置web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--MyBatis配置时用-->
<!-- <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:config/spring-*.xml</param-value>
</context-param>-->
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 配置初始配置化文件,前面contextConfigLocation看情况二选一 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:config/spring-mvc.xml</param-value>
</init-param>
<!-- 启动加载一次 -->
<load-on-startup>1</load-on-startup>
</servlet>
<!--为DispatcherServlet建立映射 -->
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<!-- 此处可以可以配置成*.do,对应struts的后缀习惯 -->
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
配置applicationContext
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"
default-lazy-init="true">
<!-- 使用annotation 自动注册bean, 并保证@Required、@Autowired的属性被注入 -->
<context:component-scan base-package="com">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
<context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
</context:component-scan>
</beans>
配置spring-mvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd">
<context:component-scan base-package="com" />
<!-- 开启注解 -->
<mvc:annotation-driven />
<!--
配置静态资源,直接映射到对应的文件夹,不被DispatcherServlet处理,3.04新增功能,需要重新设置spring-mvc-3.0.xsd
-->
<!-- 定义JSP文件的位置 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
jsp文件
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>demo</title>
<script>
var ctx = "/springMVCTest3";
function viewReport() {
var temp_form = document.createElement("form");
temp_form.action=ctx +"/preview?__report=new_report_2.rptdesign&__format=pdf";
window.open(temp_form.action+"&userId="+document.getElementById("userId").value);//这个就是等会整合完成后访问报表模板的地方
}
function post(PARAMS) {
var temp_form = document.createElement("form");
temp_form.action =ctx +"/checkReport";
temp_form.method = "post";
temp_form.style.display = "none";
for ( var x in PARAMS) {
var opt = document.createElement("textarea");
opt.name = x;
opt.value = PARAMS[x];
temp_form.appendChild(opt);
}
document.body.appendChild(temp_form);
temp_form.submit();
}
</script>
</head>
<body>
<input type="text" name="userId" id="userId" value="">${demoStr}
</input>
<button onclick="viewReport()">
check
</button>
</body>
</html>
controller文件
package com.springMVC.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class indexController{
@RequestMapping( value="/index")
public ModelAndView index(HttpServletRequest arg0, HttpServletResponse arg1)
throws Exception {
// TODO Auto-generated method stub
ModelAndView model=new ModelAndView();
model.addObject("demoStr", "HelloWorld");
model.setViewName("index");
return model;
}
}
这样最基础的Spring MVC 框架就搭好了,新建个server,绑定项目,运行
URL:http://localhost:8080/springMVCTest3/index
可以看到controller中传过来的demoStr字符串传过来了
整合Mybatis
先放张配置完成后的目录结构图,红框是要新建的,篮框中有修改
mybatis-3.3.0.jar
mybatis-spring-1.2.3.jar
sqljdbc4.jar
将上述jar包放入lib文件夹中
配置spring-common.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<!-- 1. 数据源 : DriverManagerDataSource -->
<!-- 下方填写自己相应数据库的驱动,密码等等-->
<bean id="dataSource"
class="org.apache.tomcat.jdbc.pool.DataSource" destroy-method="close">
<property name="driverClassName" value="XXXX" />
<property name="url" value="XXXX" />
<property name="username" value="XXXX" />
<property name="password" value="XXXX" />
</bean>
<!--
2. mybatis的SqlSession的工厂: SqlSessionFactoryBean dataSource:引用数据源
MyBatis定义数据源,同意加载配置
-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!-- <property name="configLocation" value="classpath:config/mybatis-config.xml" /> -->
</bean>
<!--
3. mybatis自动扫描加载Sql映射文件/接口 : MapperScannerConfigurer sqlSessionFactory
basePackage:指定sql映射文件/接口所在的包(自动扫描)
-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.report.mapper"></property>
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>
<!--
4. 事务管理 : DataSourceTransactionManager dataSource:引用上面定义的数据源
-->
<bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 5. 使用声明式事务
transaction-manager:引用上面定义的事务管理器
-->
<tx:annotation-driven transaction-manager="txManager" />
</beans>
mybatis-config.xml,这里是当需要手动加载时用的,如果需要像下方这样配置即可,当然本例中使用的是自动加载,在上面的xml中已经配置完了
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- <typeAliases>
<typeAlias alias="dept" type="com.report.model.dept" />
</typeAliases>
<mappers>
<mapper resource="com/report/mapper/deptMapper.xml"/>
</mappers>-->
</configuration>
deptMapper.java
package com.report.mapper;
import java.util.List;
import java.util.Map;
import com.report.model.dept;
public interface deptMapper {
public dept getAllDept(String paramMap);
}
deptMapper.xml,在这里写查询语句
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.report.mapper.deptMapper">
<select id="getAllDept" parameterType="String" resultType="com.report.model.dept">
select e.level name
from employee e
where e.employee_id = #{id}
</select>
</mapper>
实例类 dept.java
package com.report.model;
public class dept {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
service文件 deptService.java
package com.report.service;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.report.mapper.deptMapper;
import com.report.model.dept;
@Service
public class deptService {
@Autowired
private deptMapper mapper;
public dept getAllDept(String paramMap){
return mapper.getAllDept(paramMap);
}
}
indexController.java文件修改如下
package com.springMVC.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.report.model.dept;
import com.report.service.deptService;
@Controller
public class indexController{
@Autowired
private deptService deptService;
@RequestMapping( value="/index")
public ModelAndView index(HttpServletRequest arg0, HttpServletResponse arg1)
throws Exception {
// TODO Auto-generated method stub
ModelAndView model=new ModelAndView();
dept dept=deptService.getAllDept("P0003501");
System.out.println("xxx:"+dept.getName());
model.addObject("demoStr", dept.getName());
model.setViewName("index");
return model;
}
}
web.xml
将如下方字段外的注释符去掉
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:config/spring-*.xml</param-value>
</context-param>
配置完成,运行
可以看到如下结果
整合Birt
先放上整合完成后的目录结构图,红色是新加的,蓝色需要修改
解压刚才下载的运行包
一般结构如下
进入WebViewerExample文件
将WebViewerExample\WEB-INF\lib中的jar包放入项目的lib文件夹中,这里可能会遇到一个错误,挑一个WebViewerExample\WEB-INF\lib中的jar包打开,有时会存在一个ECLIPSE.SF文件,删掉。不然运行时会报错,建议拷贝一份lib中的jar包,把jar都过一遍,再放到项目的lib中
复制WebViewerExample\webcontent和WebViewerExample\WEB-INF\tlds文件夹,按照上面给出的结构图黏贴到项目中
复制WebViewerExample\WEB-INF文件夹中的jrun.web.xml,server-config.wsdd,viewer.properties文件,按照结构图黏贴
新建report-engine文件夹,按照下图继续新建4个子文件夹
新建reportFiles文件夹
在其中新建一个report文件,名字和上面jsp中跳转的名字一样即可
report文件建立的具体步骤请看,这里先不涉及给报表传值,这部分后续再说
http://blog.csdn.net/yeyinglingfeng/article/details/73740398
web.xml配置,最后在web.xml中添加如下代码即可
<!-- Default locale setting.
-->
<context-param>
<param-name>BIRT_VIEWER_LOCALE</param-name>
<param-value>en-US</param-value>
</context-param>
<!--
Default timezone setting.
Examples: "Europe/Paris", "GMT+1".
Defaults to the container's timezone.
-->
<context-param>
<param-name>BIRT_VIEWER_TIMEZONE</param-name>
<param-value></param-value>
</context-param>
<!--
Report resources directory for preview. Defaults to ${birt home}
-->
<context-param>
<param-name>BIRT_VIEWER_WORKING_FOLDER</param-name>
<param-value>reportFiles</param-value>
</context-param>
<!--
Temporary document files directory. Defaults to ${birt home}/documents
-->
<context-param>
<param-name>BIRT_VIEWER_DOCUMENT_FOLDER</param-name>
<param-value>WEB-INF/report-engine/documents</param-value>
</context-param>
<!--
Flag whether the report resources can only be accessed under the
working folder. Defaults to true
-->
<context-param>
<param-name>WORKING_FOLDER_ACCESS_ONLY</param-name>
<param-value>true</param-value>
</context-param>
<!--
Settings for how to deal with the url report path. e.g. "http://host/repo/test.rptdesign".
Following values are supported:
<all> - All paths.
<domain> - Only the paths with host matches current domain. Note the comparison is literal, "127.0.0.1" and "localhost" are considered as different hosts.
<none> - URL paths are not supported.
Defaults to "domain".
-->
<context-param>
<param-name>URL_REPORT_PATH_POLICY</param-name>
<param-value>domain</param-value>
</context-param>
<!--
Temporary image/chart directory. Defaults to ${birt home}/report/images
-->
<context-param>
<param-name>BIRT_VIEWER_IMAGE_DIR</param-name>
<param-value>WEB-INF/report-engine/images</param-value>
</context-param>
<!-- Engine log directory. Defaults to ${birt home}/logs -->
<context-param>
<param-name>BIRT_VIEWER_LOG_DIR</param-name>
<param-value>WEB-INF/report-engine/logs</param-value>
</context-param>
<!-- Report engine log level -->
<context-param>
<param-name>BIRT_VIEWER_LOG_LEVEL</param-name>
<param-value>WARNING</param-value>
</context-param>
<!--
Directory where to store all the birt report script libraries (JARs).
Defaults to ${birt home}/scriptlib
-->
<context-param>
<param-name>BIRT_VIEWER_SCRIPTLIB_DIR</param-name>
<param-value>WEB-INF/report-engine/scriptlib</param-value>
</context-param>
<!-- Resource location directory. Defaults to ${birt home} -->
<context-param>
<param-name>BIRT_RESOURCE_PATH</param-name>
<param-value></param-value>
</context-param>
<!-- Preview report rows limit. An empty value means no limit. -->
<context-param>
<param-name>BIRT_VIEWER_MAX_ROWS</param-name>
<param-value></param-value>
</context-param>
<!--
Max cube fetch levels limit for report preview (Only used when
previewing a report design file using the preview pattern)
-->
<context-param>
<param-name>BIRT_VIEWER_MAX_CUBE_ROWLEVELS</param-name>
<param-value></param-value>
</context-param>
<context-param>
<param-name>BIRT_VIEWER_MAX_CUBE_COLUMNLEVELS</param-name>
<param-value></param-value>
</context-param>
<!-- Memory size in MB for creating a cube. -->
<context-param>
<param-name>BIRT_VIEWER_CUBE_MEMORY_SIZE</param-name>
<param-value></param-value>
</context-param>
<!-- Defines the BIRT viewer configuration file -->
<context-param>
<param-name>BIRT_VIEWER_CONFIG_FILE</param-name>
<param-value>WEB-INF/viewer.properties</param-value>
</context-param>
<!--
Flag whether to allow server-side printing. Possible values are "ON"
and "OFF". Defaults to "ON".
-->
<context-param>
<param-name>BIRT_VIEWER_PRINT_SERVERSIDE</param-name>
<param-value>ON</param-value>
</context-param>
<!--
Flag whether to force browser-optimized HTML output. Defaults to true
-->
<context-param>
<param-name>HTML_ENABLE_AGENTSTYLE_ENGINE</param-name>
<param-value>true</param-value>
</context-param>
<!--
Filename generator class/factory to use for the exported reports.
-->
<context-param>
<param-name>BIRT_FILENAME_GENERATOR_CLASS</param-name>
<param-value>org.eclipse.birt.report.utility.filename.DefaultFilenameGenerator</param-value>
</context-param>
<!--
Viewer Filter used to set the request character encoding to UTF-8.
-->
<filter>
<filter-name>ViewerFilter</filter-name>
<filter-class>org.eclipse.birt.report.filter.ViewerFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>ViewerFilter</filter-name>
<servlet-name>ViewerServlet</servlet-name>
</filter-mapping>
<filter-mapping>
<filter-name>ViewerFilter</filter-name>
<servlet-name>EngineServlet</servlet-name>
</filter-mapping>
<!-- Viewer Servlet Context Listener -->
<listener>
<listener-class>org.eclipse.birt.report.listener.ViewerServletContextListener</listener-class>
</listener>
<!-- Viewer HttpSession Listener -->
<listener>
<listener-class>org.eclipse.birt.report.listener.ViewerHttpSessionListener</listener-class>
</listener>
<!-- Viewer Servlet, Supports SOAP -->
<servlet>
<servlet-name>ViewerServlet</servlet-name>
<servlet-class>org.eclipse.birt.report.servlet.ViewerServlet</servlet-class>
</servlet>
<!-- Engine Servlet -->
<servlet>
<servlet-name>EngineServlet</servlet-name>
<servlet-class>org.eclipse.birt.report.servlet.BirtEngineServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ViewerServlet</servlet-name>
<url-pattern>/frameset</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>ViewerServlet</servlet-name>
<url-pattern>/run</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>EngineServlet</servlet-name>
<url-pattern>/preview</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>EngineServlet</servlet-name>
<url-pattern>/download</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>EngineServlet</servlet-name>
<url-pattern>/parameter</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>EngineServlet</servlet-name>
<url-pattern>/document</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>EngineServlet</servlet-name>
<url-pattern>/output</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>EngineServlet</servlet-name>
<url-pattern>/extract</url-pattern>
</servlet-mapping>
<jsp-config>
<taglib>
<taglib-uri>/birt.tld</taglib-uri>
<taglib-location>/WEB-INF/tlds/birt.tld</taglib-location>
</taglib>
</jsp-config>
运行程序,可能会报超时错误,双击server,将红框时间改长点就行
运行成功后应该可以通过jsp页面上的按钮查看报表。
或者直接根据URL查看http://localhost:8080/springMVCTest3/preview?__report=new_report_2.rptdesign&__format=pdf&userId=
这样就能在其他项目中引用该URL来实现报表项目与实际项目的分离,当然直接整合到实际项目中也是一样的道理。
以上,将birt,springMvc,myBatis整合完成,但是还不涉及给报表传值,动态修改报表,脚本创建图表,报表调用存储过程等等操作。
不过估计大家都能看出来,&userId=这个明显就是参数的传入口,通过在报表中添加变量接受userId数据,然后将SQL语句改为JS脚本形式,接受参数,动态修改SQL就能实现控制报表的目的了。这部分之后再讲。
项目打包下载地址(不过由于数据库配置等等差异性,需要调整下,大致上只能作为结构参考,这里我把数据库配置改掉了,需要自己改成自己环境的)3个分卷压缩包
http://download.csdn.net/detail/yeyinglingfeng/9885545
http://download.csdn.net/detail/yeyinglingfeng/9885548
http://download.csdn.net/detail/yeyinglingfeng/9885551
2017-9-18
鉴于csdn调整了上传资源下载分,所以放出git hub免费下载地址
https://github.com/SecondMagic/SpringMVC-MyBatis-Birt