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计划",新老同享,低价长效,助力开发者普惠上云!

复制代码
相关推荐
多多*12 小时前
一个有 IP 的服务端监听了某个端口,那么他的 TCP 最大链接数是多少
java·开发语言·网络·网络协议·tcp/ip·缓存·mybatis
book多得1 天前
Redis 大 Key 问题:识别、危害与最优处理方案
java·redis·mybatis
q***18061 天前
十八,Spring Boot 整合 MyBatis-Plus 的详细配置
spring boot·后端·mybatis
⑩-1 天前
苍穹外卖Day(1)
java·数据库·spring boot·spring·java-ee·mybatis
朝新_1 天前
【统一功能处理】从入门到源码:拦截器学习指南(含适配器模式深度解读)
数据库·后端·mybatis·适配器模式·javaee
爱吃土豆的马铃薯ㅤㅤㅤㅤㅤㅤㅤㅤㅤ1 天前
MyBatis Plus中执行原生SQL语句方法
python·sql·mybatis
R.lin2 天前
MyBatis 专题深度细化解析
oracle·面试·mybatis
JH30732 天前
MyBatis多表联查返回List仅一条数据?主键冲突BUG排查与解决
bug·mybatis
小坏讲微服务2 天前
Spring Boot整合Redis注解,实战Redis注解使用
spring boot·redis·分布式·后端·spring cloud·微服务·mybatis
xie_pin_an2 天前
MyBatis-Plus 实战:MPJLambdaWrapper 多表联查用法全解析
java·spring boot·spring·mybatis