mybatis-plus 表名添加前綴的實現方法


mybatis-plus 表名添加前綴的實現方法

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/

到此這篇關於mybatis-plus 表名添加前綴的實現方法的文章就介紹到這了,更多相關mybatis-plus 表名添加前綴內容請搜索雲海天教程以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持雲海天教程!

原文鏈接:https://blog.csdn.net/weixin_36931308/article/details/106120879



免責聲明!

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



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