mybatis 常用sql寫法

2021-10-01 05:37:16 字數 2145 閱讀 7999

1.mybatis 迴圈string(用逗號隔開的字串) ,兩種寫法

listidlist2 = getuserids(useridlist);

string userids = string.join(",",idlist);

map.put("userids",userids);

select

t1.id id,

t1.channel_name channelname,

t1.channel_type channeltype,

t1.nymanager nymanager

from core_channel t1 where 1 = 1

and t1.`ny_ae` in

'$'order by t1.id

string productids = "tpi001,tpi002,tpi003";

insert into core_channel_product_manager (

id, channel_id, product_id,

product_name, payment, sell_price, insurance_company_id,

agency_id, start_date, end_date, creator, create_date, modifier, modify_date,batch_id)

select

null,

#,t1.id,

t1.product_name,

ifnull(t1.payment, '1'),

ifnull(#, t1.price),

t1.default_company,

t1.default_agency,

#,#,

#,now(),

#,now(),

#from core_product t1 where t1.id in($)

2.mybatis 批量插入 迴圈list 

insert into core_channel_product_manager_temp

(channel_id,

product_id,

sell_price,

start_date,

end_date,

creator,

modifier,

batch_id

)values(#,

#,#,

#,#,

#,#,#)

3.一對多查詢

select

cr.id id,

cr.name name,

# roleid

from

core_resource cr,

core_resource_role crr

where cr.id = crr.resource_id

and cr.type = '1'

and cr.parent_id = '0'

and crr.role_id = #

order by

cr.priority asc

若只有兩級,則,這裡面的返回型別寫成 resulttype="com.ssish.saas.constant.db.vo.resourcetreevo",

若是大於兩級,則多次循序,故返回型別寫 resultmap="resourcevomap"

select

cr.id id,

cr.name name,

# roleid

from

core_resource cr,

core_resource_role crr

where cr.parent_id = # and

cr.id = crr.resource_id

and cr.type = '1'

and crr.role_id = #

order by

cr.priority asc

mybatis基礎 sql寫法

1.以list list 查詢結果集的寫法 select from 表名 select from 表名 select from 表名 select from 表名 2.以返回double為查詢結果 select truncate sum amount 100,2 as amount from 表名 ...

mybatis 寫法技巧

1.trim prefix prefixoverrides suffix suffixoverrides 通常用法 例子1 1 以字元 where 覆蓋首個 and 或 or 字元 select from user id and deleteflag 0 等效於 select from user i...

MyBatis 動態Sql的寫法簡單說明

根據不同的條件需要執行不同的 sql 命令.稱為動態 sql selbyaccinaccout resulttype log select from log where 1 1 ognl表示式,直接寫key或物件的屬性.不需要新增任何特字符號 if test accin null and accin...