mybatis只能java用吗_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 {

/**

* 功能描述:通过#{}来进行查询

*

* @param ids id

* @return java.util.List

* @author xiaobu

* @date 2019/7/26 11:53

* @version 1.0

*/

List findList(String ids);

/**

* 功能描述:通过${}来进行查询

*

* @param ids id

* @return java.util.List

* @author xiaobu

* @date 2019/7/26 11:53

* @version 1.0

*/

List findList2(String ids);

/**

* 功能描述: 通过foreach来进行查询

*

* @param ids id

* @return java.util.List

* @author xiaobu

* @date 2019/7/26 11:53

* @version 1.0

*/

List findListByForEach(List ids);

}

select * from country where id in (#{ids} )

select * from country where id in (${ids} )

select * from country where id in

#{item}

@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 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 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 list = new ArrayList<>(3);

list.add(1);

list.add(2);

list.add(3);

List countries2 = countryMapper.findListByForEach(list);

System.out.println("countries2 = " + countries2);

}

foreach 说明

item表示集合中每一个元素进行迭代时的别名,

index指 定一个名字,用于表示在迭代过程中,每次迭代到的位置,

open表示该语句以什么开始,

separator表示在每次进行迭代之间以什么符号作为分隔符,

close表示以什么结束。

collection指参数类型

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MyBatis,${}和#{}是用来替换SQL语句的参数的。它们的使用场合略有不同。 ${}是字符串替换,当MyBatis处理${}时,它会将SQL的${}替换为变量的值,传入的数据不会加两边加上单引号。这种情况适用于需要动态拼接SQL语句的场景,比如动态指定表名或列名。 #{}是预编译处理,当MyBatis处理#{}时,它会将SQL的#{}替换为?,然后调用PreparedStatement的set方法来赋值。传入的字符串会在值两边加上单引号,以确保安全性和正确性。这种情况适用于需要传入参数并进行预编译的场景,比如查询条件的参数。 总结起来,${}适用于动态拼接SQL语句的场景,而#{}适用于传入参数并进行预编译的场景。在使用时,我们需要根据具体的需求选择合适的方式来处理参数。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Mybatis $ 和 #千万不要乱用](https://blog.csdn.net/wyouwd1/article/details/126130428)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [Mybatis系列:Mybatis $ 和 # 千万不要乱用!](https://blog.csdn.net/Mrs_chens/article/details/90403648)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值