zoukankan      html  css  js  c++  java
  • mybatis的模糊查询写法

    mybatis做like模糊查询

     

    1.  参数中直接加入%%

      param.setUsername("%CD%");
          param.setPassword("%11%");

    	<select  id="selectPersons" resultType="person" parameterType="person">
    		select id,sex,age,username,password from person where true 
    			<if test="username!=null"> AND username LIKE #{username}</if>
    			<if test="password!=null">AND password LIKE #{password}</if>
    	
    	</select>

    2.  bind标签

    <select id="selectPersons" resultType="person" parameterType="person">
      <bind name="pattern" value="'%' + _parameter.username + '%'" />
      select id,sex,age,username,password 
      from person
      where username LIKE #{pattern}
    </select>

    3. CONCAT

    where username LIKE concat(concat('%',#{username}),'%')

    模糊查询:

    工作中用到,写三种用法吧,第四种为大小写匹配查询

    1. sql中字符串拼接

       SELECT * FROM tableName WHERE name LIKE CONCAT(CONCAT('%', #{text}), '%');

    2. 使用 ${...} 代替 #{...}

       SELECT * FROM tableName WHERE name LIKE '%${text}%'; 

    3. 程序中拼接

       Java

       // or String searchText = "%" + text + "%";

       String searchText = new StringBuilder("%").append(text).append("%").toString();

       parameterMap.put("text", searchText);

       SqlMap.xml

       SELECT * FROM tableName WHERE name LIKE #{text};

    4. 大小写匹配查询

    [sql] view plain copy
     
    1. SELECT *  FROM TABLENAME  WHERE UPPER(SUBSYSTEM) LIKE '%' || UPPER('jz') || '%'  
    2.  --或者是  
    3. SELECT *   FROM TABLENAME  WHERE LOWER(SUBSYSTEM) LIKE '%' || LOWER('jz') || '%'  


     

    异常:

    java.lang.IllegalArgumentException: modify is ambiguous in Mapped Statements collection (try using the full name including the namespace, or rename one of the entries)
     at org.apache.ibatis.session.Configuration$StrictMap.get(Configuration.java:466)
     at org.apache.ibatis.session.Configuration.getMappedStatement(Configuration.java:349)
     at org.apache.ibatis.binding.MapperMethod.setupCommandType(MapperMethod.java:137)
     at org.apache.ibatis.binding.MapperMethod.<init>(MapperMethod.java:46)
     at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:34)
     at $Proxy10.modify(Unknown Source)

    抛出这个异常的原因可能是因为mapper.xml配置文件中<mapper>的namespace属性配置错误造成的,没有根据命名空间的值(全称类名)找到相应映射接口。

    如:<mapper namespace="com.xxx.xxx.mapper.TestMapper" >

     
     
  • 相关阅读:
    caffe学习笔记(1)安装
    windows下遍历文件夹
    图像处理之OpenCV
    OpenCV的安装和使用
    使用cpplint检测代码规范
    图像处理之原理
    Python 在cmd中import模块成功,但是在jupyter notebook中No module xxx found
    Win10上使用VS2015编译Caffe2
    Win10, VS2017环境下OpenCV3.4.2的配置
    C++ main函数的参数
  • 原文地址:https://www.cnblogs.com/fengli9998/p/7474258.html
Copyright © 2011-2022 走看看