员工类还有部门对象,在更新时sql语句该如何填写呢,传入的参数为员工对象,即关联的修改语句该怎么写呢
public class Department {//这是部门类
private Integer id;
private String departmentName;
}
public class Employee {//员工类
private Integer id;
private String lastName;
private String email;
private Integer gender;
private Department department;
private Integer did;//did对应部门的id
private Date birth;
}
public interface EmployeeMapper {//mapper接口
public void save(Employee employee);}
```xml
<update id="save" parameterType="Employee">
update employee
set lastName=#{lastName},email=#{email},gender=#{gender},birth=#{birth},
did=#{did},department.departmentName=#{department.departmentName}
where id=#{id}
update>
我在网上找的大多是多表的查询sql语句怎么写,但这多表更新语句又该怎么写呢
一条SQL做不到同时修改两张表的数据,除非你将两个修改逻辑先写成存储过程再调用存储过程,不过这不现实。将两张表分别修改。
分开更新
<update id="save" parameterType="Employee">
update employee
set lastName=#{lastName},email=#{email},gender=#{gender},birth=#{birth},
did=#{did}
where id=#{id};
update department
set departmentName=#{departmentName}
where id=#{id}
</update>
试试,要求 Employee 要有 departmentName 这个字段
test