V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
jwh199588
V2EX  ›  数据库

怎么在 mybatis 中插入数据

  •  
  •   jwh199588 · 2017-04-24 09:19:55 +08:00 · 1066 次点击
    这是一个创建于 2530 天前的主题,其中的信息可能已经有所发展或是发生改变。

    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 条回复    2017-04-24 10:36:11 +08:00
    sunjiayao
        1
    sunjiayao  
       2017-04-24 09:36:51 +08:00
    jwh199588
        2
    jwh199588  
    OP
       2017-04-24 09:43:04 +08:00
    @sunjiayao 很感谢你,大但是你看到我的 sql 语句了吗,我的 key 和 values 全部都是需要判断的,不然 key 存在,但是没有值的话 sql 语句也是不成立的啊
    sunjiayao
        3
    sunjiayao  
       2017-04-24 09:48:56 +08:00
    https://dev.mysql.com/doc/refman/5.7/en/insert.html
    sorry ,确实没仔细看。你应该看这个文档
    jason19659
        4
    jason19659  
       2017-04-24 10:19:47 +08:00
    @sunjiayao #3 6666666
    freelee
        5
    freelee  
       2017-04-24 10:36:11 +08:00
    多行插入应该是 insert into(字段 1) values(值 1),(值 2)吧,你这字段也有两遍,当然有问题
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3389 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 11:17 · PVG 19:17 · LAX 04:17 · JFK 07:17
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.