java动态标签_MyBatis动态SQL标签用法实例详解

本文通过实例代码给大家介绍了MyBatis动态SQL标签用法,非常不错,具有参考借鉴价值,需要的朋友参考下吧

1、动态SQL片段

通过SQL片段达到代码复用

select count(*)

select *

from icp

name like '%$name$%'

path like '%path$%'

area_id = #area_id#

hided = #hided#

limit #_start#, #_size#

2、数字范围查询

所传参数名称是捏造所得,非数据库字段,比如_img_size_ge、_img_size_lt字段

img_size >= #_img_size_ge#

]]>

img_size < #_img_size_lt#

]]>

多次使用一个参数也是允许的

execplantime >= #_now#

]]>

closeplantime <= #_now#

]]>

3、时间范围查询

createtime >= #_starttime#

and createtime < #_endtime#

]]>

4、in查询

state in ('$_in_state$')

5、like查询

(chnameone like '%$chnameone$%' or spellinitial like '%$chnameone$%')

chnametwo like '%$chnametwo$%'

6、or条件

(t.finished='11' or t.failure=3)

]]>

t.finished in ('10','19') and t.failure<3

]]>

7、where子查询

code not in

(select t.contentcode

from cms_ccm_programcontent t

where t.contenttype='MZNRLX_MA'

and t.programcode = #exprogramcode#)

select *

from cms_ccm_material

where code in

(select t.contentcode

from cms_ccm_programcontent t

where t.contenttype = 'MZNRLX_MA'

and programcode = #value#)

order by updatetime desc

9、函数的使用

insert into rulemaster(

name,

createtime,

updatetime,

remark

) values (

#name#,

now(),

now(),

#remark#

)

select LAST_INSERT_ID()

update rulemaster set

name = #name#,

updatetime = now(),

remark = #remark#

where id = #id#

10、map结果集

select count(a.*)

select a.id vid,

a.img imgurl,

a.img_s imgfile,

b.vfilename vfilename,

b.name name,

c.id sid,

c.url url,

c.filename filename,

c.status status

From secfiles c, juji b, videoinfo a

where

a.id = b. videoid

and b.id = c.segmentid

and c.status = 0

order by a.id asc,b.id asc,c.sortnum asc

limit #_start#, #_size#

11、trim

trim是更灵活的去处多余关键字的标签,他可以实践where和set的效果。

where例子的等效trim语句:

Xml代码

SELECT * from STUDENT_TBL ST

ST.STUDENT_NAME LIKE CONCAT(CONCAT('%', #{studentName}),'%')

AND ST.STUDENT_SEX = #{studentSex}

set例子的等效trim语句:

Xml代码

UPDATE STUDENT_TBL

STUDENT_TBL.STUDENT_NAME = #{studentName},

STUDENT_TBL.STUDENT_SEX = #{studentSex},

STUDENT_TBL.STUDENT_BIRTHDAY = #{studentBirthday},

STUDENT_TBL.CLASS_ID = #{classEntity.classID}

WHERE STUDENT_TBL.STUDENT_ID = #{studentID};

12、choose (when, otherwise)

有时候我们并不想应用所有的条件,而只是想从多个选项中选择一个。MyBatis提供了choose 元素,按顺序判断when中的条件出否成立,如果有一个成立,则choose结束。当choose中所有when的条件都不满则时,则执行 otherwise中的sql。类似于Java 的switch 语句,choose为switch,when为case,otherwise则为default。

if是与(and)的关系,而choose是或(or)的关系。

例如下面例子,同样把所有可以限制的条件都写上,方面使用。选择条件顺序,when标签的从上到下的书写顺序:

Xml代码

SELECT * from STUDENT_TBL ST

ST.STUDENT_NAME LIKE CONCAT(CONCAT('%', #{studentName}),'%')

AND ST.STUDENT_SEX = #{studentSex}

AND ST.STUDENT_BIRTHDAY = #{studentBirthday}

AND ST.CLASS_ID = #{classEntity.classID}

以上所述是小编给大家介绍的MyBatis动态SQL标签用法实例详解,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值