zoukankan      html  css  js  c++  java
  • mybatis常用的模糊查询

    常用的模糊查询有三种方法:
    直接使用 % 拼接字符串,如 '%'#{name}'%' 或 "%"#{name}"%",单引号或双引号都可以。
    使用concat(str1,str2)函数拼接
    使用mybatis的bind标签

    <!-- ******************** 模糊查询的常用的3种方式:********************* -->
    <select id="getUsersByFuzzyQuery" parameterType="User" resultType="User">
    select <include refid="columns"/> from users
    <where>
        <!-- 方法一: 直接使用 % 拼接字符串
        注意:此处不能写成 "%#{name}%" ,#{name}就成了字符串的一部分,
        会发生这样一个异常: The error occurred while setting parameters,
         应该写成: "%"#{name}"%",即#{name}是一个整体,前后加上% -->
        <if test="name != null"> name like "%"#{name}"%" </if>

        <!--方法二: 使用concat(str1,str2)函数将两个参数连接 -->
         <if test="phone != null"> and phone like concat(concat("%",#{phone}),"%") </if>

         <!--方法三: 使用 bind 标签,对字符串进行绑定,然后对绑定后的字符串使用 like 关键字进行模糊查询 -->
         <if test="email != null"> <bind name="pattern" value="'%'+email+'%'"/> and email like #{pattern} </if>
    </where>
     </select>


    插入后获取自增主键的值
    <insert id="insertReturnKeyValue" keyProperty="userCode" useGeneratedKeys="true">

  • 相关阅读:
    LNMP一键安装
    IIS出现问题报CS0016
    如何在windows live Write中添加插件
    合同管理系统 功能一览表
    房地产合同档案分类及编号规则
    属性ErrorLogFile不可用于JobServer的解决方案
    Wps定义选中区域的名称
    常用的SQL语句
    xp sp3 访问IIS元数据库失败解决办法
    完全卸载oracle11g步骤
  • 原文地址:https://www.cnblogs.com/cyf18/p/14297362.html
Copyright © 2011-2022 走看看