1.if条件语句
<!-- if(判断参数) - 将实体类不为空的属性作为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(判断参数) - 按顺序将实体类 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}就成了字符串的一部分,
会发生这样一个异常: 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>