一、简介
- 使用过程
添加依赖
添加配置文件
添加Sql mapper 文件
通过mybatis-config得到sqlSessionFactory
使用sqlSession操作要执行的sql语句
非线程安全,使用后关闭
- 全局配置文件mybatis-config.xml
<properties> :数据库信息
<settings> :全局设置 cacheEnabled一般设为false不适用缓存
<typeAliases> :为包下的类设置一个别名(使用之后在mapper里不用写完全限定名)
<enviroments>:数据库连接配置
<mapper> :关联需要执行的sql语句
- sqlmapper.xml
Namespace :对应接口
Id :对应接口类方法名
ParemeterType:参数类型
ResultType:返回类型
UseGeneratedKeys:是否自动添加主键
keyProperty:主键名
resultMap:定义对应关系。Id 为名称 type为对象类
使用注解@Param进行参数绑定
使用Map进行参数绑定
<sql>标签可以写sql语句,然后在其他变迁中通过<include refid=>进行使用
二、Mybatis spring
Spring-mybatis配置文件
<bean id=”sqlSessionFactory” class=””>
<property name=””>数据源
<>mybatis配置
<>mapper配置
三、动态sql
<where>去掉and
<set>去掉,
<if>
<for each>
四、Typehandler
自定义方法:
1.实现public interface TypeHandler<T>
2.或者继承BaseTypeHandler<T>
<typeHandler>配置处理器
五、Plugins拦截器
自定义方法:
实现接口public interface Interceptor
指定需要拦截的方法@Intercepts
六、Meerkat-dao
枚举类型的typeHandler
Code和codeOf方法
Plugin分页插件
Json的typeHandler