建表sql:
CREATE TABLE employee
(
id INT(8) PRIMARY KEY AUTO_INCREMENT ,
lastName VARCHAR(25),
email VARCHAR(64),
gender INT(1),
department_id INT,
birthday DATE,
CONSTRAINT FK_department_id FOREIGN KEY(department_id) REFERENCES department(id) ON DELETE NO ACTION ON UPDATE NO ACTION
)ENGINE=INNODB DEFAULT CHARSET =utf8;
CREATE TABLE department
(
id INT PRIMARY KEY,
departmentName VARCHAR(64)
)ENGINE=INNODB DEFAULT CHARSET=utf8;
on null context object 异常
做查询所有员工功能时,一直报这个错误,网上找了半天,也没搞定。
这个错误根本原因还是上下文对象为null导致的。
Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1011E: Method call: Attempted to call method getId() on null context object
------------------
2020-04-10 18:21:59.581 ERROR 30800 --- [nio-8088-exec-9] s.e.ErrorMvcAutoConfiguration$StaticView : Cannot render error page for request [/wang/table] and exception [Exception evaluating SpringEL expression: "emp.getId()" (template: "basic-table" - line 51, col 29)] as the response has already been committed. As a result, the response may have the wrong status code.
<tr th:each="emp : ${employeeList}">
<td th:text="${emp.getId()}" ></td>
<td th:text="${emp.getLastName()}"></td>
<td th:text="${emp.getEmail()}">12 May 2017</td>
<!-- thymeleaf的三元表达式-->
<td th:text="${emp.getGender()==0?'女':(emp.getGender()==1 ? '男':null)}">12 May 2017</td>
<td th:text="${emp.getDepartment()!=null?emp.getDepartment().getDepartmentName():null}">12 May 2017</td>
<!-- 日期格式转换: #dates是thymeleaf提供的一个工具,它能帮我们展示想要的日期格式-->
<td th:text="${#dates.format(emp.getBirthday(),'yyyy-MM-dd HH:mm:ss')}">Pending</label></td>
<td >
<!-- 在修改的时候,我们需要把员工id传递给后台,后台得到员工的详细信息,之后进入修改页面,这里使用的restful风格. -->
<a th:href="@{/employee/toUpdatePage/} + ${emp.getId()}" class="btn btn-sm btn-primary" th:text="修改" ></a>
<a th:href="@{/employee/delete/} + ${emp.getId()}" class="btn btn-sm btn-danger" th:text="删除"></a>
</td>
</tr>
前端代码:之前已经验证过,没有问题,那么根本原因还是在后端,后端没有正确传递数据。
最后果然,是因为我的mapper中的sql写的不对。
错误版本:
由于一开始,我的对象和table的字段一致,我以为就不要写column 和result对应了。其实还是太年轻。。。。
<select id="queryAllEmployee" resultMap="employeeMap">
select e.id as eId,d.id as dId ,e.email,e.birthday,e.gender,e.lastName,d.departmentName
from employee e ,department d
where e.department_id = d.id
</select>
<resultMap id="employeeMap" type="employee">
<association property="department" javaType="department">
</association>
</resultMap>
正确版本:
<select id="queryAllEmployee" resultMap="employeeMap">
select e.id as eId,d.id as dId ,e.email,e.birthday,e.gender,e.lastName,d.departmentName
from employee e ,department d
where e.department_id = d.id
</select>
<resultMap id="employeeMap" type="employee">
<result column="eId" property="id"></result>
<result column="email" property="email"></result>
<result column="lastName" property="lastName"></result>
<result column="birthday" property="birthday"></result>
<result column="gender" property="gender"></result>
<association property="department" javaType="department">
<result column="dId" property="id"></result>
<result column="departmentName" property="departmentName"></result>
</association>
</resultMap>