MyBatis 中 ${}和 #{}的正确使用方法(千万不要乱用)
作者:小布1994 发布时间:2023-11-29 05:02:37
标签: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指参数类型
来源:https://blog.csdn.net/tanhongwei1994/article/details/97380767
0
投稿
猜你喜欢
- Mybatis mapper模糊查询语句LIKE最近做学校安排的课程设计作业,用到SSM框架,在自己写mapper代码是遇到了模糊查询的问题
- 一、length()返回此字符串的长度public static void main4(String[] args) { &n
- 问题描述:解决:检查以上是否版本一致。不行就再检查下面的:如果上面的方法还是不行的话,就建议你改一下pom文件中的maven插件编译级别,可
- 前言:Android自定义View对于刚入门乃至工作几年的程序员来说都是非常恐惧的,但也是Android进阶学习的必经之路,平时
- 使用简单的fragment实现左侧导航,供大家参考,具体内容如下先上效果图:MainActivity.javapublic class Ma
- 相信绝大多数.NET玩家和我一样,常常使用Timer这个对象,而在WPF中使用DispatcherTimer的人也是很多,Dispatche
- 本文实例讲述了C#实现两接口中同名方法。分享给大家供大家参考。具体分析如下:对于一个类实现两个接口,而这两个接口又有同名方法,C#中的处理方
- session简介做过Web开发的程序员应该对Session都比较熟悉,Session是一块保存在服务器端的内存空间,一般用于保存用户的会话
- 之前我们有介绍通过Spring Boot Admin来检测服务的上下线,然后进行通知功能。https://www.jb51.net/arti
- 简介本文介绍MyBatis-Plus的分页的方法。包括:不传参数时的默认结果查询不存在的数据手动包装page自定义SQL建库建表DROP D
- 问题当我们数据库中的字段和实体类中的字段不一致的时候,查询会出问题数据库字段是 pwdid name pwd1 张三 1234562 李四
- 图像切换器(ImageSwitcher),用于实现类似于Windows操作系统的“Windows照片查看器”中的上一张、下一张切换图片的功能
- 这篇文章主要介绍了Java Collection集合iterator方法解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的
- 一、查看官网http://www.zhuozhengsoft.com/点击首页下载,进入页面:最新得5.2,我们就下载5.2版本进行测试。二
- cin的返回值今天在用STL时用到while(cin>>s1>>a>>s2>>b)这样的语句
- Dubbo过滤器概述Dubbo中的过滤器和Web应用中的过滤器的概念是一样的,提供了在服务调用前后插入自定义逻辑的途径。过滤器是整个Dubb
- View绘制的三大流程,指的是measure(测量)、layout(布局)、draw(绘制) measure负责确定View的测量宽/高,也
- 实现手机电话状态的监听,主要依靠两个类:TelephoneManger和PhoneStateListener。 TelephonseMang
- 本文实例讲述了Java实现的计算最大下标距离算法。分享给大家供大家参考,具体如下:题目描述给定一个整形数组,找出最大下标距离j−i, 当且A
- 对于多线程,大家并不陌生,对于如何创建线程也是轻车熟路,对于使用new thread和实现runable接口的方式,不再多说。这篇博文我们介