Mybatis批量插入返回成功的数目

Mybatis批量插入返回影响的行数

环境:

  • postgresql 9.6.5
  • spring 4.1
  • mybatis3
  • junit4
  • log4j

ThesisMapper.xml:

<!-- 批量插入 -->
    <insert id="insertList" parameterType="java.util.List">

        insert into public.thesis
        (name)
        values
        <foreach collection="list" item="t" index="index" separator=",">
            (
            #{t.name}
            )
        </foreach>
    </insert>

Mapper.java 借口:

public interface ThesisMapper {
    int insertList(List<Thesis> thesisList);
}

服务类:
ThesisService:

public int insertList(List<Thesis> thesisList) throws Exception {
    return thesisDao.insertList(thesisList);
}

测试父类:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:spring-mvc.xml", "classpath:spring-mybatis.xml" })
@WebAppConfiguration
public class BaseTest {

    @Autowired
    protected WebApplicationContext wac;

    @Test
    public void test() {}

}

测试类:

public class UserOpsTest extends BaseTest {

    @Autowired
    private ThesisService ts;

    @Test
    public void insertListTest() {
        List<Thesis> thesisList = new ArrayList<Thesis>();

        Thesis t1 = new Thesis();
        Thesis t2 = new Thesis();
        Thesis t3 = new Thesis();

        t1.setName("qq1");
        t2.setName("ww2");
        t3.setName("asd");

        thesisList.add(t1);
        thesisList.add(t2);
        thesisList.add(t3);

        try {
            System.out.println(ts.insertList(thesisList));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

日志输出:

[DEBUG] ==>  Preparing: insert into public.thesis ( name) values ( ? ) 
  [DEBUG] ==> Parameters: qq1(String), ww2(String), asd(String)
  [DEBUG] <==    Updates: 3
  3

返回结果既为所求.

源码地址:
https://github.com/timo1160139211/trans

  • 4
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值