目录
引言
1.引入MyBatis依赖
2.简单配置MyBatis和Mysql存储
3.增加Ureport配置文件
数据库结构
4.创建报表存储器实体UreportFileEntity
5.创建UreportFileMapper接口
6.创建配置文件UreportFileMapper.xml实现对应接口
7.创建服务接口UreportFileService
8.创建服务实现类UreportFileServiceImpl
9.创建自定义报表存储器MySQLProvider
10.启动查看结果
UReport2默认提供的名为“服务器文件系统”的报表存储机制,实际上是实现了UReport2提供的com.bstek.ureport.provider.report.ReportProvider接口;如果我们定义了自己的报表存储器,只需要实现了ReportProvider接口后,并将实现类配置到Spring中,让其成为一个标准的Spring Bean,这样UReport2就会检测到它而将其加载。
源码:https://gitee.com/lfw1024/myreport/blob/master/pom.xml
org.mybatis.spring.boot
mybatis-spring-boot-starter
2.0.0
源码:https://gitee.com/lfw1024/myreport/blob/master/src/main/resources/application.properties
# mybatis实体类包
mybatis.typeAliasesPackage=com.ggzn.entity
# mybatis mapper 文件的位置
mybatis.mapperLocations=classpath:mapper/*.xml
# ureport Mysql 存储
ureport.mysql.provider.prefix=report-
ureport.mysql.provider.disabled=false
https://gitee.com/lfw1024/myreport/blob/master/src/main/resources/ureport.properties
# 将ureport.disableFileProvider改成true,即可禁用默认提供的文件存储机制
ureport.disableHttpSessionReportCache=false
ureport.disableFileProvider=false
ureport.fileStoreDir=/WEB-INF/ureportfiles
ureport.debug=true
https://gitee.com/lfw1024/myreport/blob/master/src/main/resources/sql/ureport.sql
CREATE TABLE `ureport_file_tbl` (
`id_` int(11) NOT NULL AUTO_INCREMENT,
`name_` varchar(100) NOT NULL,
`content_` mediumblob,
`create_time_` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`update_time_` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id_`)
) ENGINE=InnoDB AUTO_INCREMENT=100 DEFAULT CHARSET=utf8;
https://gitee.com/lfw1024/myreport/blob/master/src/main/java/com/ggzn/entity/UreportFileEntity.java
@Data
public class UreportFileEntity {
private Long id;
private String name;
private byte[] content;
private Date createTime;
private Date updateTime;
}
https://gitee.com/lfw1024/myreport/blob/master/src/main/java/com/ggzn/mapper/UreportFileMapper.java
@Mapper
public interface UreportFileMapper {
/**
* 根据报表名称检查报表是否存在
* @param name 报表名称
* @return
*/
public int checkExistByName(String name);
/**
* 根据报表名称查询报表
* @param name 报表名称
* @return
*/
public UreportFileEntity queryUreportFileEntityByName(String name);
/**
* 查询全部报表
* @return
*/
public List queryReportFileList();
/**
* 根据报表名称删除报表
* @param name
* @return
*/
public int deleteReportFileByName(String name);
/**
* 保存报表
*/
public int insertReportFile(UreportFileEntity entity);
/**
* 更新报表
* @param entity
* @return
*/
public int updateReportFile(UreportFileEntity entity);
}
https://gitee.com/lfw1024/myreport/blob/master/src/main/resources/mapper/UreportFileMapper.xml
delete from ureport_file_tbl where name_ = #{name}
insert into ureport_file_tbl (name_, content_, create_time_, update_time_) values
(#{name}, #{content}, #{createTime}, #{updateTime})
update ureport_file_tbl set
name_ = #{name} ,
content_ = #{content},
create_time_ = #{createTime},
update_time_ = #{updateTime}
where id_ = #{id}
https://gitee.com/lfw1024/myreport/blob/master/src/main/java/com/ggzn/service/UreportFileService.java
public interface UreportFileService {
/**
* 根据报表名称检查报表是否存在
* @param name 报表名称
* @return
*/
public int checkExistByName(String name);
/**
* 根据报表名称查询报表
* @param name 报表名称
* @return
*/
public UreportFileEntity queryUreportFileEntityByName(String name);
/**
* 查询全部报表
* @return
*/
public List queryReportFileList();
/**
* 根据报表名称删除报表
* @param name
* @return
*/
public int deleteReportFileByName(String name);
/**
* 保存报表
*/
public int insertReportFile(UreportFileEntity entity);
/**
* 更新报表
* @param entity
* @return
*/
public int updateReportFile(UreportFileEntity entity);
}
https://gitee.com/lfw1024/myreport/blob/master/src/main/java/com/ggzn/service/impl/UreportFileServiceImpl.java
@Service
public class UreportFileServiceImpl implements UreportFileService{
@Resource
private UreportFileMapper ureportFileMapper;
@Override
public int checkExistByName(String name) {
return ureportFileMapper.checkExistByName(name);
}
@Override
public UreportFileEntity queryUreportFileEntityByName(String name) {
// TODO Auto-generated method stub
return ureportFileMapper.queryUreportFileEntityByName(name);
}
@Override
public List queryReportFileList() {
// TODO Auto-generated method stub
return ureportFileMapper.queryReportFileList();
}
@Override
public int deleteReportFileByName(String name) {
// TODO Auto-generated method stub
return ureportFileMapper.deleteReportFileByName(name);
}
@Override
public int insertReportFile(UreportFileEntity entity) {
// TODO Auto-generated method stub
return ureportFileMapper.insertReportFile(entity);
}
@Override
public int updateReportFile(UreportFileEntity entity) {
// TODO Auto-generated method stub
return ureportFileMapper.updateReportFile(entity);
}
}
https://gitee.com/lfw1024/myreport/blob/master/src/main/java/com/ggzn/ureport/provider/MySQLProvider.java
@Setter
@Component
@ConfigurationProperties(prefix = "ureport.mysql.provider")
public class MySQLProvider implements ReportProvider{
private final Logger log= LoggerFactory.getLogger(getClass());
private static final String NAME = "mysql-provider";
private String prefix = "";
private boolean disabled;
@Autowired
private ViewUreportService viewUreportService;
@Autowired
private UreportFileService ureportFileService;
@Autowired
private HttpServletRequest request;
/**
* 根据报表名加载报表文件
* @param file 报表名称
* @return 返回的InputStream
*/
@Override
public InputStream loadReport(String file) {
UreportFileEntity ureportFileEntity = ureportFileService.queryUreportFileEntityByName(getCorrectName(file));
byte[] content = ureportFileEntity.getContent();
ByteArrayInputStream inputStream = new ByteArrayInputStream(content);
return inputStream;
}
/**
* 根据报表名,删除指定的报表文件
* @param file 报表名称
*/
@Override
public void deleteReport(String file) {
ureportFileService.deleteReportFileByName(getCorrectName(file));
}
/**
* 获取所有的报表文件
* @return 返回报表文件列表
*/
@Override
public List getReportFiles() {
List list = ureportFileService.queryReportFileList();
List reportList = new ArrayList<>();
for (UreportFileEntity ureportFileEntity : list) {
reportList.add(new ReportFile(ureportFileEntity.getName(), ureportFileEntity.getUpdateTime()));
}
return reportList;
}
/**
* 保存报表文件
* @param file 报表名称
* @param content 报表的XML内容
*/
@Override
public void saveReport(String file, String content) {
file = getCorrectName(file);
UreportFileEntity ureportFileEntity = ureportFileService.queryUreportFileEntityByName(file);
Date currentDate = new Date();
if(ureportFileEntity == null){
ureportFileEntity = new UreportFileEntity();
ureportFileEntity.setName(file);
ureportFileEntity.setContent(content.getBytes());
ureportFileEntity.setCreateTime(currentDate);
ureportFileEntity.setUpdateTime(currentDate);
ureportFileService.insertReportFile(ureportFileEntity);
}else{
ureportFileEntity.setContent(content.getBytes());
ureportFileEntity.setUpdateTime(currentDate);
ureportFileService.updateReportFile(ureportFileEntity);
}
HttpSession session = request.getSession();
if(session.getAttribute("viewid") == null){
log.info("viewid为空");
}else{
String viewid = session.getAttribute("viewid").toString();
log.info("viewid:"+viewid);
ViewUreportEntity viewUreportEntity = new ViewUreportEntity();
viewUreportEntity.setId(viewid);
viewUreportEntity.setName(prefix+file);
int i = viewUreportService.insertOne(viewUreportEntity);
log.info(file+"ViewUreportEntity插入成功"+i);
}
}
/**
* @return 返回存储器名称
*/
@Override
public String getName() {
return NAME;
}
/**
* @return 返回是否禁用
*/
@Override
public boolean disabled() {
return disabled;
}
/**
* @return 返回报表文件名前缀
*/
@Override
public String getPrefix() {
return prefix;
}
/**
* @description 获取没有前缀的文件名
* @param name
* @return
*/
private String getCorrectName(String name){
log.info("前缀:"+prefix);
if(name.startsWith(prefix)){
name = name.substring(prefix.length(), name.length());
}
return name;
}
}