MyBatis面试题系列三

1、#{}${}的区别是什么?
#{}是预编译处理,${}是字符串替换。

Mybatis 在处理#{}时,会将 sql 中的#{}替换为?号,调用 PreparedStatement 的

set 方法来赋值;

Mybatis 在处理${}时,就是把${}替换成变量的值。

使用#{}可以有效的防止 SQL注入,提高系统安全性。

2、当实体类中的属性名和表中的字段名不一样 ,怎么办 ?

第 1种: 通过在查询的sql语句中定义字段名的别名,让字段名的别名和实体类

的属性名一致。

复制代码
<select id="selectorder" parametertype="int" resultetype="
 me.gacl.domain.order">
 select order_id id,order_no orderno,order_price price from
 orders where order_id=#{id};
 </select>

第2种:通过<resultMap>来映射字段名和实体类属性名的一一对应的关系。

复制代码
<select id="getOrder" parameterType="int" 
 resultMap="orderresultmap">
 select * from orders where order_id=#{id}
 </select>
 <resultMap type="me.gacl.domain.order" id="orderresultmap">
 <!--用id属性来映射主键字段-->
 <id property="id"  column="order_id">
 <!--用result属性来映射非主键字段,property为实体类属性名,column
为数据表中的属性-->
 <result property="orderno" column="order_no"/>
 <result property="price" column="order_price"/>
 </reslutMap>

3、模糊查询like语句该怎么写?

第1种:在Java代码中添加sql通配符。

复制代码
string wildcardname="%smi%";
 list<name> names=mapper.selectlike(wildcardname);
<select id="selectlike">
 select * from foo where bar like #{value}
 </select>

第 2种:在sql语句中拼接通配符,会引起sql注入

复制代码
string wildcardname = "smi";
 list<name> names = mapper.selectlike(wildcardname);
 <select id="selectlike">
 select * from foo where bar like "%"#{value}"%"
 </select>

阿里云"99计划",新老同享,低价长效,助力开发者普惠上云!

复制代码
相关推荐
梁辰兴3 小时前
企业培训笔记:外卖平台后端--套餐管理模块--新建套餐信息
笔记·vue·mybatis·springboot·外卖管理系统
信仰_2739932438 小时前
Mybatis-Spring重要组件介绍
java·spring·mybatis
通往曙光的路上16 小时前
接口练习哈哈
mybatis
通往曙光的路上16 小时前
day16_接口加强练习
mybatis
提笔了无痕1 天前
什么是Redis的缓存问题,以及如何解决
数据库·redis·后端·缓存·mybatis
vx Biye_Design2 天前
servlet宠物医院管理系统-计算机毕业设计源码77418
java·vue.js·spring·servlet·eclipse·mybatis
梁辰兴2 天前
企业培训笔记:外卖平台后端--套餐管理模块--回显套餐信息
笔记·vue·mybatis·springboot·外卖管理系统
kkkkk0211062 天前
微服务学习笔记(黑马商城)
java·spring boot·spring·spring cloud·sentinel·mybatis·java-rabbitmq
夜幽青玄3 天前
mybatis-plus调用报 org.springframework.dao.DataIntegrityViolationException 错误处理
开发语言·python·mybatis
毕业设计制作和分享3 天前
springboot150基于springboot的贸易行业crm系统
java·vue.js·spring boot·后端·毕业设计·mybatis