此处只是用来记录自己在学习java的问题(本人是做php的,因为php与java的却别比较大,所以可能有些简单的问题就会困扰自己,因此在此记录下来)
平时项目开发中往往会遇到这样的问题就是一个表单的数据需要进行拆分并同时插入多个关联表中。
一下是我的解决方案,有更好的解决方案,烦请各位大神、大咖不吝赐教发在评论中,小弟在此谢谢各位了.
先看控制的写法:
// 首先实例化对应的实体类(存放get和set的类)(bean层下面的类)
Test test = new test();
TestTwo testTwo = new TestTwo();
// 对属性进行赋值
test.setName("张三");
test.setSex(1);
// 这是两个不同的类 由于是测试的所以使用的是同样的字段,在testTwo中会有一个test的主键ID
testTwo.setName("李四");
testTwo.setSex(1);
// 将对象添加Map中
Map<String, Object> map = new HashMap<String, Object>();
map.put("test", test);
map.put("testTwo", testTwo);
testService.addTest(map);
Dao层接口的写法:
package com.ssm.dao;
import com.ssm.bean.testInfo;
import java.util.List;
public interface ITestDao {
/**
* 添加信息
* @param testInfo
*/
public int addTest(TestInfo testInfo);
}
service层接口的写法:
package com.ssm.service;
import com.ssm.bean.TestInfo;
import java.util.List;
import java.util.Map;
public interface ITestService {
/**
* 添加信息
* @param objectMap
*/
public int addTest(Map<String, Object> objectMap);
}
// testTwo
package com.ssm.service;
import com.ssm.bean.TestTwoInfo;
public interface ITestTwoService {
/**
* 添加商户员工信息
* @param bizStaffInfo
*/
public int AddTestTwo(TestTwoInfo testTwoInfo);
}
service接口的具体实现类
package com.ssm.service.impl;
import com.ssm.bean.TestInfo;
import com.ssm.bean.TestTwoInfo;
import com.ssm.dao.ITestDao;
import com.ssm.dao.ITestTwoDao;
import com.ssm.service.ITestService ;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
@Service
public class TestService implements ITestService {
@Autowired
private ITestDao testDao;
@Autowired
private ITestTwoDao testTwoDao;
/**
* 添加商户信息
* 开启事务
* @param objectMap
*/
@Transactional(rollbackFor = Exception.class)
@Override
public int addTest(Map<String, Object> objectMap) {
int id = 0;
TestInfo testInfo = (TestInfo) objectMap.get("test");
String now_time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
testInfo.setCreated_at(Timestamp.valueOf(now_time));
testInfo.setUpdated_at(Timestamp.valueOf(now_time));
id = testDao.addTest(testInfo);
if (member_id > 0) {
TestTwoInfo testTwoInfo = (TestTwoInfo) objectMap.get("testTwo");
testTwoInfo.setTest_id(id);
testTwoInfo.setUpdated_at(Timestamp.valueOf(now_time));
testTwoInfo.setCreated_at(Timestamp.valueOf(now_time));
int testTwo_id = 0;
testTwo_id = testTwoDao.AddTestTwo(testTwoInfo);
if (testTwo_id < 0) {
throw new RuntimeException("新增testTwo失败");
}
} else {
throw new RuntimeException("新增test失败");
}
return id;
}
}
// testTwo
package com.ssm.service.impl;
import com.ssm.bean.TestTwoInfo;
import com.ssm.dao.ITestTwoDao;
import com.ssm.service.ITestTwoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class BizStaffService implements IBizStaffService {
@Autowired
private ITestTwoDao testTwoDao;
/**
* 添加商户员工信息
* @param testTwoInfo
*/
@Override
public int AddTestTwof(TestTwoInfo testTwoInfo) {
return testTwoDao.AddTestTwo(testTwoInfo);
}
}
mybatis的maooer,xml此处就不提供了,相信大家都是没有问题。
各位大神有什么更好的解决方案希望能够提供给小弟