【发布时间】:2017-09-14 08:43:23
【问题描述】:
我正在做一个尝试学习百里香的小项目。 我有 3 个表 Timetables,其中包含一个 id,作为外键的 assignment id 和其他东西,Assignments 包含和 id 和作为外键的项目 id,以及包含和 id 和项目名称的 Projects。
我需要在 html 中显示时间表,这很简单,但是我想显示项目名称而不是分配 ID 列。
我的服务类中已经有一个选择,它调用所需的列,但是我如何告诉 thymeleaf 读取该列而不是在时间表中查找它。
这是我的代码
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";
}
【问题讨论】: