需求 : 根據(jù)用戶的輸入情況進行條件查詢
新建了一個 userInfo2Mapper 接口,然后寫下如下代碼,聲明 selectByCondition 這個方法
package com.example.mybatisdemo.mapper;
import com.example.mybatisdemo.model.UserInfo;
import org.apache.ibatis.annotations.*;
import java.util.List;
@Mapper
public interface UserInfo2Mapper {
List<UserInfo> selectByCondition(UserInfo userInfo);
}
我們先用XML的方式實現(xiàn)
在resources 中創(chuàng)建 Userinfo2XMLMapper.xml 文件
?將 Userinfo2XMLMapper.xml 文件中的 namespace 進行修改,改為 userInfo2Mapper 接口中的第一行 package 的內(nèi)容再加上接口名
然后補充如下代碼,用戶輸入什么條件,什么條件就不為空,就可以根據(jù)該條件進行查詢
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.mybatisdemo.mapper.UserInfo2Mapper">
<select id="selectByCondition" resultType="com.example.mybatisdemo.model.UserInfo">
select * from userinfo
where
<if test="username!=null">
username = #{username}
</if>
<if test="age!=null">
and age = #{age}
</if>
<if test="gender!=null">
and gender = #{gender}
</if>
</select>
</mapper>
再回到接口,然后Generate,test,勾選selectByCondition,ok
然后補充代碼
package com.example.mybatisdemo.mapper;
import com.example.mybatisdemo.model.UserInfo;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
@Slf4j
@SpringBootTest
class UserInfo2MapperTest {
@Autowired
private UserInfo2Mapper userInfo2Mapper;
@Test
void selectByCondition() {
UserInfo userInfo = new UserInfo();
userInfo.setUsername("io");
userInfo.setAge(23);
userInfo.setGender(0);
List<UserInfo> userInfos = userInfo2Mapper.selectByCondition(userInfo);
log.info(userInfos.toString());
}
}
然后我的數(shù)據(jù)庫里面是有這些數(shù)據(jù)的
接下來我們執(zhí)行看看結(jié)果?,沒毛病
?接下來我們對代碼稍作修改,我們不 set gender了,再看看運行結(jié)果
package com.example.mybatisdemo.mapper;
import com.example.mybatisdemo.model.UserInfo;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
@Slf4j
@SpringBootTest
class UserInfo2MapperTest {
@Autowired
private UserInfo2Mapper userInfo2Mapper;
@Test
void selectByCondition() {
UserInfo userInfo = new UserInfo();
userInfo.setUsername("io");
userInfo.setAge(23);
//userInfo.setGender(0);
List<UserInfo> userInfos = userInfo2Mapper.selectByCondition(userInfo);
log.info(userInfos.toString());
}
}
也是可以正常運行的,比限制 gender 多了一條數(shù)據(jù)?
但是當我們把setUsername也給去掉,只查詢年齡為23的人,發(fā)現(xiàn)報錯了
?我們看日志會發(fā)現(xiàn),多了一個and
這時候我們就要用 trim 標簽來消除這個多余的and?(上節(jié)博客也有trim的講解)
然后我們回到 Userinfo2XMLMapper.xml 對代碼進行修改,加上 trim 標簽
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.mybatisdemo.mapper.UserInfo2Mapper">
<select id="selectByCondition" resultType="com.example.mybatisdemo.model.UserInfo">
select * from userinfo
where
<trim prefixOverrides="and">
<if test="username!=null">
username = #{username}
</if>
<if test="age!=null">
and age = #{age}
</if>
<if test="gender!=null">
and gender = #{gender}
</if>
</trim>
</select>
</mapper>
這時再次運行程序就能成功啦
另一種方法就是 where 標簽?
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.mybatisdemo.mapper.UserInfo2Mapper">
<select id="selectByCondition" resultType="com.example.mybatisdemo.model.UserInfo">
select * from userinfo
<where>
<if test="username!=null">
username = #{username}
</if>
<if test="age!=null">
and age = #{age}
</if>
<if test="gender!=null">
and gender = #{gender}
</if>
</where>
</select>
</mapper>
這也是可以成功運行的?
用 where 還是 用 trim 都可以,這兩個沒啥差別?
但是 trim 標簽有個問題就是,如果所有where條件都為null的時候,會報錯,因為where后面沒東西了
where 標簽就不會有上面的問題?,如果查詢條件均為空,直接刪除 where 關鍵字
如果一定要用 trim 標簽也有一種解決方式
接下來我們看看如何用注釋的方式實現(xiàn)
在 UserInfo2Mapper 接口中寫入下面的代碼,跟XML代碼差不多
package com.example.mybatisdemo.mapper;
import com.example.mybatisdemo.model.UserInfo;
import org.apache.ibatis.annotations.*;
import java.util.List;
@Mapper
public interface UserInfo2Mapper {
@Select("<script>" +
"select * from userinfo" +
" <where>" +
" <if test='username!=null'>" +
" username = #{username}" +
" </if>" +
" <if test='age!=null'>" +
" and age = #{age}" +
" </if>" +
" <if test='gender!=null'>" +
" and gender = #{gender}" +
" </if>" +
" </where>"+
"</script>")
List<UserInfo> selectByCondition2(UserInfo userInfo);
}
然后就,右鍵,Generate,test,勾選 selectByCondition2,ok,然后補充下面代碼,也跟XML一樣
package com.example.mybatisdemo.mapper;
import com.example.mybatisdemo.model.UserInfo;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
@Slf4j
@SpringBootTest
class UserInfo2MapperTest {
@Autowired
private UserInfo2Mapper userInfo2Mapper;
@Test
void selectByCondition2() {
UserInfo userInfo = new UserInfo();
//userInfo.setUsername("io");
userInfo.setAge(23);
//userInfo.setGender(0);
List<UserInfo> userInfos = userInfo2Mapper.selectByCondition(userInfo);
log.info(userInfos.toString());
}
}
運行試試,沒毛病?文章來源:http://www.zghlxwxcb.cn/news/detail-814242.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-814242.html
到了這里,關于Mybatis 動態(tài)SQL條件查詢(注釋和XML方式都有)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!