MyBatis動態傳入表名,欄位名引數
阿新 • • 發佈:2018-12-15
問題
Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?,?' at line 6; bad SQL grammar []; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?,?' at line 6
要實現動態傳入表名、列名,需要做如下修改
- 新增屬性statementType="STATEMENT"
- 同時sql裡的屬有變數取值都改成${xxxx},而不是#{xxx}
<insert id="insertSelectiveList" parameterType="java.util.Map" statementType="STATEMENT"> INSERT INTO ${tableName}( user_no, take_time, created_at, creator, updated_at, modifier )VALUES <foreach collection ="list" item="rateList" separator =","> (${rateList.userNo},${rateList.takeTime}','${rateList.createdAt}', '${rateList.creator}', '${rateList.updatedAt}', '${rateList.modifier}') </foreach > </insert>
1. statementType:STATEMENT(非預編譯),PREPARED(預編譯)或CALLABLE中的任意一個,這就告訴 MyBatis 分別使用Statement,PreparedStatement或者CallableStatement。預設:PREPARED。這裡顯然不能使用預編譯,要改成非預編譯。
2. ${xxxx}:$將傳入的資料直接顯示生成在sql中,對於字串資料,需要手動加上引號