溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

MyBatis中如何使用${}和#{}

發(fā)布時間:2020-07-22 13:50:33 來源:億速云 閱讀:237 作者:小豬 欄目:開發(fā)技術(shù)

這篇文章主要為大家展示了MyBatis中如何使用${}和#{},內(nèi)容簡而易懂,希望大家可以學(xué)習一下,學(xué)習完之后肯定會有收獲的,下面讓小編帶大家一起來看看吧。

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

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

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

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);
}
<&#63;xml version="1.0" encoding="UTF-8" &#63;>

<!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>
@Test
 public void countTotal(){
  //統(tǒng)計總數(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);
  //報錯 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 ( &#63; , &#63; , &#63; )
  //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表示以什么結(jié)束。
  • collection指參數(shù)類型

以上就是關(guān)于MyBatis中如何使用${}和#{}的內(nèi)容,如果你們有學(xué)習到知識或者技能,可以把它分享出去讓更多的人看到。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI