多条件模糊查询的sql怎么写?

给大家举个栗子:

	<select id="selectPositionRecordByPageCount" resultType="java.lang.Long">
		SELECT
			COUNT(1)
		FROM
			t_oa_adjust_position_record r
		INNER JOIN t_oa_employee e ON e.oid=r.employee_oid
		INNER JOIN t_oa_employee_details e1 ON r.employee_oid = e1.employee_oid
		INNER JOIN t_oms_user u ON e.user_oid = u.oid
		INNER JOIN t_oms_org o ON u.dept_id = o.oid
		<where>
    		 <include refid="query_positionRecord_where"></include>  
    	</where>
	</select>
	<select id="selectPositionRecordByPage" resultType="SelectPositionRecordByPage">
		SELECT
			r.oid,
			e.name,
			o.name department,
			e.position,
			e.work_number,
			e1.entry_time,
			approval_initiate_date,
			before_adjust_department,
			after_adjust_department,
			before_adjust_position,
			after_adjust_position,
			before_adjust_rank,
			after_adjust_rank,
			position_adjust_start_time,
			position_adjust_type,
			position_adjust_reason
		FROM
			t_oa_adjust_position_record r
		INNER JOIN t_oa_employee e ON e.oid=r.employee_oid
		INNER JOIN t_oa_employee_details e1 ON r.employee_oid = e1.employee_oid
		INNER JOIN t_oms_user u ON e.user_oid = u.oid
		INNER JOIN t_oms_org o ON u.dept_id = o.oid
		<where>
    		 <include refid="query_positionRecord_where"></include>  
    	</where> 
    	order by e.oid desc,r.position_adjust_start_time desc,r.updated_at desc 
    	limit #{pageIndex},#{pageSize};	
	</select>
	
	<sql id="query_positionRecord_where">  
    	<if test="name != null and name != ''">
    		and e.name like #{name}
    	</if>
    	<if test="department != null and department != ''">
    		and u.dept_id in 
    		<foreach collection="department" item="departmentId" index="index"
				open="(" close=")" separator=",">
				#{departmentId}
  			</foreach>
    	</if>
    	<if test="position != null and position != ''">
    		and e.position=#{position}
    	</if>
    	<if test="workNumber != null and workNumber != ''">
    		and e.work_number like #{workNumber}
    	</if>
    	<if test="approvalInitiateDate != null and approvalInitiateDate != ''">
    		and r.approval_initiate_date=#{approvalInitiateDate}
    	</if>
    	<if test="positionAdjustType != null and positionAdjustType != ''">
    		and r.position_adjust_type=#{positionAdjustType}
    	</if>
 	</sql>

      本人做项目用的是框架是SSM,在mybatis下sql语句是写在xml里的,大家可以看到where条件里面引入了一段sql,在这段sql中用的基本是“=”,有需要模糊查询的就用"like",其中有一个字段因为传过来的是一个list集合,故做了上述处理。

       另外说一下,还加了分页噢!

       希望能帮到你!学习路上共勉!微笑

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

关阿炎

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值