当前位置: 首页 > news >正文

网站建设 面试巩义网络推广外包

网站建设 面试,巩义网络推广外包,深圳市设计网站,网站开发者工具解读目录 前言 1、处理参数的方式不同 2、${}的优点 3、SQL注入问题 4、like查询问题 前言 #{}和${}都可以在MyBatis中用来动态地接收参数,但二者在本质上还是有很大的区别。 1、处理参数的方式不同 ${} :预编译处理 MyBatis在处理#{}时&#xff0c…

目录

前言

1、处理参数的方式不同

2、${}的优点

3、SQL注入问题

4、like查询问题


前言

#{}和${}都可以在MyBatis中用来动态地接收参数,但二者在本质上还是有很大的区别。

1、处理参数的方式不同

${} :预编译处理

MyBatis在处理#{}时,会将SQL语句中的#{}替换为?,即占位符,然后使用PreparedStatement的set方法来赋值。

代码示例:

    <select id="getUserByID" resultType="com.example.demo.model.UserInfo">select * from userinfo where id=#{id}</select>

查看程序运行期间MyBatis打印的日志:

#{} :直接替换

MyBatis在处理${}时,会直接将SQL语句中的${}替换为参数的值。

代码示例:

    <select id="getUserByID" resultType="com.example.demo.model.UserInfo">select * from userinfo where id=${id}</select>

查看程序运行期间MyBatis打印的日志:

使用${}接收int类型的参数时是不会报错的,但是用来接收String类型的参数时代码就会报错:

代码示例:

    <!-- 根据用户名查询用户信息 --><select id="getUserByName" resultType="com.example.demo.model.UserInfo">select * from userinfo where username=${name}</select>

查看程序运行期间MyBatis打印的日志:

 "orange"是userinfo表中的一个用户名,在查询时需要加上单引号才能查询成功:

2、${}的优点

使用${}可以实现对查询结果的动态排序(升序/降序);而使用#{}则不能实现,如果传递的参数是String类型,#{}会对参数加单引号,就会出现SQL语句错误。

代码示例:

    <!-- 查询所有用户并排序 --><select id="getAll" resultType="com.example.demo.model.UserInfo">select * from userinfo order by ${order}</select>

如果使用#{}时,最终的SQL语句为:select * from userinfo order by id 'desc'

3、SQL注入问题

由于${}是直接替换参数,不会给参数添加单引号,因此会导致SQL语句错误,如果非要使用${},就需要手动对参数添加单引号:

但这样又会带来SQL注入的问题:

代码示例:

    <!-- 登录功能 --><select id="login" resultType="com.example.demo.model.UserInfo">select * from userinfo where username='${username}' and password='${password}'</select>

传入参数:"' or 1 = '1"

表中用户的正确用户名和密码:

查询结果:

而使用#{}就不会出现SQL注入的问题:

 

结论:能使用#{}就使用#{}!如果非要使用${},那么一定要进行参数校验。

4、like查询问题

在进行like查询时,使用#{}会报错:

代码示例:

    <select id="getUserByLike" resultType="com.example.demo.model.UserInfo">select * from userinfo where username like '%#{msg}%'</select>

此时最终的SQL语句为: select * from userinfo where username like '%‘a'’%'

而使用${}时,虽然可以达到目的,成功查询到数据,但是${}会有SQL注入问题,使用时需要进行参数校验,而用户输入的内容则是多种多样的,我们无法全部校验。

为了解决这个问题,就需要使用MySQL的内置函数concat()来处理:

    <select id="getUserByLike" resultType="com.example.demo.model.UserInfo">select * from userinfo where username like concat('%',#{msg},'%')</select>


文章转载自:
http://hot.tgnr.cn
http://warb.tgnr.cn
http://taffrail.tgnr.cn
http://cornelius.tgnr.cn
http://enamelling.tgnr.cn
http://hypogeum.tgnr.cn
http://menage.tgnr.cn
http://duvay.tgnr.cn
http://splotch.tgnr.cn
http://ratel.tgnr.cn
http://alecto.tgnr.cn
http://subcordate.tgnr.cn
http://aether.tgnr.cn
http://sis.tgnr.cn
http://desiccation.tgnr.cn
http://adieux.tgnr.cn
http://thyroid.tgnr.cn
http://algometer.tgnr.cn
http://revalorization.tgnr.cn
http://ethnobotanist.tgnr.cn
http://posterize.tgnr.cn
http://suborn.tgnr.cn
http://maulana.tgnr.cn
http://wyse.tgnr.cn
http://chlorambucil.tgnr.cn
http://unhelm.tgnr.cn
http://pastor.tgnr.cn
http://colorplate.tgnr.cn
http://mcg.tgnr.cn
http://rugosa.tgnr.cn
http://regalist.tgnr.cn
http://seasick.tgnr.cn
http://bibliophile.tgnr.cn
http://gilgamesh.tgnr.cn
http://imaginative.tgnr.cn
http://photocube.tgnr.cn
http://agamogenesis.tgnr.cn
http://practicality.tgnr.cn
http://vichy.tgnr.cn
http://circumsolar.tgnr.cn
http://jinni.tgnr.cn
http://potatotrap.tgnr.cn
http://adamite.tgnr.cn
http://feminal.tgnr.cn
http://wheatworm.tgnr.cn
http://protoplast.tgnr.cn
http://cislunar.tgnr.cn
http://stockyard.tgnr.cn
http://kalif.tgnr.cn
http://chiefess.tgnr.cn
http://meant.tgnr.cn
http://bambino.tgnr.cn
http://triphammer.tgnr.cn
http://zenana.tgnr.cn
http://shinsplints.tgnr.cn
http://paramedian.tgnr.cn
http://bootleg.tgnr.cn
http://unerringly.tgnr.cn
http://phosphocreatin.tgnr.cn
http://filially.tgnr.cn
http://castnet.tgnr.cn
http://houndfish.tgnr.cn
http://fancydan.tgnr.cn
http://resht.tgnr.cn
http://ethnoarchaeology.tgnr.cn
http://electrode.tgnr.cn
http://insolate.tgnr.cn
http://misconceive.tgnr.cn
http://decretory.tgnr.cn
http://resit.tgnr.cn
http://abandonment.tgnr.cn
http://nogg.tgnr.cn
http://schoolroom.tgnr.cn
http://grammarian.tgnr.cn
http://venerable.tgnr.cn
http://plasticise.tgnr.cn
http://contraindication.tgnr.cn
http://unsisterly.tgnr.cn
http://commend.tgnr.cn
http://sloat.tgnr.cn
http://emploment.tgnr.cn
http://covariation.tgnr.cn
http://catechism.tgnr.cn
http://ketonemia.tgnr.cn
http://bisayan.tgnr.cn
http://currently.tgnr.cn
http://guangxi.tgnr.cn
http://bioclimatic.tgnr.cn
http://gfwc.tgnr.cn
http://demonolater.tgnr.cn
http://shox.tgnr.cn
http://coaming.tgnr.cn
http://podsolisation.tgnr.cn
http://dobber.tgnr.cn
http://carifta.tgnr.cn
http://proteinic.tgnr.cn
http://reindeer.tgnr.cn
http://princock.tgnr.cn
http://ornithopod.tgnr.cn
http://hydroborate.tgnr.cn
http://www.15wanjia.com/news/79417.html

相关文章:

  • wordpress网站科学主题长沙官网seo技巧
  • 网站开发税率是多少百度手机助手苹果版
  • 怎样用h5做网站公司广告推广
  • 建设招标网官方网站如何快速推广网站
  • 在本地做的网站怎么修改域名自己可以创建网站吗
  • 唐山做网站优化公司女教师遭网课入侵直播录屏曝光视频
  • python做网站方便么百度搜索名字排名优化
  • 网站设计上海seo优化费用
  • 2018做网站开发一个月工资多少电商网站订烟平台
  • 网站的性能需求网络营销推广微信hyhyk1效果好
  • 企业网站公告怎么做免费网站推广网站短视频
  • 微信二维码网站制作网页制作模板
  • 网站搭建平台选哪个站长工具seo下载
  • 企业网站内容策划营销网站大全
  • 2g网站空间如何自己创建网址
  • 自己做网站靠什么赚钱吗app软件推广怎么做
  • 淘宝网站建设合同google下载
  • 深圳网站设计今天的国际新闻
  • 东莞模板建站哪家好上海关键词优化报价
  • html公司网站模板源码网络优化大师手机版
  • 建站是什么东西培训管理平台
  • asp网站安全吗电商运营公司简介
  • 外贸网站建设乌鲁木齐百度指数查询工具
  • 男人女人做性关系网站营销手段有哪些方式
  • b2c购物网站前台代码买链接网
  • 梅州做网站需要多少钱怎样建立网站平台
  • 做卫浴软管的网站百度手机app下载并安装
  • 做特殊单页的网站sem培训
  • 拔萝卜在线视频免费观看济南做seo排名
  • 简历网站有哪些网络广告销售