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

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

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

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

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

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

2.添加接口中的執(zhí)行語句
public List< Animal> selCon(Animal animal);
3.text文檔檢驗結(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條件語句
<!-- if(判斷參數(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ū)嶓w類 User 第一個不為空的屬性作為: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}就成了字符串的一部分,
? ? ? ? ? ? ? ? 會發(fā)生這樣一個異常: The error occurred while setting parameters,
? ? ? ? ? ? ? ? 應(yīng)該寫成: "%"#{name}"%",即#{name}是一個整體,前后加上%
? ? ? ? ? ? -->
? ? ? ? ? ? <if test="name != null">
? ? ? ? ? ? ? ? name like "%"#{name}"%"
? ? ? ? ? ? </if>
? ? ? ? ? ? <!--方法二: 使用concat(str1,str2)函數(shù)將兩個參數(shù)連接 -->
? ? ? ? ? ? <if test="phone != null">
? ? ? ? ? ? ? ? and phone like concat(concat("%",#{phone}),"%")
? ? ? ? ? ? </if>
? ? ? ? ? ? <!--方法三: 使用 bind 標(biāo)簽,對字符串進行綁定,然后對綁定后的字符串使用 like 關(guān)鍵字進行模糊查詢 -->
? ? ? ? ? ? <if test="email != null">
? ? ? ? ? ? ? ? <bind name="pattern" value="'%'+email+'%'"/>
? ? ? ? ? ? ? ? and email like #{pattern}
? ? ? ? ? ? </if>
? ? ? ? </where>
? ? </select>以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java通過MyBatis框架對MySQL數(shù)據(jù)進行增刪查改的基本方法
MyBatis框架由Java的JDBC API進一步封裝而來,在操作數(shù)據(jù)庫方面效果拔群,接下來我們就一起來看看Java通過MyBatis框架對MySQL數(shù)據(jù)進行增刪查改的基本方法:2016-06-06
SpringCloud微服務(wù)熔斷器Hystrix使用詳解
這篇文章主要介紹了Spring Cloud Hyxtrix的基本使用,它是Spring Cloud中集成的一個組件,在整個生態(tài)中主要為我們提供服務(wù)隔離,服務(wù)熔斷,服務(wù)降級功能,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07
eclipse創(chuàng)建項目沒有dynamic web的解決方法
最近上課要用到eclipse,要用到Dynamic web project.但是我下載的版本上沒有.接下來就帶大家了解 eclipse創(chuàng)建項目沒有dynamic web的解決方法,文中有非常詳細(xì)的圖文示例,需要的朋友可以參考下2021-06-06
Springboot整合阿里巴巴SMS的實現(xiàn)示例
本文主要介紹了Springboot整合阿里巴巴SMS的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-12-12
SpringBoot設(shè)置首頁(默認(rèn)頁)跳轉(zhuǎn)功能的實現(xiàn)方案
這篇文章主要介紹了SpringBoot設(shè)置首頁(默認(rèn)頁)跳轉(zhuǎn)功能,本文通過兩種方案,給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2019-07-07

