mybatis中#和$符號的區別


#{ }

  1.在使用#{}時意味着用的是預編譯,sql語句會用?占位,傳的值會用 ' ' 包住,可防止sql注入 

select * from student where id=#{id}

 

  編譯后是

select * from student where id='1'

 

${ }

   1.在使用${}時傳的值會原樣輸入

select * from ${tableName} order by ${id}

  則后台語句為:select * from student order by id
  使用#{}則成:select * from 'student' order by 'id'是不對的

注:

  在使用以下的配置時,必須使用#{}

  

<select id="selectMessageByIdI" parameterType="int" resultType="Message">
          select * from message where id=#{id};
</select>
parameterType="int"已聲明了參數類型是int所有用#{id}

還有在鍵表時也要用${表名},用#{表名}就會報錯,因為在編譯時用#{表名}會在表名加上“”號如“表名”,執行時就會報sql語句錯誤
<update id="addTable" parameterType="com.example.yunpingtai.domain.BuildTable">
         CREATE TABLE ${tableName} (id bigint(20) NOT NULL AUTO_INCREMENT,PRIMARY KEY (id)) 
</update>

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM