1.在进行批量数据插入的时候,可能还要将数据插入到其他相关的表中
1.在进行批量数据插入的时候,可能还要将数据插入到其他相关的表中,为了保证数据插入的高效,并且防止其他的高并发(前提:mysql自增字段),适合于小批量,不是高并发
项目下载地址:http://pan.baidu.com/s/1i4LhmZz
相关的SQL文件:http://pan.baidu.com/s/1jHVU5bK
核心思路:
1.数据库进行批量插入,中间出现任何异常都回滚
2.貌似事务的处理逻辑是
- 1.在接口的时候定义异常
- 2.在service中实现,但是不能手动的try catch,直接将异常抛出来
- 3.在controller中对异常进行try catch,针对于不同的一场进行页面错误显示或者页面跳转
3.如果直接在service中抛一个异常,进行try catch,数据库不会回滚
核心代码:
@Transactional(propagation=Propagation.REQUIRED,rollbackFor={RuntimeException.class, Exception.class},timeout=30,isolation=Isolation.DEFAULT)
public int insertMultiUserFail(List<User> userList)throws Exception {
int num = -1;
int insertNum = userDao.insertMultiUser(userList);
if (insertNum == userList.size()) {
num = -1;//模拟数据库失败
throw new Exception();
}
return num;
}
@Transactional(propagation=Propagation.REQUIRED,rollbackFor={RuntimeException.class, Exception.class},timeout=30,isolation=Isolation.DEFAULT)
public int insertMultiUserSuccess(List<User> userList)throws Exception {
int num = -1;
int insertNum = userDao.insertMultiUser(userList);
if (insertNum == userList.size()) {
num = insertNum;//模拟数据库成功
}
return num;
}
2.在使用的时候遇到一个大牛的网站:(MyBatics不支持批量插入数据的Id返回,但是使用JDBC可以使用)
1.自增主键,UUID,和Oracle对比:http://www.cnblogs.com/w1217/p/5541631.html
2.官网支持:http://blog.csdn.net/u014336799/article/details/52023887
3.开源中国技术大牛:https://my.oschina.net/zudajun/blog/674946