25,980
社区成员
发帖
与我相关
我的任务
分享
<insert id="addTrainRecordBatch" useGeneratedKeys="true" parameterType="java.util.List">
<selectKey resultType="long" keyProperty="id" order="AFTER">
SELECT
LAST_INSERT_ID()
</selectKey>
insert into t_train_record (add_time,emp_id,activity_id,flag)
values
<foreach collection="list" item="item" index="index" separator="," >
(#{item.addTime},#{item.empId},#{item.activityId},#{item.flag})
</foreach>
</insert>
具体可以Google样例。
感觉mybatis的批量插入xml定义不太靠谱,小的还可以...大的就算了吧...
我都是自己另开jdbctemplate底层,然后注入数据源,用jdbc的批处理随便折腾@Service
public class TestService implements ITestService
{
public static void main(String[] args)
{
ApplicationContext context = new ClassPathXmlApplicationContext(
"applicationContext.xml");
ITestService testService = context.getBean(ITestService.class);
testService.test(0, "");
}
@Resource(name = "sqlSession")
SqlSession sqlSession;
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void test(int userId, String ip)
{
// TODO Auto-generated method stub
long now = System.currentTimeMillis();
ITestDao testDao = this.sqlSession.getMapper(ITestDao.class);
for (int id = 1; id < 10001; id++)
{
String name = "name_" + id;
Test test = new Test(id, name);
testDao.add(test);
}
System.out.println(System.currentTimeMillis() - now);
}
}
如果你的方法是A,有B调用A的话,A和B的ExecutorType是要相同,不同的话A方法需要开启一个新的事务


