MyBatis 中 ${}和 #{}的正确使用方法(千万不要乱用)

作者:小布1994 时间:2023-11-29 05:02:37 

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 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(){
 //统计总数 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指参数类型

来源:https://blog.csdn.net/tanhongwei1994/article/details/97380767

标签:MyBatis,${},#{}
0
投稿

猜你喜欢

  • Android Studio升级到3.0后遇到的坑

    2022-01-23 00:59:07
  • Java数据结构及算法实例:冒泡排序 Bubble Sort

    2022-10-17 08:39:45
  • Spring Boot Reactor 整合 Resilience4j详析

    2021-08-08 10:30:02
  • Java Hutool 包工具类推荐 ExcelUtil详解

    2023-01-22 23:47:12
  • SpringBoot通过@Value实现给静态变量注入值详解

    2022-04-30 14:30:37
  • Spring-boot 2.3.x源码基于Gradle编译过程详解

    2022-04-07 15:41:36
  • C#和Java有什么区别和联系

    2022-01-02 04:33:26
  • Mybatis中的延迟加载案例解析

    2023-02-27 01:55:37
  • java开发中使用IDEA活动模板快速增加注释的方法

    2021-09-25 20:42:24
  • 教你怎么用Idea打包jar包

    2023-03-15 03:30:51
  • Android的RV列表刷新详解Payload与Diff方式异同

    2023-07-05 13:17:29
  • Android登录时密码保护功能

    2023-10-24 07:09:40
  • java rocketmq--消息的产生(普通消息)

    2023-10-19 08:51:50
  • java实现仿windows 字体设置选项卡实例

    2023-01-02 11:45:42
  • servlet上传文件实现代码详解(四)

    2021-09-04 21:17:23
  • Spring Security和Shiro的相同点与不同点整理

    2023-01-15 17:07:20
  • Android实现简单的banner轮播图

    2021-10-25 01:11:28
  • spring boot使用自定义的线程池执行Async任务

    2023-08-15 07:41:25
  • Java并发编程之详解CyclicBarrier线程同步

    2023-08-01 14:08:47
  • jvm调优的几种场景(小结)

    2023-04-11 18:37:04
  • asp之家 软件编程 m.aspxhome.com