zoukankan      html  css  js  c++  java
  • mybatis中使用mysql的模糊查询字符串拼接(like)

    方法一:

    
    
    <!--  根据hid,hanme,grade,模糊查询医院信息-->

    方法一:
    List<Hospital> getHospitalLike(@Param("selectword") String selectword);
    <select id="getHospitalLike" resultType="com.hand.hand.domain.Hospital">
    SELECT *
    FROM hospital
    where hid=cast(#{selectword} as signed INTEGER ) OR hname like concat('%',#{selectword},'%')
    OR grade like concat('%',#{selectword},'%')
    </select>


     where hid=cast(#{selectword} as signed INTEGER )  hid为Integer类型,而参数selectword为string类型,所以用cast将string转化为Integer
    where hname like concat('%',#{selectword},'%') 字符串拼接,将%和selectword拼接成%selectword%

    方法二:动态sql中的bind
    List<Hospital> getHospitalLike(@Param("selectword") String selectword);
    <select id="getHospitalLike" resultType="com.hand.hand.domain.Hospital">
    <bind name="bindselectword" value="'%'+selectword+'%'"></bind>
    SELECT *
    FROM hospital
    <if test="selectword!=null">
    where hid=cast(#{selectword} as signed INTEGER ) OR hname like #{bindselectword}
    OR grade like #{bindselectword}
    </if>

    </select>

    方法三:
    在service层直接拼接字符串,xml直接使用转入的参数



    此方法测试出错(参考博客后,测试出错):
    <!--where hid like '%'||#{selectword}||'%' or hname like '%'||#{selectword}||'%' or grade like '%'||#{selectword}||'%'-->


    其他拼接法,可参考:https://www.cnblogs.com/dushan/p/4766954.html
  • 相关阅读:
    nginx 过滤了自定义的请求头参数
    Mysql5.7查看已经执行的sql语句
    Reids5 持久化
    JS 格式化时间,转成 几天前,几个月前
    个人小镜像站点
    记录一次清理Redis 病毒程序 kdevtmpfsi
    laravels 热重启
    Redis 布隆器安装和简单实现
    Redis Zset类型跳跃表算法实现(JAVA)
    Redis5 基于Lua实现分布式排它锁
  • 原文地址:https://www.cnblogs.com/shuaifing/p/7928628.html
Copyright © 2011-2022 走看看