mybatis-plus @TableField 與 @Select 合並使用的理解


緣由

昨晚擼碼,要使用 MyISAM全文索引,mybatis-plus 目前沒有該內容的寫法,所以就只能自己寫sql,奈何個人懶得寫xml, 就使用@Select 注解進行了實現,然后問題出現了;

問題

Model內容

@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@TableName("test")
public class TestModel {
   /**
     * 主鍵
     */
    @TableId(type = IdType.ASSIGN_UUID)
    private String id;

    /**
     * 名稱
     */
    @TableField(value = "the_name")
    private String theUserName;
}

mapper 內容

@Mapper
public interface TestMapper extends BaseMapper<TestModel> {

   @Select("SELECT id,the_name FROM  test WHERE  MATCH(the_name ) AGAINST (CONCAT('*',#{tag},'*')  IN BOOLEAN MODE ) LIMIT 10 ;")
    List<TestModel> listByTag(@Param("tag") String tag);
}

使用 mybatis-plus 的list()方法查詢,能夠正確返回結果,而使用自己寫的這個卻不能返回正確結果,條數、{id}存在,而 theUserName 卻是個null

//mybatis-plus list() 方法結果
[
  {"id":"aaadf3","theUserName":"hello"
] 
//自寫 listByTag 方法結果
[
  {"id":"aaadf3","theUserName":null
]

查找問題

打印了這兩個方法執行的sql

-- mybatis-plus list() sql 
SELECT id,the_name AS theUserName FROM test 
--自寫 listByTag sql
SELECT id,the_name FROM test WHERE  MATCH(the_name ) AGAINST (CONCAT('*',?,'*')  IN BOOLEAN MODE ) LIMIT 10 ;

再去看 @TableField 源碼 value 屬性 到底是干啥
發現 在 SELECT的sql生成腳本中 是一個 AS 的存在,而自己寫@Select 將不會對其做處理了。

解決方法

自己 寫 AS 💢💢💢


免責聲明!

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



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