使用Mybatis-Plus问题解答

博客 动态
0 177
羽尘
羽尘 2023-06-06 12:10:36
悬赏:0 积分 收藏

使用Mybatis-Plus问题解答

我们使用一个新的框架难免会遇到各种问题,当然使用这款国产的优秀的Mybatis-Plus框架也不例外,下面我就给大家列举一下使用Mybatis-Plus可能遇到的一些问题,并做一下一一的解答。

1:如何排除非表的字段(这个问题一定要注意,我们Java中写的Entity类的属性是和表的字段一一对应的,如果属性在数据库中没有对应字段程序就会出错。当然表中可以有多的字段)。

使用一些三种方法都可以:(开发中我们经常使用第三者方式)

使用 transient 修饰: 

private transient String noColumn; 

使用 static 修饰 :

private static String noColumn; 

使用 TableField 注解:

@TableField(exist=false)

private String noColumn;

2:出现 Invalid bound statement (not found) 异常 (这种一般是写的mapper接口找不到对应的xml中的mapper中写的sql)

不要怀疑,正视自己,这个异常肯定是你插入的姿势不对……

  • 检查是不是引入 jar 冲突

  • 检查 Mapper.java 的扫描路径

方法一:在 Configuration 类上使用注解 MapperScan

@Configuration
@MapperScan("com.yourpackage.*.mapper")
public class YourConfigClass{
  ...
}

方法二:在Configuration类里面,配置MapperScannerConfigurer

@Bean
public MapperScannerConfigurer mapperScannerConfigurer(){
    MapperScannerConfigurer scannerConfigurer = new MapperScannerConfigurer();
    //可以通过环境变量获取你的mapper路径,这样mapper扫描可以通过配置文件配置了
    scannerConfigurer.setBasePackage("com.yourpackage.*.mapper");
    return scannerConfigurer;
}
  • 检查是否指定了主键?如未指定,则会导致 selectById 相关 ID 无法操作,请用注解 @TableId 注解表 ID 主键。当然 @TableId 注解可以没有!但是你的主键必须叫 id(忽略大小写)

  • SqlSessionFactory不要使用原生的,请使用MybatisSqlSessionFactory

  • 检查是否自定义了SqlInjector,是否复写了getMethodList()方法,该方法里是否注入了你需要的方法(可参考DefaultSqlInjector)

  • IDEA 默认的 build 步骤可能会引起 mapper 文件无法正常编译到对应的 resources 文件夹中,请检查 build 之后相关资源文件夹是否有对应的 xml 文件,如果没有,请调整 IDEA 的 build 设置,推荐调整为 Maven 或 Gradle 的 build。

3:自定义 SQL 无法执行

问题描述:指在 XML 中里面自定义 SQL,却无法调用。本功能同 MyBatis 一样需要配置 XML 扫描路径:

Spring MVC 配置

<bean id="sqlSessionFactory" class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="typeAliasesPackage" value="xxx.entity" />
    <property name="mapperLocations" value="classpath*:/mybatis/*/*.xml"/>
    ...
</bean>

Spring Boot 配置

mybatis-plus:
  mapper-locations: classpath*:/mapper/**/*.xml

对于IDEA系列编辑器,XML 文件是不能放在 java 文件夹中的,IDEA 默认不会编译源码文件夹中的 XML 文件,可以参照以下方式解决:

1:将配置文件放在 resource 文件夹中

2:对于 Maven 项目,可指定 POM 文件的 resource

<build>
  <resources>
      <resource>
          <!-- xml放在java目录下-->
          <directory>src/main/java</directory>
          <includes>
              <include>**/*.xml</include>
          </includes>
      </resource>
      <!--指定资源的位置(xml放在resources下,可以不用指定)-->
      <resource>
          <directory>src/main/resources</directory>
      </resource>
  </resources>
</build>

提示 注意!

Maven 多模块项目的扫描路径需以 classpath*: 开头 (即加载多个 jar 包下的 XML 文件)

4:关于 Long 型主键填充不生效的问题

检查是不是用了long而不是Long!

提示 

long类型默认值为 0,而 MP 只会判断是否为 null

JavaScript 无法处理 Java 的长整型 Long 导致精度丢失,具体表现为主键最后两位永远为 0,解决思路:Long 转为 String 返回。

FastJson 处理方式

@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    FastJsonHttpMessageConverter fastJsonConverter = new FastJsonHttpMessageConverter();
    FastJsonConfig fjc = new FastJsonConfig();
    // 配置序列化策略
    fjc.setSerializerFeatures(SerializerFeature.BrowserCompatible);
    fastJsonConverter.setFastJsonConfig(fjc);
    converters.add(fastJsonConverter);
}

JackJson 处理方式

方式一

/ 注解
处理,这里可以配置公共 baseEntity 处理
@JsonSerialize(using=ToStringSerializer.class)
public long getId() {
    return id;
}

方式二

/ 全局配置序列化返回 JSON 处理
final ObjectMapper objectMapper = new ObjectMapper();
SimpleModule simpleModule = new SimpleModule();
simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
objectMapper.registerModule(simpleModule);

5:插入或更新的字段有 空字符串 或者 null

FieldStrategy 有以下策略:

方式一:调整字段验证注解

根据具体情况,在需要更新的字段中调整验证注解,如验证非空:

@TableField(strategy=FieldStrategy.NOT_EMPTY)

方式二:使用 UpdateWrapper (3.x)

使用以下方法来进行更新或插入操作:

//updateAllColumnById(entity) // 全部字段更新: 3.0已经移除
mapper.update(
   new User().setName("mp").setAge(3),
   Wrappers.<User>lambdaUpdate()
           .set(User::getEmail, null) //把email设置成null
           .eq(User::getId, 2)
);
//也可以参考下面这种写法
mapper.update(
    null,
    Wrappers.<User>lambdaUpdate()
       .set(User::getAge, 3)
       .set(User::getName, "mp")
       .set(User::getEmail, null) //把email设置成null
       .eq(User::getId, 2)
);

6:字段类型为 bit、tinyint(1) 时映射为 boolean 类型

默认mysql驱动会把tinyint(1)字段映射为boolean: 0=false, 非0=true。MyBatis 是不会自动处理该映射,如果不想把tinyint(1)映射为boolean类型:

修改类型tinyint(1)为tinyint(2)或者int。

需要修改请求连接添加参数 tinyInt1isBit=false,如下:

jdbc:mysql://127.0.0.1:3306/mp?tinyInt1isBit=false

7:MP 如何查指定的几个字段

EntityWrapper.sqlSelect 配置你想要查询的字段

//2.x
EntityWrapper<H2User> ew = new EntityWrapper<>();
ew.setSqlSelect("test_id as id, name, age");//只查询3个字段
List<H2User> list = userService.selectList(ew);
for(H2User u:list){
    Assert.assertNotNull(u.getId());
    Assert.assertNotNull(u.getName());
    Assert.assertNull(u.getPrice()); // 这个字段没有查询出来
}

//3.x
mapper.selectList(
    Wrappers.<User>lambdaQuery()
    .select(User::getId, User::getName)
);
//或者使用QueryWrapper
mapper.selectList(
    new QueryWrapper<User>()
    .select("id","name")
);

8:Cause: org.apache.ibatis.type.TypeException:Error setting null for parameter #1 with JdbcType OTHER

配置 jdbcTypeForNull=NULL Spring Bean 配置方式:

MybatisConfiguration configuration = new MybatisConfiguration();
configuration.setDefaultScriptingLanguage(MybatisXMLLanguageDriver.class);
configuration.setJdbcTypeForNull(JdbcType.NULL);
configuration.setMapUnderscoreToCamelCase(true);//开启下划线转驼峰
sqlSessionFactory.setConfiguration(configuration);

yml 配置

mybatis-plus:
  configuration:
    jdbc-type-for-null: 'null'

9:通用 insertBatch 为什么放在 service 层处理

SQL 长度有限制海量数据量单条 SQL 无法执行,就算可执行也容易引起内存泄露 JDBC 连接超时等

不同数据库对于单条 SQL 批量语法不一样不利于通用

目前的解决方案:循环预处理批量提交,虽然性能比单 SQL 慢但是可以解决以上问题。

10:启动 mybatis 本身的 log 日志

# 方式一
mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl 

# 方式二 application.yml 中增加配置,指定 mapper 文件所在的包
logging:
  level:
    com.baomidou.example.mapper: debug

这些问题都是摘自Mybatis-Plus的官方网站地址如下:

https://www.baomidou.com/pages/f84a74/

如果想看更多的问题请访问上面的网址,当然更多的问题也可以访问Mybatis-Plus的github地址那里面有很多人提的issue。很多开发中遇到的问题都可以在issue里面找到答案,我也是常常去看那里的issue从里面获取答案获取灵感。欢迎大家关注我的微信公众号,继续为大家推出好的文章。

晚霞和微风是真的很美。

posted @ 2023-06-06 11:52  程序员xiaozhang  阅读(2)  评论(1编辑  收藏  举报
回帖
    羽尘

    羽尘 (王者 段位)

    2233 积分 (2)粉丝 (11)源码

     

    温馨提示

    亦奇源码

    最新会员