mybatis学习总结
1、流程
-
创建一个maven项目
-
导入依赖
-
创建Mybatis核心配置文件
-
properties属性
-
可以引入外部配置文件
-
-
settings设置
-
-
设置全局缓存
-
设置延迟加载
-
-
typeAliases类型别名
-
通过指定包将包内类全部起别名
-
直接指定javabean为其起别名
-
-
plugins插件
-
environments(环境配置)
-
可配置多套环境,用于连接不同的数据库,SqlSessionFactory 实例只能选择一种环境
-
environment(环境变量)
-
transactionManager(事务管理器)
-
dataSource(数据源)
-
-
-
mappers映射器
-
绑定Mapper.xml文件
-
-
-
创建javaBean、接口、Mapper.xml文件
-
绑定Mapper.xml或者绑定接口
-
使用class绑定接口
-
使用resource绑定Mapper
-
2、Mapper.xml文件
-
修改基本配置,namespace中的包名要和接口中的包名相同
-
编写CRUD语句
1、SQL映射文件中的顶级元素:
-
cache
– 该命名空间的缓存配置。 -
cache-ref
– 引用其它命名空间的缓存配置。 -
resultMap
– 描述如何从数据库结果集中加载对象,是最复杂也是最强大的元素。 -
sql
– 可被其它语句引用的可重用语句块。
2、元素中的属性:
-
id
– 代码块的名 -
parameterType
– 传入的参数类型 -
resultType
– 语句返回结果的类名(返回类型,若是集合则返回集合包含的类型) -
resultMap
– 对外部 resultMap 的命名引用 -
flushCache
– 设置为true且被调用后会导致本地缓存和二级缓存被清空,默认false -
useCache
– 设置为true后会导致语句的结果被二级缓存起来,默认true
3、resultMap的使用
对于联表查询可以使用结果集映射的方式获得返回结果
-
在查询语句块中使用resultMap,引用外部的resultMap
-
外部resultMap 使用某一个javabean当做返回类型
-
在外部resultMap将数据库查询的字段映射成javabaen的属性中
-
复杂的javabaen属性可以继续使用association或collection对其进行进一步的映射
-
association符合多对一的情况
-
<resultMap id="StudentsTeacher2" type="student">
<result property="id" column="sid"/>
<result property="name" column="sname"/>
<association property="teacher" javaType="teacher">
<result property="id" column="tid"/>
<result property="name" column="tname"/>
</association>
</resultMap>
-
-
collection符合一对多的情况
-
<resultMap id="teacherStudent" type="teacher">
<id property="id" column="tid"/>
<result property="name" column="tname"/>
<collection property="students" ofType="student">
<id property="id" column="sid"/>
<result property="name" column="sname"/>
<result property="tid" column="tid"/>
</collection>
</resultMap>
-
-
javaType 用来指定实体类中的属性类型
-
ofType 用来指定映射到List或集合中的pojo类型,泛型中约束的类型
3、动态SQL
-
IF
标签 – 用于判断,成立则执行标签内内容 -
where
标签 – 用于where语句拼接的情况,可自动去掉首条件前面的and和or -
choose
标签 – 用于多个条件满足其一足以的情况-
when
标签 -
otherwise
标签
-
-
set
标签 – 用于修改语句,可以自动去掉最后一个条件的逗号 -
trim
标签 – 格式化的标记,可完成set或where标记的功能-
prefix:用来定义set元素或where元素的功能
-
prefixoverride:去掉子句第一个and或者or
-
suffixOverrides:去掉最后一个多余的逗号
-
suffix:后缀,可以这样使用suffix=” where id = #{id} “
-
-
forEach
-
collection=”ids” :集合的引用
-
item=”id” :遍历得到的元素引用
-
open=”(” :在前面加入元素
-
separator=”,” :每次遍历后元素之间的分隔符
-
close=”)” :在后面加入元素
-
4、日志
-
导入依赖
-
设置日志配置文件
-
在mybatis核心配置文件中设置日志
-
使用日志
需掌握的日志:
-
LOG4J⭐
-
<settings>
<setting name="logImpl" value="LOG4J"/>
</settings>
-
-
COMMONS_LOGGING
-
STDOUT_LOGGING⭐ 标准日志输出
-
5、注解
简单的CRUD可以使用注解实现,在接口的抽象类上使用
// 使用注解查询
注意:使用注解需要在核心配置文件中绑定接口。在复杂的功能面前注解并不适用
6、mybatis中的模糊查询
使用mysql的函数实现:
<select id="fuzzyQuery" resultType="com.bin.pojo.Book">
select * from mybatis.book where bookName like
concat('%',#{info},'%');
</select>
7、错误整理:
1、找不到mapper中的map
Could not find parameter map com.bin.dao.user.UserMapper.map
到mapper配置文件中将parameterMap改成parameterType
2、主键重复
Duplicate entry ’17’ for key ‘PRIMARY’
3、未写结果集映射
A query was run and no Result Maps were found for the Mapped Statement ‘com.bin.dao.user.UserMapper.getUserCount’. It’s likely that neither a Result Type nor a Result Map was specified
4、mapper注册失败
org.apache.ibatis.binding.BindingException: Type interface com.bin.dao.UserDao is not known to the MapperRegistry.
5、可能遇到的错误:
-
配置文件没有注册
-
绑定接口错误
-
方法名不对
-
返回类型不对
-
resource绑定mapper,需要使用路径
-
程序配置文件必须符合规范
-
NullPointerException没有注册到资源
-
输出的xml文件中存在中文乱码问题
-