1.特殊字符查询
表达式:name like '%'||#{name}||'%' escape '\' ( 在mysql中||代表是or的意思,escape表示在、后面的字符不是转义字符,而是普通字符,因为%、_等特殊符号在数据库中出现会被认为是转移字符,要想在数据库中查询%这个特殊字符 ) Mybatis的模糊查询
2.返回类型
查询结果为List (resulrType=“泛型中的类型“,也可用resultMap)
返回结果为Map,返回一条map和多条map(resulrType=map)
<collection> </collection> 一对多关联映射
<association> </association> 一对一关联映射
3.二级缓存
多个sqlsession共享
4.转义符
符号 | 转义 | |
< | < | <![CDATA[ >= ]]> |
> | > | <![CDATA[ <= ]]> |
5.批量操作
<insert id="insert" parameterType="java.util.List">
insert into myTable(
id,
name,
account
)
<foreach collection="list" item="item" index="index" separator="union">
select
#{item.id},
#{item.name},
#{item.account},
from dual
</foreach>
</insert>
支持forech标签是因为mysql支持批量提交
insert into myTable (id,column1) VALUES(1,'A'),(2,'B'),(3,'C');
6.动态查询
类似switch语句,配合when、otherwise使用
<select id="methodName" parameterType="java.lang.String" resultType="string">
select *
from mybable
where 1 = 1
<choose>
<when test="title != null">
and title = #{title}
</when>
<when test="content != null">
and content = #{content}
</when>
<otherwise>
and owner = "owner1"
</otherwise>
</choose>
</select>
7.trim标签的使用
8.Mybatis如何进行分页
9.分页插件的原理
10.接口绑定
在方法上面加上@Select这样的注解;或者是通过xml方式绑定sql
11.sql中区分不同的数据库
<if test="_databaseId == 'MySQL' or _databaseId == 'Oracle'">
sql语句
</if>
_databaseId :数据库的别名
<if test="_parameter!=null">
sql语句
</if>
_parameter:代表整个参数