mybatis 問題匯總

1、null 轉(zhuǎn) int 失敗
錯誤:
attempted to return null from a method with a primitive return type (int).

解決方法:
將 dao 的 int 變?yōu)?Integer就好

2、打開mybatis 的日志

mybatis:
 configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

3、如果 pojo 實體類和查找的字段名稱不同就需要 使用resultMap做字段翻譯

    <resultMap  id="tableInfo" type="com.test.model.TableColAndType">
        <id column="COLUMN_NAME" property="columnName"/>
        <result column="DATA_TYPE" property="dataType"/>
        <result column="COLUMN_COMMENT" property="columnComment"/>
     </resultMap>
 
 <select id="selectTableColsAndType" resultMap = "tableInfo">
    select COLUMN_NAME,DATA_TYPE,COLUMN_COMMENT from information_schema.columns where TABLE_NAME=#{tableName}
 </select>

或者

 <select id="selectTableColsAndType" resultType = "com.test.model.TableColAndType">
    select COLUMN_NAME,DATA_TYPE,COLUMN_COMMENT from information_schema.columns where TABLE_NAME=#{tableName}
 </select>
package com.test.model;

import lombok.Data;

@Data
public class TableColAndType {
    private String COLUMN_NAME;
    private String DATA_TYPE;
    private String COLUMN_COMMENT ;

}

3、mybatis-plus sql 中出現(xiàn)關(guān)鍵字
需要在 model中加入 注釋來強化字段

 @TableField(value="`load`")
    private Integer load;

4、無法使用 REGEXP '^ #{name} $'
可以使用 MySQL 中 字符串拼接方法

//
select * from usr where name REGEXP '^ #{name} $'  -- mybatis中出錯
select * from usr where name REGEXP concat('^',#{name},'$')  -- 改為這個可行
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容