java Mybatis+MySQL 批量插入、批量更新、批量查詢

隨筆記錄。親測可用

1.批量查詢

mapper

int insertBrandBusinessByExcel(List<Label> list);

.xml

  <!--批量插入-->
  <insert id="insertBrandBusinessByExcel" parameterType="com.mj.linen.pojo.label.Label">
      insert into tb_label (ecp,pid,ehco)<!--對應字段-->
      values 
      <foreach collection="list" separator="," item="label"><!--循環插入,item:別名-->
         (#{label.ecp,jdbcType=VARCHAR},#{label.pid,jdbcType=INTEGER},#{label.ehco,jdbcType=VARCHAR})
      </foreach>
  </insert>

controller:

/**
	 * 批量入庫
	 * @param list
	 * @param pid
	 * @return
	 */
	@PostMapping("/insertAll")//@RequestParam 不寫會報List錯誤
	public Object insertLabel(@RequestParam("list")List<Label> list,String ehco,int pid,String user){

postman:

批量插入圖片

相應事物層控制層就不寫了都是相應的調用(我是測試),或處理下邏輯。

2.批量更新

mapper

int batchUpdateLabelDTO(List<Label> list);

.xml

<!--批量更新(回收)-->
  <update id="batchUpdateLabelDTO" parameterType="java.util.List">
       <foreach collection="list" separator=";" item="label">
         update tb_label
         <set> 
            state = #{label.state,jdbcType=INTEGER},
            hid = #{label.hid,jdbcType=INTEGER},
            frequency = #{label.frequency,jdbcType=INTEGER}
         </set>
         <where> 
            id = #{label.id,jdbcType=INTEGER}
         </where> 
      </foreach>
  </update>

controller:

/**
	 * 批量更新
	 * @param list
	 * @param
	 * @return
	 */
	@PostMapping("/CascadeUpdate")
	public Object batchUpdateLabelDTO(@RequestBody List<Label> list){

postman:

批量更新圖

3.批量查詢

mapper

List<LabelDTO> selectLabelDTO(List<String> list);

.xml

  <select id="selectLabelDTO"  parameterType="java.util.List" resultMap="lable_variety_BaseResultMap">
     SELECT l.id,l.ecp,l.pid,l.frequency,l.ehco,l.state,l.hid,l.gross,l.employ,l.unused,l.account_balance,
      l.total_cost,l.created,l.updated,l.delivery_time,
      v.id v_id,v.variety_name,v.category_id,v.grade_id,v.image,v.specification,v.rental_price,v.occupancy_expenses,
     v.frequency v_frequency,v.echo,v.created v_created,v.updated v_updated
     FROM tb_label l LEFT JOIN tb_variety v ON v.id = l.pid
      WHERE l.ecp in
      <foreach collection="list" item="label" open="(" close=")" separator=",">
          #{label}<!--數組類型爲對象時可調用多個字段,我的類型只是是字符串,所以沒調用,像:#{label.id}-->
      </foreach>
  </select>

查詢和同樣上兩個同樣。

 

記錄,方便以後查閱。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章