目 录CONTENT

文章目录

easyExcel的简单使用 以及 JOOQ简单联表查询

不争
2024-01-02 / 0 评论 / 0 点赞 / 58 阅读 / 3247 字

easyExcel的简单使用 以及 JOOQ简单联表查询

1.这个项目用用的Gradle 先导入依赖

compile 'com.alibaba:easyexcel:3.1.1'

2.固三 Controller 写法

    @GetMapping("/excel/download/article/")
    public void download(@RequestParam(value = "published", required = false) Byte published, HttpServletResponse response) {
        try {
            // 这里换成自己的写的service
            List<ContentArticleVo> data = contentArticleService.fetchContentArticle(published);
            // 导出到Excel
            response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
            response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + URLEncoder.encode("文章内容.xlsx", "UTF-8") + "\"");

            EasyExcel.write(response.getOutputStream(), ContentArticleVo.class).sheet("文章").doWrite(data);
        } catch (Exception ex) {
            log.warn("download-article", ex);
        }
    }

JOOQ简单联表查询

    public List<ContentArticleVo> fetchContentArticle(Byte published) {
        try {
            DSLContext create = DSL.using(configuration);

            SelectConditionStep<Record6<Long, String, String, String, String, String>> query = create.select(
                            Tables.CONTENT_ARTICLE.ID,
                            Tables.CONTENT_ARTICLE.TITLE,
                            Tables.CONTENT_COLUMN.NAME.as("category"),
                            Tables.CONTENT_ARTICLE.AUTHOR,
                            Tables.CONTENT_ARTICLE.IMAGES,
                            Tables.CONTENT_ARTICLE.CONTENT
                    )
                    .from(Tables.CONTENT_ARTICLE)
                    .join(Tables.CONTENT_COLUMN)
                    .on(Tables.CONTENT_ARTICLE.COLUMN_ID.eq(Tables.CONTENT_COLUMN.ID))
                    .where(Tables.CONTENT_ARTICLE.PUBLISH.eq(published));

            List<ContentArticleVo> result = query.fetchInto(ContentArticleVo.class);
            return result;
        } catch (Exception ex) {
            log.warn("fetchContentArticle", ex);
            return null;
        }
    }
0

评论区