V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
jwh199588
V2EX  ›  数据库

怎么在 mybatis 中插入数据

  •  
  •   jwh199588 · Apr 24, 2017 · 1725 views
    This topic created in 3291 days ago, the information mentioned may be changed or developed.

    insert into questionBank
    <if test="list != null and list.size &gt; 0">
    <foreach collection="list" item="myList" separator=",">
    <if test="myList != null">
    (
    <if test="myList.pdId !=null">pdid</if>
    <if test="myList.answer != null"> ,answer</if>
    <if test="myList.testType != null"> ,testType</if>
    <if test="myList.questionTitle != null">,questionTitle</if>
    ) values(
    <if test="myList.pdId !=null"> #{myList.pdId}</if>
    <if test="myList.answer != null"> , #{myList.answer}</if>
    <if test="myList.testType != null"> , #{myList.testType}</if>
    <if test="myList.questionTitle != null"> , #{myList.questionTitle}</if>
    ) </if></foreach></if>

      这样写不行吗,因为我要判断参数中的 key 是否为空,才好插入数据,所以对 key 进行判断
    
    5 replies    2017-04-24 10:36:11 +08:00
    jwh199588
        2
    jwh199588  
    OP
       Apr 24, 2017
    @sunjiayao 很感谢你,大但是你看到我的 sql 语句了吗,我的 key 和 values 全部都是需要判断的,不然 key 存在,但是没有值的话 sql 语句也是不成立的啊
    luman
        3
    luman  
       Apr 24, 2017
    https://dev.mysql.com/doc/refman/5.7/en/insert.html
    sorry ,确实没仔细看。你应该看这个文档
    jason19659
        4
    jason19659  
       Apr 24, 2017
    @sunjiayao #3 6666666
    freelee
        5
    freelee  
       Apr 24, 2017
    多行插入应该是 insert into(字段 1) values(值 1),(值 2)吧,你这字段也有两遍,当然有问题
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5596 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 38ms · UTC 08:45 · PVG 16:45 · LAX 01:45 · JFK 04:45
    ♥ Do have faith in what you're doing.