亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

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

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

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

2、是字符串替換,在處理是字符串替換,MyBatis在處理時,它會將sql中的{}是字符串替換,在處理{ }是字符串替換, MyBatis在處理{ }時,它會將sql中的是字符串替換,在處理是字符串替換,MyBatis在處理時,它會將sql中的{ }替換為變量的值,傳入的數據不會加兩邊加上單引號。

注意:使用${ }會導致sql注入,不利于系統的安全性!SQL注入:就是通過把SQL命令插入到Web表單提交或輸入域名或頁面請求的查詢字符串,最終達到欺騙服務器執行惡意的SQL命令。常見的有匿名登錄(在登錄框輸入惡意的字符串)、借助異常獲取數據庫信息等

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> { /** * 功能描述:通過#{}來進行查詢 * * @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); /** * 功能描述:通過${}來進行查詢 * * @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); /** * 功能描述: 通過foreach來進行查詢 * * @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(){ //統計總數 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); //報錯 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 說明

item表示集合中每一個元素進行迭代時的別名, index指 定一個名字,用于表示在迭代過程中,每次迭代到的位置, open表示該語句以什么開始, separator表示在每次進行迭代之間以什么符號作為分隔符, close表示以什么結束。 collection指參數類型

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

標簽: Mybatis 數據庫
相關文章:
主站蜘蛛池模板: 男女爱爱视频在线观看 | 一级特黄毛片 | 国产h在线播放 | 亚洲精品综合久久 | 日韩一级二级三级 | 国产va免费精品观看 | 成人αv在线视频高清 | 欧美成人h版影院在线播放 欧美成人h版整片合集 | 国产小视频2023 | julia中文字幕久久亚洲 | 高清性色生活片免费观看 | 免费观看三级毛片 | 黄色大片视频网站 | 丁香天堂网 | 青青青国产免费手机视频在线观看 | 亚洲成人在线网站 | 最新国产成人盗摄精品视频 | 久久综合九色综合亚洲小说 | 日韩美女一级毛片a | 日韩欧美一级毛片视频免费 | 亚洲精品国产福利在线观看 | 欧美日韩在线观看精品 | 色免费观看| 91久久国产露脸国语对白 | 在线观看视频一区 | 国产一级精品高清一级毛片 | 香港三级理论在线观看网站 | 黄色毛片在线观看 | 97视频在线 | 欧美ol丝袜高跟秘书在线播放 | 视频在线观看大片 | 国产无遮挡色视频免费视频 | 成人免费一区二区三区在线观看 | 日本色图视频 | 国产级a爱做片免费观看 | 国产成人精品亚洲77美色 | 欧美性性性性性色大片免费的 | 欧美一级α片毛片免费观看 | 182福利视频在线观看黄 | 亚洲综合图 | 99在线精品免费视频 |