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

 更新时间:2020年07月22日 10:14:32   作者:小布1994  
这篇文章主要介绍了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命令。常见的有匿名登录(在登录框输入恶意的字符串)、借助异常获取数据库信息等

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指参数类型

到此这篇关于MyBatis 中 ${}和 #{}的正确使用方法(千万不要乱用)的文章就介绍到这了,更多相关MyBatis ${}和 #{}内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • synchronized及JUC显式locks 使用原理解析

    synchronized及JUC显式locks 使用原理解析

    这篇文章主要为大家介绍了synchronized及JUC显式locks 使用原理解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-12-12
  • 详解Java如何判断一个对象是否为空

    详解Java如何判断一个对象是否为空

    我们在刚开始学习Java的时候,遇到过最多的异常肯定是臭名昭著的空指针异常(NullPointerException),可以说它陪伴了我们整个初学阶段,那么如何优雅的判断一个对象是否为空并且减少空指针异常呢,
    2024-01-01
  • JavaScript中的closest方法示例详解

    JavaScript中的closest方法示例详解

    这篇文章主要介绍了JavaScript中closest方法的相关资料,closest()是JavaScript中的一个非常实用的 DOM 方法,用于查找与当前元素匹配的最近的祖先元素,它支持 CSS 选择器,可以应用于事件委托、动态内容处理等场景,需要的朋友可以参考下
    2025-02-02
  • MAC上IntelliJ IDEA的svn无法保存密码解决方案

    MAC上IntelliJ IDEA的svn无法保存密码解决方案

    今天小编就为大家分享一篇关于MAC上IntelliJ IDEA的svn无法保存密码解决方案,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2018-10-10
  • Spring Boot实战之静态资源处理

    Spring Boot实战之静态资源处理

    这篇文章主要介绍了Spring Boot实战之静态资源处理,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-01-01
  • Java Calendar类的详解及使用实例

    Java Calendar类的详解及使用实例

    这篇文章主要介绍了Java Calendar类的详解及使用实例的相关资料,需要的朋友可以参考下
    2017-04-04
  • java中JSON字符串转换为Map集合的两种方法

    java中JSON字符串转换为Map集合的两种方法

    本文主要介绍了java中JSON字符串转换为Map集合,包含了两种方法,这种需求可能涉及到从外部接口获取数据,或者在程序中处理配置信息等,感兴趣的可以了解一下
    2024-07-07
  • 解决springboot整合cxf-jaxrs中json转换的问题

    解决springboot整合cxf-jaxrs中json转换的问题

    这篇文章主要介绍了解决springboot整合cxf-jaxrs中json转换的问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-07-07
  • spring springMVC中常用注解解析

    spring springMVC中常用注解解析

    这篇文章主要介绍了spring springMVC中常用注解,本文给大家介绍的非常详细,需要的朋友可以参考下
    2018-05-05
  • Spring定时任务@scheduled多线程使用@Async注解示例

    Spring定时任务@scheduled多线程使用@Async注解示例

    这篇文章主要为大家介绍了Spring定时任务@scheduled多线程使用@Async注解示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-11-11

最新评论