mybatis 问题汇总

2020-12-05  本文已影响0人  Mr菜头

1、null 转 int 失败
错误:
attempted to return null from a method with a primitive return type (int).

解决方法:
将 dao 的 int 变为 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 中出现关键字
需要在 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},'$')  -- 改为这个可行
上一篇 下一篇

猜你喜欢

热点阅读