mybatis 复杂查询

1、定义sql语句,减少我们经常手写的字段

例子:     select id,name,age from user

可以写成  <sql id="col"> id,name,age</sql>,select <include refid="col" /> from user

这样以后如果用到上面的字段或者修改col中某个字段,我们只需要修改col就好,不需要每条sql语句都修改。

 

2、条件判断

<select id="findList" resultType="java.util.Map" parameterType="java.util.Map">
	  SELECT job_id,job_name,job_status,job_cron,job_class,job_method,init_status FROM qurta_setting
	  <where>
			<if test="job_name != null and job_name != ''">
			     AND job_name like  CONCAT('%',#{job_name}, '%')
			</if> 
	  </where>
	  <choose>
			<when test="page !=null and page.orderBy != null and page.orderBy != ''">
				ORDER BY ${page.orderBy}
			</when>
            <otherwise>
				ORDER BY job_id DESC
			</otherwise>
		</choose>
	</select>

3、复杂的map查询,结构为Map<String,List<String>>

additional  就是上面map的名字,

<sql id="get_account_id" >
		<if test="additional != null">
			<foreach collection="additional" index="key"  item="val" open=" and " separator=" and ">
			     <if test="key == '不包含'">
	                 <foreach collection="val"  item="_param" separator="and" open="(" close=")" >  
		                 <if test="key == '不包含'">
		                     locate('${_param}',c.account_name)=0 
		                 </if>
	                 </foreach>  
	             </if>
	             <if test="key != '不包含'">
	                 <foreach collection="val"  item="_param" separator="or" open="(" close=")" >  
		                 <if test="key == '包含'">
		                     locate('${_param}',c.account_name)>0 
		                 </if>
		                 <if test="key == '等于'">
		                     c.account_name='${_param}' 
		                 </if>
		                 <if test="key == '开始是'">
		                     locate('${_param}',c.account_name)=1 
		                 </if>
		                 <if test="key == '结尾是'">
		                     locate(reverse('${_param}'), reverse(c.account_name) )=1 
		                 </if>
		             </foreach>
	             </if>
		    </foreach>
		</if>
	  </sql>

    

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值