标题Mybatis总结
## 标题Mybatis:是什么
是一款非常优秀的基于Java编写的封装JDBC操作的半自动化ORM持久层框架
## 标题Mybatis:作用
实现数据库的CRUD操作
## 标题Mybatis:为什么使用
原JDBC操作数据库存在的问题
数据库连接池重复创建和销毁,浪费资源 ====>数据库连接池
存在很多硬编码,数据库连接四要素,SQL语句,输入参数,解析结果====>配置文件
标题Mybatis:使用步骤
## 标题ORM + 数据库连接(SqlMapConfig.xml) + SqlSession
XML
>R:创建数据库表,创建单表,多表(一对一,一对多,多对一,多对多)
>O:创建模型类
1.private声明属性
2.类型使用包装类型
3.实现setter和getter方法
4.对一模型类属性的声明,对多模型类集合属性的声明
>M:映射文件
输入映射、SQL语句、输出映射
<mapper namespace="对应DAO接口的类全名">
<resultMap id="" type="">
<id property="" column=""/>
<result property="" column=""/>
<association property="" javaType="" column="" select="">
<!--说明select属性和下面的标签冲突,不可同时设置 -->
<id property="" column=""/>
<result property="" column=""/>
</association>
<collection property="" ofType="" column="" select="">
<!--说明select属性和下面的标签冲突,不可同时设置 -->
<id property="" column=""/>
<result property="" column=""/>
</collection>
</resultMap>
<select id="唯一标识" parameterType="传入参数类全名|别名"
resultType="返回值类全名|别名"|resultMap="外部resultMap的命名引用">
select的SQL语句
</select>
<insert id="唯一标识" parameterType="传入参数类全名|别名">
<selectKey keyProperty="模型类主键属性名"
resultType="返回主键的类型" order="AFTER|BEFORE">
获取主键的SQL:select last_insert_id()|select uuid()
<!--说明:order指定获取主键SQL语句在insert的SQL语句前/后执行 -->
</selectKey>
insert的SQL语句
</insert>
<update id="唯一标识" parameterType="传入参数类全名|别名">
update的SQL语句
</update>
<delete id="唯一标识" parameterType="传入参数类全名|别名">
delete的SQL语句
</delete>
<!--
#{}与${}区别:
1.#{}占位符,${}字符串拼接
2.如果为简单数据类型 #{任意值} ${value}
3.如果为pojo类型或pojo包装类型 #{pojo属性.属性....},${}一致
-->
<!--动态SQL -->
<if test=""></if>
<where></where> <!--替换sql中的where并将第一个出现的and去掉 -->
<set></set><!--替换sql中的set并将最后一个出现的,去掉 -->
<foreach collection="" item="" open="" close="" separator=""></foreach>
<!--
collection:遍历的集合
item:接收每次遍历结果的变量 使用${}使用
open:循环开始执行一次
close:循环结束执行一次
separator:每次循环后拼接一次
-->
<sql id=""></sql> <include refid=""/>
</mapper>
>数据库连接SqlMapConfig.xml
1.properties 引入外部的配置文件,通过${key}使用
<properties resource=""></properties>
2.settings 设置mybatis的系统配置
<!--了解,延迟加载 缓存 -->
<settings>
<setting name="" value=""/>
</settings>
3.typeAliases 为模型类起别名,方法映射文件编写
<!--别名不区分大小写 -->
<typeAliases>
<typeAlias type="" alias=""/>
<package name=""/> <!--模型类的包名-->
</typeAliases>
4.environments 数据库连接信息配置 后期交给spring管理
连接池:POOLED UNPOOLED JNDI-->了解
5.mappers 配置映射文件
<mappers>
<mapper class="DAO接口类全名"/>
<mapper resource="映射文件路径"/>
<package name="DAO接口包名"/>
<!--DAO接口要和映射文件的包结构和名称一致 -->
<mappers>
>SqlSession
Resources:加载SqlMapConfig.xml主配置文件
InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");
SqlSessionFactoryBuilder:
构建SqlSessionFactory,使用构建者模式封装工厂的创建细节
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
SqlSessionFactory:
通过builder的build(in)方法来创建,使用工厂模式降低类之间的依赖
SqlSessionFactory factory = builder.build(in);
SqlSession:mybatis和数据库交互的桥梁,可以实现CRUD
sqlSession.
selectList() selectOne() insert() update() delete()
commmit() close() clearCache()
sqlSession.getMapper(DAO接口.class);
mapper代理方式开发,满足的规范
namespace要和DAO接口的全类名保持一致
statement的ID要和DAO接口的方法名保持一致
statement的parameterType要和方法的参数保持一致
statement的resultType|resultMap要和方法的返回值保持一致。
缓存:一级缓存和二级缓存
一级缓存:sqlSession级别 单例 数据不共享
二级缓存: sqlSessionFactory级别 非单例 数据可以共享
Annotation
@Insert @Update @Delete @Select
@Results(id="",value={
@Result(id=true|false,property="",column=""),
@Result(property="",column="",one=@One(
select="",fetchType=FetchType.LAZY|EAGER
),many=@Many(
select="",fetchType=FetchType.LAZY|EAGER
)),
})
@ResultMap(value="")