介绍:因为项目框架的存在未知的版本号org错误,导致使用mybatis自带的常规以list集合和array数组的形式批量插入不可行,因此解决办法是:自行创建map以"list"为map的键,要批量输入的list对象为值,再调用mybaits的getSqlSession().insert()方法即可.
1,首先创建抽象类AbstracTestDao继承SqlSessionDaoSupport
import javax.annotation.Resource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.support.SqlSessionDaoSupport;
public abstract class AbstractTestDao extends SqlSessionDaoSupport{
@Resource
public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory){
super.setSqlSessionFactory(sqlSessionFactory);
}
}
public interface TestDaos {
//@Param("list")
void ib(List clist);
}
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Repository;
import com.allinfinance.cis.dao.impl.AbstractTestDao;
import com.allinfinance.cis.infrastructure.TmCisCustIdx;
import com.allinfinance.cis.zm.dao.TestDaos;
@Repository("testDao")
public class TestDao extends AbstractTestDao implements TestDaos{
public void ib(List clist){
Map> map = new HashMap>();
map.put("list",clist);//将传来的list参数放进map中,因为mybits的批量mapping文件中的for循环的collection的值key固定为"list",所以map的key也为list,如果参数是数组,key则为"array"
getSqlSession().insert("com.allinfinance.cis.infrastructure.mapping.TmCisCustIdxMapper.insertBatch",map);//mapper文件对应的id方法
}
}
insert into TM_CIS_CUST_IDX ( ORG,CUST_ID,BIZ_ORG_CODE,ID_TYPE,ID_NO,CREATE_DATE,CREATE_BY,UPDATE_DATE,UPDATE_BY,JPA_VERSION )
values
select #{item.org,jdbcType=VARCHAR } ,#{item.custId,jdbcType=VARCHAR } , #{item.bizOrgCode,jdbcType=VARCHAR } ,
#{item.idType,jdbcType=VARCHAR } , #{item.idNo,jdbcType=VARCHAR } , #{item.createDate,jdbcType=TIMESTAMP } ,
#{item.createBy,jdbcType=VARCHAR } ,#{item.updateDate,jdbcType=TIMESTAMP } ,
#{item.updateBy,jdbcType=VARCHAR } , #{item.jpaVersion,jdbcType=INTEGER }from dual
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring-context.xml")
public class TestTest{
@Autowired
private TmCisCustIdxDao tmCisCustIdxDao;
@Autowired
private TestDaos testDao;
@Before
public void setUp() throws Exception {
}
@Test
public void test() {
TmCisCustIdx tmCisCustIdx = new TmCisCustIdx();
String custId = UUID.randomUUID().toString();
Date date = new Date();
tmCisCustIdx.setCustId(custId);
tmCisCustIdx.setOrg("01");
tmCisCustIdx.setBizOrgCode("TLJR");
tmCisCustIdx.setIdNo("6200000CP");
tmCisCustIdx.setIdType("I");
tmCisCustIdx.setCreateBy("admin");
tmCisCustIdx.setCreateDate(date);
tmCisCustIdx.setUpdateBy("admin");
tmCisCustIdx.setUpdateDate(date);
tmCisCustIdx.setJpaVersion(0);
List clist = new ArrayList();
clist.add(tmCisCustIdx);
testDao.ib(clist);
}
}
}