原生查询,其中参数like-无结果

anauzrmj  于 2021-06-20  发布在  Mysql
关注(0)|答案(0)|浏览(186)

我在workbench中有工作sql查询:

SELECT tmi.* FROM tree_menu_item tmi WHERE (tmi.altname='FC' OR tmi.title='FC') AND 
(tmi.parent_title LIKE '%FC_parent%' OR 
tmi.parent_title LIKE concat(IFNULL((select parent_title from tree_menu_item where altname='FC_parent'),'xxx'),'%'))

但无法在jparepository中运行:

public interface TreeMenuItemRepository extends JpaRepository<TreeMenuItem, Long> {
    @Query(value = "SELECT tmi.* FROM tree_menu_item tmi WHERE (tmi.altname=:title OR tmi.title=:title) AND (tmi.parent_title LIKE %:parent% "
        + "OR tmi.parent_title LIKE concat(IFNULL((SELECT parent_title FROM tree_menu_item WHERE altname=:parent),'xxx'),'%'))", nativeQuery = true)
    TreeMenuItem findOneByParentTitle(@Param("parent") String parent, @Param("title") String title);
}

当我在mysql中打开查询日志时,我可以看到like条件是没有的 % Package 。解决方法是复制 :parent 参数,以便在相同条件下不共享:

public interface TreeMenuItemRepository extends JpaRepository<TreeMenuItem, Long> {
  @Query(value = "SELECT tmi.* FROM tree_menu_item tmi WHERE (tmi.altname=:title OR tmi.title=:title) AND (tmi.parent_title LIKE %:parentLike% "
        + "OR tmi.parent_title LIKE concat(IFNULL((SELECT parent_title FROM tree_menu_item WHERE altname=:parentAlt),'xxx'),'%'))", nativeQuery = true)
  TreeMenuItem findOneByParentTitle(@Param("parentLike") String parentLike, @Param("parentAlt") String parentAlt, @Param("title") String title);
}

这是错误还是功能?还有别的解决办法吗?这发生在SpringBootV2.0.2上

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题