springboot 如何通過后臺創(chuàng)建臨時表
其實創(chuàng)建臨時表,跟增刪改查的原理是一樣的,只不過是在xml中寫一個創(chuàng)建臨時表sql語句,xml中并不是只能寫增刪改查語句的
1,首先弄一個xml
在xml中寫一個修改頭標簽,因為是建立的是臨時表,所以表名要變,需要在表名處接收一個參數$(tableName) ,這時xml文件就寫好了
2,在mapper中寫出對應方法
這時需要在參數中加上注解@Param,只有加上這個注解,在xml中才可以接收到我傳入的參數
3,接下來在service層和Controller層中調用這個方法
然后在postman中傳入需要的表名,就可以生成這個表了。
springboot mybatis下臨時表的創(chuàng)建和刪除,可用于查重去重
/** * 創(chuàng)建臨時表 */ @Update({"drop temporary table if exists ${tableName};", "create temporary table ${tableName} select doctor_id from crm_speaker where 1=2 "}) void createTemoraryTable(@Param("tableName") String tableName); /** * 保存數據到臨時表里面以便校驗數據重復 */ @Insert("<script>" + "insert into ${tableName} (doctor_id) values " + " <foreach collection="list" item="doct" index="index" separator=","> " + " (" + " #{doct.doctorId,jdbcType=VARCHAR} " + " ) " + " </foreach> " + "</script>") void insertBatchCheckDatas(@Param("list") List<SpeakerDO> dOs, @Param("tableName") String tableName); /** * 刪除臨時表 */ @Update({"drop temporary table if exists ${tableName}"}) void dropTemporaryTable(@Param("tableName") String tableName);
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/qq_39776207/article/details/80627918