package com.cesgroup.mybatis.homework;

import cn.hutool.core.util.IdUtil;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.UUID;

/**
 * @author ytj
 * @date 2019/12/13 10:29
 */
@Service
public class PersonService {
    private SqlSession sqlSession;

    public PersonService(SqlSessionFactory sqlSessionFactory) {
        sqlSession = new SqlSessionTemplate(sqlSessionFactory, ExecutorType.BATCH) ;
    }

    public int batchInsertCity(List<Person> list){
        PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class);
        for (Person person : list) {
            person.setId(String.valueOf(UUID.randomUUID()));
            personMapper.insertPerson(person);
        }
        return list.size();
    }
}
