網(wǎng)站建設(shè)好了怎么進行推廣會計培訓班哪個機構(gòu)比較好
#{}和${}的區(qū)別:
#{}: 底層使用PreparedStatement。特點:先進行SQL語句的編譯,然后給SQL語句的占位符問號?傳值??梢员苊釹QL注入的風險。
${}:底層使用Statement。特點:先進行SQL語句的拼接,然后再對SQL語句進行編譯。存在SQL注入的風險。
優(yōu)先使用#{},這是原則。避免SQL注入的風險。
什么時候使用${}?
1.如果需要SQL語句的關(guān)鍵字放到SQL語句中,只能使用${},因為#{}是以值的形式放到SQL語句當中的。
如有的時候我們的sql語句需要使用esc與desc來決定查詢結(jié)果是升序還是降序
附上兩種的執(zhí)行結(jié)果
#{}的執(zhí)行結(jié)果:
Preparing: selectid, car_num as carNum, brand, guide_price as guidePrice, produce_time as produceTime, car_type as carTypefrom t_car order by produce_time ?
Parameters: asc(String)selectid, car_num as carNum, brand, guide_price as guidePrice, produce_time as produceTime, car_type as carType
from t_car order by produce_time 'asc'
${}的執(zhí)行結(jié)果:
Preparing:select id, car_num as carNum, brand, guide_price as guidePrice, produce_time as produceTime, car_type as carTypefrom t_car order by produce_time asc
Parameters:
2. 向SQL語句當中拼接表名,就需要使用${}
2. 向SQL語句當中拼接表名,就需要使用${}現(xiàn)實業(yè)務當中,可能會存在分表存儲數(shù)據(jù)的情況。因為一張表存的話,數(shù)據(jù)量太大。查詢效率比較低??梢詫⑦@些數(shù)據(jù)有規(guī)律的分表存儲,這樣在查詢的時候效率就比較高。因為掃描的數(shù)據(jù)量變少了。日志表:專門存儲日志信息的。如果t_log只有一張表,這張表中每一天都會產(chǎn)生很多l(xiāng)og,慢慢的,這個表中數(shù)據(jù)會很多。怎么解決問題?可以每天生成一個新表。每張表以當天日期作為名稱,例如:t_log_20220901t_log_20220902 ....你想知道某一天的日志信息怎么辦?假設(shè)今天是20220901,那么直接查:t_log_20220901的表即可。
3.批量刪除:一次刪除多條記錄。
批量刪除的SQL語句有兩種寫法:第一種or:delete from t_car where id=1 or id=2 or id=3;第二種int:delete from t_car where id in(1,2,3);應該采用${}的方式:delete from t_car where id in(${ids});
4.模糊查詢:like
第一種方案:
‘% $ {brand}%’
第二種方案:concat函數(shù),這個是mysql數(shù)據(jù)庫當中的一個函數(shù),專門進行字符串拼接
concat(‘%’,#{brand},‘%’)
第三種方案:比較雞肋了??梢圆凰?。
concat(‘%’,‘${brand}’,‘%’)
第四種方案:
“%”#{brand}“%”