使用Mybatis如何實(shí)現(xiàn)多個(gè)控制條件查詢
實(shí)現(xiàn)多個(gè)控制條件查詢
擴(kuò)展知識(shí)
1.給包起別名用<typeAliases>標(biāo)簽
< typeAliases> < typeAlias type=“MybatiesAnimal.Animal” alias=“animal”/> < !-- 指定實(shí)體類在哪個(gè)包里 --> < package name=“MybatiesAnimal”/> < /typeAliases>
2.在<mappers>中通過<package>標(biāo)簽引Mapper.xml

如果想用package直接引入所有mapper/xml, 那么要求接口和xml在一個(gè)包里
實(shí)現(xiàn)多個(gè)條件簡(jiǎn)單查詢
操作步驟如下:
1.在Mapper.xml中輸入要查詢的SQL語(yǔ)句

2.接口文件中參數(shù)用@Param接
語(yǔ)句如下:
public List selAnimalBy(@Param(“NAME”) String a,@Param(“kind”) String b);
如果傳的是2個(gè)java簡(jiǎn)單類型 那么需要用@Param(“name”) 指定參數(shù)名
3.text文檔中輸出
語(yǔ)句如下:
List< Animal> animal1=sqlsession.getMapper(AnimalMapper.class).selAnimalBy(“老虎”, “貓科”); System.out.println(animal1);
數(shù)據(jù)庫(kù)的字段名和實(shí)體類的屬性名不一致時(shí)
因?yàn)閿?shù)據(jù)庫(kù)命名多個(gè)單詞之間用下劃線連接,而Java命名規(guī)則為第二個(gè)字母大寫,命名規(guī)則不同時(shí),需要傳值。
方法一:修改SQL語(yǔ)句
SELECT a_sid sid,kind,numbers,address,NAME FROM animal
給a_sid 取別名為sid
xml中語(yǔ)句修改如下:

方法二:通過配置resultMap標(biāo)簽
原查詢語(yǔ)句:
< select id=“selAnimalBy” resultType=“animal” >
SELECT * FROM animal WHERE NAME=#{NAME} AND kind=#{kind}
< /select>修改為:

實(shí)現(xiàn)多個(gè)條件復(fù)雜查詢
例如:查詢動(dòng)物列表中貓科中包含虎字段的數(shù)據(jù)

SQL語(yǔ)句為:
SELECT * FROM animal WHERE 1=1 AND kind=“貓科” AND NAME LIKE ‘%虎%'
1.配置xml中的select標(biāo)簽
如下圖:

2.添加接口中的執(zhí)行語(yǔ)句
public List< Animal> selCon(Animal animal);
3.text文檔檢驗(yàn)結(jié)果
Animal animal1=new Animal(); animal1.setKind(“貓科”); animal1.setName(“虎”); List< Animal>animal2=sqlsession.getMapper(AnimalMapper.class).selCon(animal1); System.out.println(animal2);
輸出結(jié)果如下:

MyBatis條件查詢總結(jié)
1.if條件語(yǔ)句
<!-- if(判斷參數(shù)) - 將實(shí)體類不為空的屬性作為where條件 -->
<select id="getStudentList_if" resultMap="resultMap_studentEntity" parameterType="liming.student.manager.data.model.StudentEntity">
SELECT ST.STUDENT_ID,
ST.STUDENT_NAME,
ST.STUDENT_SEX,
ST.STUDENT_BIRTHDAY,
ST.STUDENT_PHOTO,
ST.CLASS_ID,
ST.PLACE_ID
FROM STUDENT_TBL ST
WHERE
<if test="studentName !=null ">
ST.STUDENT_NAME LIKE CONCAT(CONCAT('%', #{studentName, jdbcType=VARCHAR}),'%')
</if>
<if test="studentSex != null and studentSex != '' ">
AND ST.STUDENT_SEX = #{studentSex, jdbcType=INTEGER}
</if>
<if test="studentBirthday != null ">
AND ST.STUDENT_BIRTHDAY = #{studentBirthday, jdbcType=DATE}
</if>
<if test="classId != null and classId!= '' ">
AND ST.CLASS_ID = #{classId, jdbcType=VARCHAR}
</if>
<if test="classEntity != null and classEntity.classId !=null and classEntity.classId !=' ' ">
AND ST.CLASS_ID = #{classEntity.classId, jdbcType=VARCHAR}
</if>
<if test="placeId != null and placeId != '' ">
AND ST.PLACE_ID = #{placeId, jdbcType=VARCHAR}
</if>
<if test="placeEntity != null and placeEntity.placeId != null and placeEntity.placeId != '' ">
AND ST.PLACE_ID = #{placeEntity.placeId, jdbcType=VARCHAR}
</if>
<if test="studentId != null and studentId != '' ">
AND ST.STUDENT_ID = #{studentId, jdbcType=VARCHAR}
</if>
</select> 2.choose (when otherwise)
<!-- choose(判斷參數(shù)) - 按順序?qū)?shí)體類 User 第一個(gè)不為空的屬性作為:where條件 -->
<select id="getUserList_choose" resultMap="resultMap_user" parameterType="com.yiibai.pojo.User">
SELECT *
FROM User u
<where>
<choose>
<when test="username !=null ">
u.username LIKE CONCAT(CONCAT('%', #{username, jdbcType=VARCHAR}),'%')
</when >
<when test="sex != null and sex != '' ">
AND u.sex = #{sex, jdbcType=INTEGER}
</when >
<when test="birthday != null ">
AND u.birthday = #{birthday, jdbcType=DATE}
</when >
<otherwise>
</otherwise>
</choose>
</where>
</select> <select id="dynamicChooseTest" parameterType="Blog" resultType="Blog">
select * from t_blog where 1 = 1
<choose>
<when test="title != null">
and title = #{title}
</when>
<when test="content != null">
and content = #{content}
</when>
<otherwise>
and owner = "owner1"
</otherwise>
</choose>
</select>4.in的用法
? ? <select id="getNewListByLabelID" resultMap="BaseResultMap" parameterType="arraylist">
? ? ? ? SELECT
? ? ? ? <include refid="Base_Column_List"/>
? ? ? ? FROM tb_problem
? ? ? ? WHERE id IN
? ? ? ? <foreach collection="ids" index="index" item="id" separator="," close=")" open="(">
? ? ? ? ? ? #{id}
? ? ? ? </foreach>
? ? </select>還可以這樣
<select id="queryAllOpenProduct" parameterType="com.tims.open.domain.OpenProductQueryCondition"
? ? resultType="com.tims.open.domain.OpenProduct">
? ? SELECT
? ? ? ? ?*
? ? FROM
? ? ? ? product_db.product p
? ? WHERE
? ? ? ? p.isvalid = 1
? ? <if test="list != null">
? ? ? ? <foreach collection="list" index="index" item="item" separator="," open="AND p.id IN (" close=")">
? ? ? ? ? ? ? ?#{item}
? ? ? ? </foreach>
? ? </if>
</select>
//查詢condition類
Public OpenProductQueryCondition{
? ? private Integer productId; ?
? ? private List<Integer> list;
}5.模糊查詢
<!-- ******************** 模糊查詢的常用的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>以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
springboot jackson自定義序列化和反序列化實(shí)例
這篇文章主要介紹了spring boot jackson自定義序列化和反序列化實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10
Java的四種常見線程池及Scheduled定時(shí)線程池實(shí)現(xiàn)詳解
這篇文章主要介紹了Java的四種常見線程池及Scheduled定時(shí)線程池實(shí)現(xiàn)詳解,在Java中,我們可以通過Executors類來(lái)創(chuàng)建ScheduledThreadPool,Executors類提供了幾個(gè)靜態(tài)方法來(lái)創(chuàng)建不同類型的線程池,包括ScheduledThreadPool,需要的朋友可以參考下2023-09-09
java多線程開發(fā)ScheduledExecutorService簡(jiǎn)化方式
這篇文章主要為大家介紹了java多線程開發(fā)ScheduledExecutorService的簡(jiǎn)化方式,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-03-03
SpringBoot 使用@WebMvcTest測(cè)試MVC Web Controller
這篇文章主要介紹了SpringBoot 使用@WebMvcTest測(cè)試MVC Web Controller,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11
IDEA將Maven項(xiàng)目中指定文件夾下的xml等文件編譯進(jìn)classes的方法
這篇文章主要介紹了IDEA將Maven項(xiàng)目中指定文件夾下的xml等文件編譯進(jìn)classes的方法,幫助大家更好的利用IDEA進(jìn)行Java的開發(fā)學(xué)習(xí),感興趣的朋友可以了解下2021-01-01
如何在spring boot項(xiàng)目中使用Spring Security的BCryptPasswordE
本文介紹如何在Spring Boot項(xiàng)目中通過修改pom.xml引入安全依賴,添加配置類以解除默認(rèn)的HTTP請(qǐng)求攔截,以及如何創(chuàng)建BCryptPasswordEncoder對(duì)象進(jìn)行密碼的加密和匹配,通過這些步驟,可以有效地增強(qiáng)應(yīng)用的安全性2023-08-08

