a.添加ureport依赖(根据需要添加其他依赖) <!--UReport2--> <dependency> <groupId>com.bstek.ureport</groupId> <artifactId>ureport2-console</artifactId> <version>2.1.5</version> </dependency>
b.注册服务bean,访问ureport
/** * Ureport2配置类 */ @Configuration @ImportResource("reportcontext.xml") public class UreportConfig { @Bean public ServletRegistrationBean builderUreport(){ return new ServletRegistrationBean(new UReportServlet(),"/ureport/*"); } }
c.加载ureport2的配置文件
*在配置文件中可自定义指定ureport保存文件路径(可不指定)
## 更改 ureport2 生成的xml的保存目录(需真实存在的目录
ureport.fileStoreDir=D:\\ureportfiles
新建UReport的配置文件reportcontext.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <import resource="classpath:ureport-console-context.xml"/> <!-- 引入配置文件 --> <bean id="propertyConfigurer" parent="ureport.props"> <property name="location"> <!-- 读取配置文件 --> <value>classpath:application.properties</value> </property> </bean> </beans>
d.启动项目。访问https://localhost:8080/ureport/designer 设计页面
e.ureport2有3中数据源,推荐使用内置数据源
/** * UReport2采用内置数据数据源配置 */ @Component public class UreportDataSource implements BuildinDatasource { private static final Logger log = LoggerFactory.getLogger(UreportDataSource.class); @Autowired private DataSource dataSource; @Override public String name() { return "内置数据源"; } @Override public Connection getConnection() { try { return dataSource.getConnection(); } catch (SQLException e) { e.printStackTrace(); log.error("报表数据源初始化错误"); return null; } } }
f.报表设计完成保存后,可以通过
http://host[:port][/context-path]/ureport/preview?_u=file:报表名称 预览