1 回答
TA贡献1818条经验 获得超3个赞
Controller:
@RequestMapping("/saveEmployee")
public String saveEmployee(Employee employee) {
Employee e=new Employee();
e.setEmployee_name("张无忌");
e.setDep_id(1);
EmployeeDetail detail=new EmployeeDetail();
e.setEmployeeDetail(detail);
int r=employeeService.saveEmployee(e);
if(r>0) {
}
return null;
}
Service:
Service实现类:
@Transactional
public int saveEmployee(Employee e) {
int r=employeeMapper.saveEmployee(e);
EmployeeDetail d=e.getEmployeeDetail();
d.setId(e.getId());
return employeeDetailMapper.saveEmployeeDetail(d);
}
mapper文件:
<!-- 保存Employee对象 -->
<insert id="saveEmployee" parameterType="com.ssm.relation.pojo.Employee" useGeneratedKeys="true" keyProperty="id" keyColumn="id">
insert into employee(id,employee_name,dep_id) values(e_seq.nextval,#{employee_name},#{dep_id})
</insert>
在Service中开启一个事务,执行完int r=employeeMapper.saveEmployee(e);这一行以后这个对象的主键就已经有值了,并且就是主键。因此e.getId()
就是往数据库存以后的主键值
- 1 回答
- 0 关注
- 1117 浏览
添加回答
举报