mybatis-plus 表名添加前缀的实现方法

来自:网络
时间:2020-10-14
阅读:

1、使用mybatis-plus自身的查询构造去,只需要在全局配置中添加如下配置

mybatis-plus:
 mapper-locations: classpath:mappers/*Mapper.xml  # mapper映射文件
 global-config:
  db-config:
   table-prefix: tr_

2、自定义sql语句中添加表名前缀

在yml文件中添加如下配置

mybatis-plus:
 mapper-locations: classpath:mappers/*Mapper.xml  # mapper映射文件
 global-config:
  db-config:
   table-prefix: tr_
 configuration-properties: 
  prefix: tr_ # 自定义sql中表名带前缀

然后在自定义sql语句如下

select * from ${prefix}user 

编译后的sql语句

select * from tr_user

MybatisPlus 数据库字段使用驼峰命名法时碰到的问题

假如有个实体类:

class User{
 int userId;
}

按照规范,数据库User表里边对应userId的字段名应该为 user_id。

如果数据库的字段名也是userId的话(没有下划线),那么使用MybatisPlus的时候就会碰到映射问题,实际查询的时候默认是查询user_id。

解决办法

.properties添加一行配置,关闭驼峰到下划线的映射即可

mybatis-plus.configuration.map-underscore-to-camel-case=false

mybaits-plus功能还是很强大的,官网地址:https://mp.baomidou.com/guide/

返回顶部
顶部