spring boot实现自动输出word文档功能
本文用到Apache POI组件
组件依赖在pom.xml文件中添加
<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>4.1.0</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.1.0</version> </dependency>
首先创建相关的实体类、编写需要用到的sql查询。
import lombok.Data; // 选择题实体 @Data public class MultiQuestion { private Integer questionId; private String subject; private String section; private String answerA; private String answerB; private String answerC; private String answerD; private String question; private String level; private String rightAnswer; private String analysis; //题目解析 private Integer score; }
import lombok.Data; //填空题实体类 @Data public class FillQuestion { private Integer questionId; private String subject; private String question; private String answer; private Integer score; private String level; private String section; private String analysis; //题目解析 }
import lombok.Data; //判断题实体类 @Data public class JudgeQuestion { private Integer questionId; private String subject; private String question; private String answer; private String level; private String section; private Integer score; private String analysis; //题目解析 }
创建好要用到的实体类之后,利用mybatis写sql查询,可以分为两种:1、配置mapper.xml文件路径,在xml文件中编写sql语句。2、直接使用注解。本文使用方法为第二种。
@Mapper public interface MultiQuestionMapper { /** * select * from multiquestions where questionId in ( * select questionId from papermanage where questionType = 1 and paperId = 1001 * ) */ @Select("select * from multi_question where questionId in (select questionId from paper_manage where questionType = 1 and paperId = #{paperId})") List<MultiQuestion> findByIdAndType(Integer PaperId); @Select("select * from multi_question") IPage<MultiQuestion> findAll(Page page); /** * 查询最后一条记录的questionId * @return MultiQuestion */ @Select("select questionId from multi_question order by questionId desc limit 1") MultiQuestion findOnlyQuestionId(); @Options(useGeneratedKeys = true,keyProperty = "questionId") @Insert("insert into multi_question(subject,question,answerA,answerB,answerC,answerD,rightAnswer,analysis,section,level) " + "values(#{subject},#{question},#{answerA},#{answerB},#{answerC},#{answerD},#{rightAnswer},#{analysis},#{section},#{level})") int add(MultiQuestion multiQuestion); @Select("select questionId from multi_question where subject =#{subject} order by rand() desc limit #{pageNo}") List<Integer> findBySubject(String subject,Integer pageNo); }
//填空题 @Mapper public interface FillQuestionMapper { @Select("select * from fill_question where questionId in (select questionId from paper_manage where questionType = 2 and paperId = #{paperId})") List<FillQuestion> findByIdAndType(Integer paperId); @Select("select * from fill_question") IPage<FillQuestion> findAll(Page page); /** * 查询最后一条questionId * @return FillQuestion */ @Select("select questionId from fill_question order by questionId desc limit 1") FillQuestion findOnlyQuestionId(); @Options(useGeneratedKeys = true,keyProperty ="questionId" ) @Insert("insert into fill_question(subject,question,answer,analysis,level,section) values " +<div>本文来源gaodai.ma#com搞#代!码网_</div> "(#{subject,},#{question},#{answer},#{analysis},#{level},#{section})") int add(FillQuestion fillQuestion); @Select("select questionId from fill_question where subject = #{subject} order by rand() desc limit #{pageNo}") List<Integer> findBySubject(String subject,Integer pageNo); }
//判断题 @Mapper public interface JudgeQuestionMapper { @Select("select * from judge_question where questionId in (select questionId from paper_manage where questionType = 3 and paperId = #{paperId})") List<JudgeQuestion> findByIdAndType(Integer paperId); @Select("select * from judge_question") IPage<JudgeQuestion> findAll(Page page); /** * 查询最后一条记录的questionId * @return JudgeQuestion */ @Select("select questionId from judge_question order by questionId desc limit 1") JudgeQuestion findOnlyQuestionId(); @Insert("insert into judge_question(subject,question,answer,analysis,level,section) values " + "(#{subject},#{question},#{answer},#{analysis},#{level},#{section})") int add(JudgeQuestion judgeQuestion); @Select("select questionId from judge_question where subject=#{subject} order by rand() desc limit #{pageNo}") List<Integer> findBySubject(String subject,Integer pageNo); }