mybatis的模糊查詢功能使用的很廣泛灰殴,以MySQL數(shù)據(jù)庫(kù)為例(不同的數(shù)據(jù)庫(kù)饿悬,有些可能不支持)
常用的模糊查詢有三種方法:
直接使用 % 拼接字符串嗡载,如 '%'#{name}'%' 或 "%"#{name}"%"踏拜,單引號(hào)或雙引號(hào)都可以鞋邑。
使用concat(str1,str2)函數(shù)拼接
使用mybatis的bind標(biāo)簽
現(xiàn)在有數(shù)據(jù)庫(kù)mybatis1中表users中有如下記錄:
User [id=2, name=bb, phone=13422222222, email=bb@163.com]
User [id=5, name=關(guān)羽, 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的標(biāo)簽
<!-- ******************** 模糊查詢的常用的3種方式:********************* -->
<select id="getUsersByFuzzyQuery" parameterType="User" resultType="User">
select <include refid="columns"/> from users
<where>
<!--
方法一: 直接使用 % 拼接字符串
注意:此處不能寫成 "%#{name}%" ,#{name}就成了字符串的一部分,
會(huì)發(fā)生這樣一個(gè)異常: The error occurred while setting parameters,
應(yīng)該寫成: "%"#{name}"%",即#{name}是一個(gè)整體,前后加上%
-->
<if test="name != null">
name like "%"#{name}"%"
</if>
<!--方法二: 使用concat(str1,str2)函數(shù)將兩個(gè)參數(shù)連接 -->
<if test="phone != null">
and phone like concat(concat("%",#{phone}),"%")
</if>
<!--方法三: 使用 bind 標(biāo)簽,對(duì)字符串進(jìn)行綁定,然后對(duì)綁定后的字符串使用 like 關(guān)鍵字進(jìn)行模糊查詢 -->
<if test="email != null">
<bind name="pattern" value="'%'+email+'%'"/>
and email like #{pattern}
</if>
</where>
</select>
1氓辣、測(cè)試name:傳入 name 屬性秒裕,模糊匹配 name 中包含“張”字的數(shù)據(jù)記錄
@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);
}
}
測(cè)試結(jié)果:
User [id=6, name=張飛, phone=13344444444, email=ZhangFei@shu.com]
User [id=11, name=張三豐, phone=13423455432, email=zhangSanFeng@wudang.com]
2、測(cè)試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);
}
}
測(cè)試結(jié)果:
User [id=6, name=張飛, phone=13344444444, email=ZhangFei@shu.com]
User [id=10, name=郭嘉, phone=13447685234, email=guoJia@wei.com]
3几蜻、測(cè)試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);
}
}
測(cè)試結(jié)果:
User [id=5, name=關(guān)羽, 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]
綜合測(cè)試 :多條件動(dòng)態(tài)模糊查詢:
/**
* 根據(jù) name, phone, email 字段多條件動(dòng)態(tài)模糊查詢
*/
@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);
}
}
結(jié)果:
User [id=6, name=張飛, phone=13344444444, email=ZhangFei@shu.com]