怎么在 mybatis 中插入数据

2017-04-24 09:19:55 +08:00
 jwh199588

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 进行判断
1079 次点击
所在节点    数据库
5 条回复
sunjiayao
2017-04-24 09:36:51 +08:00
jwh199588
2017-04-24 09:43:04 +08:00
@sunjiayao 很感谢你,大但是你看到我的 sql 语句了吗,我的 key 和 values 全部都是需要判断的,不然 key 存在,但是没有值的话 sql 语句也是不成立的啊
sunjiayao
2017-04-24 09:48:56 +08:00
https://dev.mysql.com/doc/refman/5.7/en/insert.html
sorry ,确实没仔细看。你应该看这个文档
jason19659
2017-04-24 10:19:47 +08:00
@sunjiayao #3 6666666
freelee
2017-04-24 10:36:11 +08:00
多行插入应该是 insert into(字段 1) values(值 1),(值 2)吧,你这字段也有两遍,当然有问题

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/356885

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX