久久福利_99r_国产日韩在线视频_直接看av的网站_中文欧美日韩_久久一

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

MyBatis 中 ${}和 #{}的正確使用方法(千萬(wàn)不要亂用)

瀏覽:7日期:2023-10-23 14:58:02

1、#{}是預(yù)編譯處理,MyBatis在處理#{ }時(shí),它會(huì)將sql中的#{ }替換為?,然后調(diào)用PreparedStatement的set方法來(lái)賦值,傳入字符串后,會(huì)在值兩邊加上單引號(hào),如上面的值 “4,44,514”就會(huì)變成“ ‘4,44,514’ ”;

2、是字符串替換,在處理是字符串替換,MyBatis在處理時(shí),它會(huì)將sql中的{}是字符串替換,在處理{ }是字符串替換, MyBatis在處理{ }時(shí),它會(huì)將sql中的是字符串替換,在處理是字符串替換,MyBatis在處理時(shí),它會(huì)將sql中的{ }替換為變量的值,傳入的數(shù)據(jù)不會(huì)加兩邊加上單引號(hào)。

注意:使用${ }會(huì)導(dǎo)致sql注入,不利于系統(tǒng)的安全性!SQL注入:就是通過(guò)把SQL命令插入到Web表單提交或輸入域名或頁(yè)面請(qǐng)求的查詢字符串,最終達(dá)到欺騙服務(wù)器執(zhí)行惡意的SQL命令。常見(jiàn)的有匿名登錄(在登錄框輸入惡意的字符串)、借助異常獲取數(shù)據(jù)庫(kù)信息等

package com.xiaobu.mapper;import com.xiaobu.base.mapper.MyMapper;import com.xiaobu.entity.Country;import java.util.List;/** * @author xiaobu * @version JDK1.8.0_171 * @date on 2018/11/27 19:21 * @description V1.0 */public interface CountryMapper extends MyMapper<Country> { /** * 功能描述:通過(guò)#{}來(lái)進(jìn)行查詢 * * @param ids id * @return java.util.List<com.xiaobu.entity.Country> * @author xiaobu * @date 2019/7/26 11:53 * @version 1.0 */ List<Country> findList(String ids); /** * 功能描述:通過(guò)${}來(lái)進(jìn)行查詢 * * @param ids id * @return java.util.List<com.xiaobu.entity.Country> * @author xiaobu * @date 2019/7/26 11:53 * @version 1.0 */ List<Country> findList2(String ids); /** * 功能描述: 通過(guò)foreach來(lái)進(jìn)行查詢 * * @param ids id * @return java.util.List<com.xiaobu.entity.Country> * @author xiaobu * @date 2019/7/26 11:53 * @version 1.0 */ List<Country> findListByForEach(List<Integer> ids);}

<?xml version='1.0' encoding='UTF-8' ?><!DOCTYPE mapper PUBLIC '-//mybatis.org//DTD Mapper 3.0//EN' 'http://mybatis.org/dtd/mybatis-3-mapper.dtd' ><mapper namespace='com.xiaobu.mapper.CountryMapper'> <select resultType='com.xiaobu.entity.Country'> select * from country where id in (#{ids} ) </select> <select resultType='com.xiaobu.entity.Country'> select * from country where id in (${ids} ) </select> <select parameterType='List' resultType='com.xiaobu.entity.Country'> select * from country where id in <foreach collection='list' index='index' item='item' open='(' separator=',' close=')'> #{item} </foreach> </select></mapper>

@Test public void countTotal(){ //統(tǒng)計(jì)總數(shù) SELECT COUNT(Id) FROM country Example example = new Example(City.class); int count =countryMapper.selectCountByExample(example); System.out.println('count = ' + count); //按條件查詢 SELECT COUNT(Id) FROM country Country country = new Country(); //country.setCountryname('1234'); int conunt2 = countryMapper.selectCount(country); System.out.println('conunt2 = ' + conunt2); } @Test public void findList(){ //Preparing: select * from country where id in ( ’1,2,3’) List<Country> countries = countryMapper.findList('1,2,3'); //countries = [Country(countryname=Angola, countrycode=AO)] System.out.println('countries = ' + countries); //報(bào)錯(cuò) There is no getter for property named ’ids’ in ’class java.lang.String List<Country> countries2 = countryMapper.findList2('1,2,3'); System.out.println('countries2 = ' + countries2); } @Test public void findListByForeach(){ //Preparing: select * from country where id in ( ? , ? , ? ) //Parameters: 1(Integer), 2(Integer), 3(Integer) List<Integer> list = new ArrayList<>(3); list.add(1); list.add(2); list.add(3); List<Country> countries2 = countryMapper.findListByForEach(list); System.out.println('countries2 = ' + countries2); }

foreach 說(shuō)明

item表示集合中每一個(gè)元素進(jìn)行迭代時(shí)的別名, index指 定一個(gè)名字,用于表示在迭代過(guò)程中,每次迭代到的位置, open表示該語(yǔ)句以什么開(kāi)始, separator表示在每次進(jìn)行迭代之間以什么符號(hào)作為分隔符, close表示以什么結(jié)束。 collection指參數(shù)類型

到此這篇關(guān)于MyBatis 中 ${}和 #{}的正確使用方法(千萬(wàn)不要亂用)的文章就介紹到這了,更多相關(guān)MyBatis ${}和 #{}內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

相關(guān)文章:
主站蜘蛛池模板: 国产a视频 | 亚洲欧美日韩国产综合 | 成人免费视频在线观看 | 久久久久中文字幕 | 国产精品久久久久久亚洲影视 | 亚洲wu码| 在线播放国产精品 | 久久综合九色综合欧美狠狠 | 欧美三级影院 | 国产精品中文字幕在线观看 | 国产美女精品 | 精品国产一区二区三区高潮视 | 欧美日韩国产在线 | 日本黄色一级片视频 | 亚洲福利片 | 麻豆视频国产 | 午夜国产羞羞视频免费网站 | 国产精品欧美日韩 | 国产亚洲成av人片在线观看桃 | 欧美一区二区三区 | 网站一区二区三区 | 亚洲国产成人av好男人在线观看 | 久久99精品久久久久子伦 | 91夜夜 | 欧美精品一区二区三区在线 | 亚洲精品乱码久久久久久 | 亚洲黄色高清视频 | 国产成人高清在线 | 免费看爱爱视频 | a成人在线 | 一本色道久久综合狠狠躁篇的优点 | 日韩在线一区二区三区 | 国产精品999 | 国产精品毛片久久久久久 | 美国黄色毛片女人性生活片 | 91夜夜操 | 无毒黄网| 中文字幕91 | 国产精品成人一区二区三区夜夜夜 | 91视频免费观看 | 日韩精品一区二区三区四区五区 |