Thymeleaf如何根据另一个表中的值显示一个表中的列

时间:2017-09-14 08:43:23

标签: mysql thymeleaf

我正在研究一个试图学习百里香的小项目。 我有3个表时间表,其中包含一个id,作为外键和其他东西的赋值id,包含id和项目id作为外键的Assignments以及包含id和id和项目名称的Projects。

我需要在html中显示时间表,这很简单,但我想显示项目名称而不是分配ID列。

我的服务类中已经有一个select来调用所需的列但是我怎么能告诉thymeleaf读取该列而不是在Timetables中查找它。

这是我的代码

HTML

<table class="table table-bordered">
    <thead>
    <tr>
        <th>id</th>
        <th>assignment</th>
        <th>date</th>
        <th>number of hours</th>
    </tr>
    </thead>
    <tbody>
    <tr th:each="timetable: ${timetables}">
        <td th:text="${timetable.timetableId}">45</td>
        <td th:text="${timetable.assignmentId}">vasi</td>
        <td th:text="${timetable.date}">1 ian</td>
        <td th:text="${timetable.hoursWorked}">3000</td>
    </tr>
    </tbody>
</table>

服务类

@Autowired
JdbcTemplate template;

public List<Timetable> findAll(String loginname) {
    String sql = "  SELECT timetables.timetableId, timetables.assignmentId, timetables.date, timetables.hoursWorked, users.username, projects.projectName FROM timetables INNER join assignments on assignments.assignmentId = timetables.assignmentId INNER JOIN users on users.userId = assignments.userId " +
            "INNER JOIN projects on assignments.projectId = projects.projectId where username= ?";

    RowMapper<Timetable> rm = new RowMapper<Timetable>() {
        @Override
        public Timetable mapRow(ResultSet resultSet, int i) throws SQLException {
            Timetable timetable = new Timetable(resultSet.getInt("timetableId"),
                    resultSet.getInt("assignmentId"),
                    resultSet.getDate("date"),
                    resultSet.getInt("hoursWorked"));

            return timetable;
        }
    };

    return template.query(sql, rm, loginname);
}

控制器

@Autowired
TimetableService service;

@Autowired
AssignmentsService serv;

@RequestMapping(value = {"/Timetable"}, method = RequestMethod.GET)
public String index(Model md) {
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    String loginname = auth.getName();
    md.addAttribute("timetables", service.findAll(loginname));
    return "Timetable";
}

1 个答案:

答案 0 :(得分:1)

我不熟悉JdbcTemplate,但我想你需要做以下

如果您还没有在mapRow

中添加projectName,请添加它
RowMapper<Timetable> rm = new RowMapper<Timetable>() {
        @Override
        public Timetable mapRow(ResultSet resultSet, int i) throws SQLException {
            Timetable timetable = new Timetable(resultSet.getInt("timetableId"),
                    resultSet.getInt("assignmentId"),
                    resultSet.getDate("date"),
                    resultSet.getString("projectName"),
                    resultSet.getInt("hoursWorked"));

            return timetable;
        }
    };

然后,在html页面中添加项目名称

<tbody>
   <tr th:each = "timetable: ${timetables}">
         <td th:text="${timetable.timetableId}">45</td>
         <td th:text="${timetable.projectName}">vasi</td>
         <td th:text="${timetable.date}">1 ian</td>
         <td th:text="${timetable.hoursWorked}">3000</td>
   </tr>
</tbody>