mybatis基礎 sql寫法

2021-10-01 10:10:09 字數 2940 閱讀 3642

1.以list> list 查詢結果集的寫法

select * from 表名

select * from 表名

select * from 表名

select * from 表名

2.以返回double為查詢結果

select

truncate(sum(amount)/100,2) as amount from 表名 where `project_id` = #

3.以返回long為查詢結果

select

count(*)

from 表名

4.以實體返回為查詢結果

select * from 表名

5.新增寫法

insert into

(`user_id`,`display_name`,`user_image`,`org_id`,`project_id`,`mode`,`invoice_id`,`amount`,`status`,

`goods_name`,`org_name`,

`ali_trade_no`,`ali_user_id`,`ali_user_name`,`ali_custom_id`,`bfb_custom_id`,`bfb_trade_no`,`anonymous`,`pay_date`,`weixin_trade_no`,

`create_date`,`update_date`,`is_random`,`project_main_img`,`project_target`,`share_order_id`,`order_type`,`client_ip`,`don_together_id`,`month_donate_id`

) values

( #,#,#,#,#,#,#,#,#,

#,#,

#,#,#,#,#,#,#,#,#,

#,#,

#,#,

#,#,

#,#,

#,# )

6.修改寫法

update

set`user_id` = #,

`display_name` = #,

`user_image`

= #,

`org_id` =#,

`project_id`

=#,`mode`

=#,`invoice_id`=#,

`amount`=#,

`status`=#,

`goods_name`=#,

`org_name`=#,

`ali_trade_no`=#,

`ali_user_id`=#,

`ali_user_name`=#,

`ali_custom_id`=#,

`bfb_custom_id`=#,

`bfb_trade_no` =#,

`anonymous`=#,

`pay_date` =#,

`weixin_trade_no`=#,

`create_date`=#,

`update_date`=#,

`is_random`=#,

`project_main_img`=#,

`project_target`=#,

`share_order_id`=#,

`order_type`=#,

`client_ip`=#

where

`id` = #;

7.刪除寫法

delete from

表名where id = #

8.多條件排序

select

d.id,

d.company_name as companyname,

d.company_idea as

companyidea,

date_format(d.create_date, '%y-%m-%d') as entrydate,

count(c.pro_id) as pronumber,

ifnull(c.donate_amount, 0) as

donateamount,

ifnull(c.donate_material_amount, 0) as

donatematerialamount,

d.update_date as updatedate

from

gongyi_donation_company d

left join gongyi_company_donate c on d.id =

c.donate_company_id

where

1 = 1

andd.company_name like concat('%',#,'%')

and (d.is_del = 0 or c.is_del = 0)

group by

d.company_name

order by

entrydate asc,

entrydate desc,

pronumber asc,

pronumber desc,

c.donate_amount asc,

c.donate_amount desc,

c.donate_material_amount asc,

c.donate_material_amount desc,

d.update_date asc,

d.update_date desc,

d.update_date desc

mybatis 常用sql寫法

1.mybatis 迴圈string 用逗號隔開的字串 兩種寫法 listidlist2 getuserids useridlist string userids string.join idlist map.put userids userids select t1.id id,t1.channe...

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...