查看: 271|回复: 0

[Java代码] mybatis动态插入list传入List参数的实例代码

发表于 2018-5-4 08:00:02

mybatis动态插入list的实例代码如下所述:

  1. <insert id="savePrpcitemkindList" parameterType="java.util.List">
  2. insert into prpcitemkind
  3. (RISKCODE, ITEMKINDNO, FAMILYNO, FAMILYNAME, PROJECTCODE, CLAUSECODE,
  4. CLAUSENAME,
  5. KINDCODE, KINDNAME, ITEMNO, ITEMCODE, ITEMDETAILNAME, GROUPNO, MODECODE,
  6. MODENAME, STARTDATE,
  7. STARTHOUR, ENDDATE, ENDHOUR, MODEL, BUYDATE, ADDRESSNO, CALCULATEFLAG, CURRENCY,
  8. UNITAMOUNT,
  9. QUANTITY, UNIT, VALUE, AMOUNT, RATEPERIOD, RATE, SHORTRATEFLAG, SHORTRATE,
  10. PREPREMIUM, CALPREMIUM,
  11. BASEPREMIUM, BENCHMARKPREMIUM, DISCOUNT, ADJUSTRATE, UNITPREMIUM, PREMIUM,
  12. DEDUCTIBLERATE, DEDUCTIBLE,
  13. FLAG, INSERTTIMEFORHIS, OPERATETIMEFORHIS, TAXRATE, TAXFLAG, TAXFEE,
  14. NETPREMIUM, TAXFEE_GB, TAXFEE_LB, ALLTAXFEE, ALLNETPREMIUM,PROPOSALNO)
  15. <foreach collection="list" item="item" index="index" separator="union all">
  16. <trim prefix="select" suffix="from dual">
  17. <choose>
  18. <when test="item.riskcode != null">#{item.riskcode},</when>
  19. <otherwise>null,</otherwise>
  20. </choose>
  21. <choose>
  22. <when test="item.itemkindno != null">#{item.itemkindno},</when>
  23. <otherwise>null,</otherwise>
  24. </choose>
  25. <choose>
  26. <when test="item.familyno != null">#{item.familyno},</when>
  27. <otherwise>null,</otherwise>
  28. </choose>
  29. <choose>
  30. <when test="item.familyname != null">#{item.familyname},</when>
  31. <otherwise>null,</otherwise>
  32. </choose>
  33. <choose>
  34. <when test="item.projectcode != null">#{item.projectcode},</when>
  35. <otherwise>null,</otherwise>
  36. </choose>
  37. <choose>
  38. <when test="item.clausecode != null">#{item.clausecode},</when>
  39. <otherwise>null,</otherwise>
  40. </choose>
  41. <choose>
  42. <when test="item.clausename != null">#{item.clausename},</when>
  43. <otherwise>null,</otherwise>
  44. </choose>
  45. <choose>
  46. <when test="item.kindcode != null">#{item.kindcode},</when>
  47. <otherwise>null,</otherwise>
  48. </choose>
  49. <choose>
  50. <when test="item.kindname != null">#{item.kindname},</when>
  51. <otherwise>null,</otherwise>
  52. </choose>
  53. <choose>
  54. <when test="item.itemno != null">#{item.itemno},</when>
  55. <otherwise>null,</otherwise>
  56. </choose>
  57. <choose>
  58. <when test="item.itemcode != null">#{item.itemcode},</when>
  59. <otherwise>null,</otherwise>
  60. </choose>
  61. <choose>
  62. <when test="item.itemdetailname != null">#{item.itemdetailname},</when>
  63. <otherwise>null,</otherwise>
  64. </choose>
  65. <choose>
  66. <when test="item.groupno != null">#{item.groupno},</when>
  67. <otherwise>null,</otherwise>
  68. </choose>
  69. <choose>
  70. <when test="item.modecode != null">#{item.modecode},</when>
  71. <otherwise>null,</otherwise>
  72. </choose>
  73. <choose>
  74. <when test="item.modename != null">#{item.modename},</when>
  75. <otherwise>null,</otherwise>
  76. </choose>
  77. <choose>
  78. <when test="item.startdate != null">#{item.startdate},</when>
  79. <otherwise>null,</otherwise>
  80. </choose>
  81. <choose>
  82. <when test="item.starthour != null">#{item.starthour},</when>
  83. <otherwise>null,</otherwise>
  84. </choose>
  85. <choose>
  86. <when test="item.enddate != null">#{item.enddate},</when>
  87. <otherwise>null,</otherwise>
  88. </choose>
  89. <choose>
  90. <when test="item.endhour != null">#{item.endhour},</when>
  91. <otherwise>null,</otherwise>
  92. </choose>
  93. <choose>
  94. <when test="item.model != null">#{item.model},</when>
  95. <otherwise>null,</otherwise>
  96. </choose>
  97. <choose>
  98. <when test="item.buydate != null">#{item.buydate},</when>
  99. <otherwise>null,</otherwise>
  100. </choose>
  101. <choose>
  102. <when test="item.addressno != null">#{item.addressno},</when>
  103. <otherwise>null,</otherwise>
  104. </choose>
  105. <choose>
  106. <when test="item.calculateflag != null">#{item.calculateflag},</when>
  107. <otherwise>null,</otherwise>
  108. </choose>
  109. <choose>
  110. <when test="item.currency != null">#{item.currency},</when>
  111. <otherwise>null,</otherwise>
  112. </choose>
  113. <choose>
  114. <when test="item.unitamount != null">#{item.unitamount},</when>
  115. <otherwise>null,</otherwise>
  116. </choose>
  117. <choose>
  118. <when test="item.quantity != null">#{item.quantity},</when>
  119. <otherwise>null,</otherwise>
  120. </choose>
  121. <choose>
  122. <when test="item.unit != null">#{item.unit},</when>
  123. <otherwise>null,</otherwise>
  124. </choose>
  125. <choose>
  126. <when test="item.value != null">#{item.value},</when>
  127. <otherwise>null,</otherwise>
  128. </choose>
  129. <choose>
  130. <when test="item.amount != null">#{item.amount},</when>
  131. <otherwise>null,</otherwise>
  132. </choose>
  133. <choose>
  134. <when test="item.rateperiod != null">#{item.rateperiod},</when>
  135. <otherwise>null,</otherwise>
  136. </choose>
  137. <choose>
  138. <when test="item.rate != null">#{item.rate},</when>
  139. <otherwise>null,</otherwise>
  140. </choose>
  141. <choose>
  142. <when test="item.shortrateflag != null">#{item.shortrateflag},</when>
  143. <otherwise>null,</otherwise>
  144. </choose>
  145. <choose>
  146. <when test="item.shortrate != null">#{item.shortrate},</when>
  147. <otherwise>null,</otherwise>
  148. </choose>
  149. <choose>
  150. <when test="item.prepremium != null">#{item.prepremium},</when>
  151. <otherwise>null,</otherwise>
  152. </choose>
  153. <choose>
  154. <when test="item.calpremium != null">#{item.calpremium},</when>
  155. <otherwise>null,</otherwise>
  156. </choose>
  157. <choose>
  158. <when test="item.basepremium != null">#{item.basepremium},</when>
  159. <otherwise>null,</otherwise>
  160. </choose>
  161. <choose>
  162. <when test="item.benchmarkpremium != null">#{item.benchmarkpremium},</when>
  163. <otherwise>null,</otherwise>
  164. </choose>
  165. <choose>
  166. <when test="item.discount != null">#{item.discount},</when>
  167. <otherwise>null,</otherwise>
  168. </choose>
  169. <choose>
  170. <when test="item.adjustrate != null">#{item.adjustrate},</when>
  171. <otherwise>null,</otherwise>
  172. </choose>
  173. <choose>
  174. <when test="item.unitpremium != null">#{item.unitpremium},</when>
  175. <otherwise>null,</otherwise>
  176. </choose>
  177. <choose>
  178. <when test="item.premium != null">#{item.premium},</when>
  179. <otherwise>null,</otherwise>
  180. </choose>
  181. <choose>
  182. <when test="item.deductiblerate != null">#{item.deductiblerate},</when>
  183. <otherwise>null,</otherwise>
  184. </choose>
  185. <choose>
  186. <when test="item.deductible != null">#{item.deductible},</when>
  187. <otherwise>null,</otherwise>
  188. </choose>
  189. <choose>
  190. <when test="item.flag != null">#{item.flag},</when>
  191. <otherwise>null,</otherwise>
  192. </choose>
  193. <choose>
  194. <when test="item.inserttimeforhis != null">#{item.inserttimeforhis},</when>
  195. <otherwise>null,</otherwise>
  196. </choose>
  197. <choose>
  198. <when test="item.operatetimeforhis != null">#{item.operatetimeforhis},</when>
  199. <otherwise>null,</otherwise>
  200. </choose>
  201. <choose>
  202. <when test="item.taxrate != null">#{item.taxrate},</when>
  203. <otherwise>null,</otherwise>
  204. </choose>
  205. <choose>
  206. <when test="item.taxflag != null">#{item.taxflag},</when>
  207. <otherwise>null,</otherwise>
  208. </choose>
  209. <choose>
  210. <when test="item.taxfee != null">#{item.taxfee},</when>
  211. <otherwise>null,</otherwise>
  212. </choose>
  213. <choose>
  214. <when test="item.netpremium != null">#{item.netpremium},</when>
  215. <otherwise>null,</otherwise>
  216. </choose>
  217. <choose>
  218. <when test="item.taxfee_gb != null">#{item.taxfee_gb},</when>
  219. <otherwise>null,</otherwise>
  220. </choose>
  221. <choose>
  222. <when test="item.taxfee_lb != null">#{item.taxfee_lb},</when>
  223. <otherwise>null,</otherwise>
  224. </choose>
  225. <choose>
  226. <when test="item.alltaxfee != null">#{item.alltaxfee},</when>
  227. <otherwise>null,</otherwise>
  228. </choose>
  229. <choose>
  230. <when test="item.allnetpremium != null">#{item.allnetpremium},</when>
  231. <otherwise>null,</otherwise>
  232. </choose>
  233. #{item.proposalno}
  234. </trim>
  235. </foreach>
  236. </insert>
复制代码

Mybatis 传入List参数的实例代码如下所述:

java代码

  1. //dao 代码 如果传入的参数是数组,也一样
  2. void insertDefaultMsg(@Param("idsList") ArrayList<String> idsList);
  3. //xml文件
  4. <update id="insertDefaultMsg" parameterType="java.util.List">
  5. UPDATE
  6. plf_pur_delivery_plan p
  7. SET
  8. p.promise_delivery_amount = p.quantity_req,
  9. p.promise_delivery_time = p.quantity_time
  10. WHERE p.id IN
  11. <foreach item="item" collection="idsList" separator="," open="(" close=")" index="">
  12. #{item, jdbcType=VARCHAR}
  13. </foreach>
  14. </update>
  15. //如果传入字符串
  16. //service 代码
  17. Map<String, String> map = bean.getDatas().get(0);
  18. String ids = map.get("ids");
  19. String[] idsArr = ids.split(",");
  20. String inId = "";
  21. for (int i = 0; i < idsArr.length; i++) {
  22. if (i != idsArr.length - 1) {
  23. inId += "'" + idsArr[i] + "',";
  24. } else {
  25. inId += "'" + idsArr[i] + "'";
  26. }
  27. }
  28. List<Map<String, String>> list = purDeliveryPlanDao
  29. .getPlanListManual(inId);
  30. //dao层代码
  31. List<Map<String, String>> getPlanListManual(@Param("inId") String inId);
  32. //xml 文件
  33. <select id="getPlanListManual" parameterType="java.lang.String" resultType="java.util.Map">
  34. SELECT
  35. p.supplier_code,
  36. p.pur_order_no,
  37. IF(
  38. p.update_times > 0,
  39. '修改订单',
  40. '新订单'
  41. ) AS order_type
  42. FROM
  43. plf_pur_delivery_plan p
  44. WHERE p.is_notice = '0'
  45. AND p.is_delete = '1'
  46. AND p.id in(<![CDATA[${inId}]]>)
  47. GROUP BY p.supplier_code,
  48. p.pur_order_no
  49. ORDER BY p.supplier_code
  50. </select>
复制代码

总结

以上所述是小编给大家介绍的mybatis动态插入list传入List参数的实例代码,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!



回复

使用道具 举报