首页  

mybatis知识点     所属分类 mybatis 浏览量 1263
#{}和${}的区别
#{} 预编译处理,${} 字符串替换。
#{}可以有效的防止SQL注入

like 写法

String name = "%xxx%";
select * from t_user where name like #{name}

name="xxx";
select * from t_user where name  LIKE CONCAT('%',#{name},'%'))

in的写法

Select * from news where 
id in<foreach collection="ids" item="item" open="("separatosr="," close=")">#{ids} </foreach>


sql xml 标签 
select|insert|updae|delete
动态sql标签
trim|where|set|foreach|if|choose|when|otherwise|bind
使用OGNL从sql参数对象中计算表达式的值,根据表达式的值动态拼接sql


insert
useGeneratedKeys="true" keyProperty="id" 
通过 xxxDO.getId() 获取自动生成的id 



Xml + dao接口
namespace + sqlId
Map< String, MappedStatement>
不支持重载
使用JDK动态代理为Dao接口生成proxy对象,代理对象proxy会拦截接口方法,执行MappedStatement所代表的sql,然后将sql执行结果返回。

Mapper接口方法名和mapper.xml中定义的每个sql的id相同;
Mapper接口方法的输入参数类型和mapper.xml中定义的每个sql 的parameterType的类型相同;
Mapper接口方法的输出参数类型和mapper.xml中定义的每个sql的resultType的类型相同;
Mapper.xml文件中的namespace即是mapper接口的类路径。


Mybatis分页
Mybatis使用RowBounds进行分页,对ResultSet执行内存分页(假分页,非物理分页)
分页插件原理
使用Mybatis提供的插件接口,实现自定义插件,在插件的拦截方法内拦截待执行的sql,
然后重写sql,根据dialect方言,添加对应的物理分页语句和物理分页参数。

select * from student
拦截sql后重写为
select t.* from (select * from student)t limit 0,10

Mybatis插件运行原理

可以编写针对 ParameterHandler(参数处理程序) ResultSetHandler(结果集处理) StatementHandler Executor(执行)这4种接口的插件


sql执行结果封装

sql列别名
resultMap

https://gitee.com/dyyx/springboothello/blob/master/src/main/resources/mapper/UserMapper.xml

sql别名忽略大小写
T_NAME AS name
T_NAME AS NaMe 



三种基本的Executor执行器
SimpleExecutor ReuseExecutor BatchExecutor
SimpleExecutor
每执行一次update或select,就开启一个Statement对象,用完立刻关闭Statement对象。
ReuseExecutor
执行update或select,以sql作为key查找Statement对象,存在就使用,不存在就创建,
用完后,不关闭Statement对象,而是放置于Map< String, Statement>内,供下一次使用 
BatchExecutor
执行update(没有select,JDBC批处理不支持select),
将所有sql都添加到批处理中 addBatch ,等待统一执行 executeBatch 
它缓存了多个Statement对象,每个Statement对象都是addBatch()完毕后,等待逐一执行executeBatch()批处理。
与JDBC批处理相同。


MyBatis缓存
一级缓存 二级缓存
一级缓存 与 session关联 默认开启
二级缓存 作用域为 Mapper(Namespace) 可自定义缓存组件 譬如 Ehcache  默认关闭
使用二级缓存对象类需要实现Serializable序列化接口



Mybatis 仅支持 association 关联对象 和 collection 关联集合对象的延迟加载,
Mybatis 配置文 配置是否启用延迟加载 lazyLoadingEnabled=true|false
使用动态代码实现延迟加载 

9种动态sql标签 trim|where|set|foreach|if|choose|when|otherwise|bind
使用OGNL从sql参数对象中计算表达式的值,根据表达式的值动态拼接sql


参数传递
public User selectUser(String name, int deptId);
select * from user where user_name = #{0} and dept_id = #{1}

public User selectUser(@Param("userName") String name, int @Param("deptId") deptId);
select * from user where user_name = #{userName} and dept_id = #{deptId}
使用 map或javabean

上一篇     下一篇
关于如何挣钱的35条建议

代理的几种实现方式

RPC原理简介

dubbo功能模块介绍

网站架构演变过程

redis消息发布与订阅