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()
就是往数据库存以后的主键值
直接读取当前表主键,直接插入,SQL 如下:
假如多条记录,如A 为主表,字段a,B为另外一个表,字段为b
declare @Count int,@Start int,@b int
set @Start =0
select @Count=count(*) from A
while(@Start<@Count)
begin
select @b=a from A where a=@Start
insert into B (b) values (@b)
set @Start=@Start+1
end
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)