国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - MyBatis 中 ${}和 #{}的正確使用方法(千萬不要亂用)

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

2020-07-22 11:56小布1994 Java教程

這篇文章主要介紹了MyBatis 中 ${}和 #{}的正確使用方法,本文給大家提到了MyBatis 中 ${}和 #{}的區別,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下

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

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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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);
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?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 id="findList" resultType="com.xiaobu.entity.Country">
  select * from country where id in (#{ids} )
 </select>
 
 
 <select id="findList2" resultType="com.xiaobu.entity.Country">
  select * from country where id in (${ids} )
 </select>
 
 <select id="findListByForEach" 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>
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
@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 ${}和 #{}內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!

原文鏈接:https://blog.csdn.net/tanhongwei1994/article/details/97380767

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 久久66 | jizzhd中国| 99pao成人国产永久免费视频 | 自拍偷拍专区 | 毛片一级 | 亚洲一区二区中文字幕 | 国产在线精品一区二区 | 亚洲欧美国产另类 | 亚洲一区二区三区四区五区午夜 | 久久国产精品久久久久久 | av电影免费 | 一级黄色在线观看 | 久久影院久久 | 精品综合99久久久久久www | 精品视频三区 | 天天久久 | 久久亚洲国产 | 黄色小视频在线观看 | 国产日产精品一区二区三区四区 | 日韩三区| 91超碰在线观看 | 999精品视频一区二区三区 | 色婷婷综合久久久中字幕精品久久 | 久久久久久久av | 黄色毛片a | 黄色影视| 日韩在线视频播放 | 日日夜夜草草 | 国产精品久久久久久久久久 | 欧美日韩在线一区二区三区 | 95香蕉视频 | 国产日韩一区 | 羞涩网站 | 久久亚| 日韩在线精品视频 | 含羞草www国产在线视频 | 99国产精品99久久久久久 | 欧美日韩精品一区二区在线播放 | 一区二区三区免费在线 | 91色在线视频 | 日韩欧美1区 |