SpingBoot整合mybatis并使用Thymeleaf遍历listmap

对于Thymeleaf 模板引擎简介 与 Spring Boot 整合入门 。这里就不详细介绍。
需要入门的朋友,可以参考一下这一篇文章:Thymeleaf 模板引擎简介 与 Spring Boot 整合入门

首先,介绍一下springboot整合Mybatis返回 List<Map<String,Object>>。

Mybatis返回Map & List动态列数据集,需要在xml文件中的resultType都指定为Hashmap。

Mapper

 <mapper namespace="com.example.mapper.ExamQuestionsMapper">
  <select id="findExamRadioQuestions" 
   parameterType="int"
   resultType="java.util.HashMap">
   select q.id,q.subject,
        q.type, q.taotiid,
        q.optiona,q.optionb,
        q.optionc,q.optiond,
         q.answer,l.lessonname
        from  lesson l,questions q
        where
       q.taotiid=l.taotiid
       and
       q.type='单选'
       and
       l.id=#{value}
  </select>    
  .........
   public interface ExamQuestionsMapper {
    public List<Map<String,Object>> findExamRadioQuestions(Integer taotiid);
}

Service

public class ExamQuestionsServiceImpl implements ExamQuestionsService {
    //属性注入
    @Autowired
    private ExamQuestionsMapper examQuestionsMapper;

    @Override
    public List<Map<String,Object>> findExamRadioQuestions(Integer taotiid) {

        return examQuestionsMapper.findExamRadioQuestions(taotiid);
    }
    }

好了到这里,我们先测试我们返回的数据,进行遍历一下。

test

public class ExampleApplicationTests {
    @Autowired
    private ExamQuestionsService exampleQuestionsService;
    @Test
    public void contextLoads() {
        List<Map<String,Object>>  questionsList = exampleQuestionsService.findExamRadioQuestions(3);
        for (Map<String, Object> map : questionsList) {
            for (String s : map.keySet()) {
                System.out.print(map.get(s) + "  ");
            }
            System.out.println("-----------");
        }
        }}

效果:将所有数据读取出来

在这里插入图片描述
接下来是Controller

Controller

 //返回考试题目
    @RequestMapping("/toExamPageList")
    public String toExamPage(Integer lessonid,Model model) {
        //返回单选题目
        List<Map<String,Object>> questionsList  = exampleQuestionsService.findExamRadioQuestions(lessonid);
        model.addAttribute("radioQuestionsList", questionsList);
        //返回多选题目
        List<Map<String,Object>> questionsList2  = exampleQuestionsService.findExamCheckboxQuestions(lessonid);
        model.addAttribute("checkboxQuestionsList", questionsList2);
        //跳转到对应的html
        return "exampage";
    }
        

html

 <form action="toExamPage1" method="post" name="examform">
            <table cellpadding="20px">
                <tr>
                    <td><font style="font-weight:bold">一、单选题</font>(每题10分,答错不得分)</td>
                </tr>
                <tr th:each ="m,iterStat:${radioQuestionsList}" >
                    <td th:text="${iterStat.index+1}+'、'+ ${m.subject}"></td> <br><br>
                    <td><input type="radio" th:value="A" th:text="'A、'+${m.optionb}"</td>
                    <td><input type="radio" th:value="B" th:text="'B、'+${m.optionb}"></td>
                    <td><input type="radio" th:value="C" th:text="'C、'+${m.optionc}"></td>
                    <td><input type="radio" th:value="D" th:text="'D、'+${m.optiond}"></td>
                      </tr>

其中iterStat.index代表当前迭代步数。而如果你对于Thymeleaf中th:each不太熟悉。
这里推荐这位博主文章:Thymeleaf th:each 循环迭代与 th:if、th:switch 条件判断

最后是我实现的效果
在这里插入图片描述

发布了21 篇原创文章 · 获赞 9 · 访问量 4504

猜你喜欢

转载自blog.csdn.net/qq_36470898/article/details/99131376