配置Bean

<!-- 配置一个可以执行批量的SqlSession -->
<bean id="mySqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
    <constructor-arg name="sqlSessionFactory" ref="userSqlSessionFactory"/>
    <constructor-arg name="executorType" value="BATCH"/>
</bean>

其中:

userSqlSessionFactory 为提前配置的 SqlSessionFactoryBean

value选择BATCH(批量)

声明SqlSession

@Autowired
private SqlSession sqlSession;

UUID做用户名

String userName = null;
EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);
// 批量插入1000条数据
for (int i = 0; i < 1000; i++) {
    userName = UUID.randomUUID().toString().substring(0, 5) + i;
    mapper.insertSelective(new Employee(null, userName, "男", userName + "@ssm.com", 1, null));
}

分类: SSM

Deng Jia

Keep Moving.

0 条评论

发表评论

电子邮件地址不会被公开。