mybatis 的模糊查询功能使用的很广泛, 以 MySQL 数据库为例 (不同的数据库, 有些可能不支持)
常用的模糊查询有三种方法:
直接使用 % 拼接字符串, 如
%#{name}%
或 "%"#{name}"%", 单引号或双引号都可以
使用 concat(str1,str2) 函数拼接
使用 mybatis 的 bind 标签
现在有数据库 mybatis1 中表 users 中有如下记录:
User [id=2, name=bb, phone=13422222222, email=bb@163.com]
User [id=5, name = 关羽, phone=13333333333, email=GuanYu@shu.com]
User [id=6, name = 张飞, phone=13344444444, email=ZhangFei@shu.com]
User [id=7, name = 赵云, phone=13355555555, email=ZhaoYun@shu.com]
User [id=8, name = 黄忠, phone=13366666666, email=HuangZhong@shu.com]
User [id=9, name = 曹操, phone=13477883429, email=caoCao@wei.com]
User [id=10, name = 郭嘉, phone=13447685234, email=guoJia@wei.com]
User [id=11, name = 张三丰, phone=13423455432, email=zhangSanFeng@wudang.com]
在 userMapper.xml 文件中新建映射 sql 的标签
- <!-- ******************** 模糊查询的常用的 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>
1 测试 name: 传入 name 属性, 模糊匹配 name 中包含张字的数据记录
- @Test
- public void fuzzyQuery(){
- SqlSession session = MybatisUtils.getSession(false);
- User u = new User();
- u.setName("张");
- List<User> userList = session.selectList("com.qcc.mapping.userMapper.getUsersByFuzzyQuery", u);
- for (User user : userList) {
- System.out.println(user);
- }
- }
测试结果:
User [id=6, name = 张飞, phone=13344444444, email=ZhangFei@shu.com]
User [id=11, name = 张三丰, phone=13423455432, email=zhangSanFeng@wudang.com]
2 测试 phone: 传入 phone 属性, 模糊匹配 phone 中包含 44 的所有 User 信息
- @Test
- public void fuzzyQuery(){
- SqlSession session = MybatisUtils.getSession(false);
- User u = new User();
- // u.setName("张");
- u.setPhone("44");
- List<User> userList = session.selectList("com.qcc.mapping.userMapper.getUsersByFuzzyQuery", u);
- for (User user : userList) {
- System.out.println(user);
- }
- }
测试结果:
User [id=6, name = 张飞, phone=13344444444, email=ZhangFei@shu.com]
User [id=10, name = 郭嘉, phone=13447685234, email=guoJia@wei.com]
3 测试 email: 传入 email 属性, 模糊匹配 email 中包含 shu 的所有 User 信息
- @Test public void fuzzyQuery() {
- SqlSession session = MybatisUtils.getSession(false);
- User u = new User();
- // u.setName("张");
- // u.setPhone("44");
- u.setEmail("shu");
- List < User > userList = session.selectList("com.qcc.mapping.userMapper.getUsersByFuzzyQuery", u);
- for (User user: userList) {
- System.out.println(user);
- }
- }
测试结果:
User [id=5, name = 关羽, phone=13333333333, email=GuanYu@shu.com]
User [id=6, name = 张飞, phone=13344444444, email=ZhangFei@shu.com]
User [id=7, name = 赵云, phone=13355555555, email=ZhaoYun@shu.com]
User [id=8, name = 黄忠, phone=13366666666, email=HuangZhong@shu.com]
综合测试 : 多条件动态模糊查询:
- /**
- * 根据 name, phone, email 字段多条件动态模糊查询
- */
- @Test public void fuzzyQuery() {
- SqlSession session = MybatisUtils.getSession(false);
- User u = new User("张", "44", "shu");
- List < User > userList = session.selectList("com.qcc.mapping.userMapper.getUsersByFuzzyQuery", u);
- for (User user: userList) {
- System.out.println(user);
- }
- }
结果:
User[id = 6, name = 张飞, phone = 13344444444, email = ZhangFei@shu.com]
发现结果正常的查询出来了, 说明模糊查询的三种写法同时都是没有问题的
来源: http://www.bubuko.com/infodetail-2491537.html