参数为LIKE的本机查询-无结果

时间:2018-07-05 13:13:17

标签: mysql spring-data-jpa spring-data

我在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条件没有%包装。解决方法是复制:parent参数,以便在具有相同条件的LIKE条件中不共享该参数:

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);
}

这是错误或功能吗?还有其他解决方案吗?这发生在SpringBoot v2.0.2

0 个答案:

没有答案