我正在做一个试图学习百里香的小项目。
我有3个表时间表,其中包含一个ID,一个作为外键和其他东西的工作分配ID,包含和一个ID以及作为外键的项目ID的工作分配和一个包含ID和项目名称的项目。

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

我的服务类别中已经有一个选择,该选择调用了必填列,但是如何告诉百里香叶读取该列而不是在时间表中查找。

这是我的代码

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";
}

最佳答案

我不熟悉JdbcTemplate,但我想您需要执行以下操作

如果尚未将projectName添加到Timetable类中,请在mapRow中添加

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>

10-06 14:11
查看更多