mybatis動態列名


mybatis動態列名

<select id="getUser" resultType="java.util.Map" parameterType="java.lang.String" statementType="STATEMENT">
    select 
        ${columns}
    from ${tableName}
        where COMPANY_REMARK = ${company}
  </select>

要實現動態調用表名和字段名,就不能使用預編譯了,需添加statementType="STATEMENT"

statementType:STATEMENT(非預編譯),PREPARED(預編譯)或CALLABLE中的任意一個,這就告訴 MyBatis 分別使用Statement,PreparedStatement或者CallableStatement。
默認:PREPARED。這里顯然不能使用預編譯,要改成非預編譯。

其次,sql里的變量取值是${xxx},不是#{xxx}。因為${}是將傳入的參數直接顯示生成sql,如${xxx}傳入的參數為字符串數據,需在參數傳入前加上引號,如:

String name = "sprite";
        name = "'" + name + "'";

如果使用的是動態列名,則不需要添加 ' ',直接傳入參數即可

  <!--查詢月度指標-->
  <select id="findMonthIndicator" resultMap="BaseResultMap"
          parameterType="com.jn.ssr.superrescuereporting.web.entity.PerformanceIndicatorEntity" statementType="STATEMENT">
    select  ${param.orderNumber},${param.turnover}
     from in_performance_indicators
     <where>
       <if test="param.year != null">
         and year = #{param.year,jdbcType=INTEGER}
       </if>
     </where>
  </select>


免責聲明!

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



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