ORM持久层框架——对数据持久化进行操作
u减少了61%的代码量
u最简单的持久化框架
u架构级性能增强
uSQL代码从程序代码中彻底分离,可重用
u增强了项目中的分工
u增强了移植性
mybatis的框架搭建:
(1) 引入依赖
(2) 创建核心配置文件
(3) 创建映射配置文件
(4) API
===================
1. 三个核心类
(1) SqlSessionFactoryBuilder:用过即丢,目的是为了创建SqlSessionFactory实例
(2) SqlSessionFactory:这是一个单例,可以通过配置对象或者XML来进行创建;MyBatis最核心最重要的功能,大多都集中在SqlSessionFactory
(3) SqlSession:实际操作SQL的核心类,不是一个线程安全的对象,所以一般用完以后要重新获取。在关闭连接之前,我们可以使用sqlSession完成多次数据库的执行任务。
=====================
1. 核心配置文件的配置
(1) 配置properties
(2) 配置数据源:
① transactionManager:配置事务管理器
② dataSource:配置连接池
(3) Mapper标签:
① 配置Mapper映射的路径
==========================
【1】配置pom文件:mybatismysql-connector-java
【2】配置mysql-config.xml
-读取properties <properties resource="jdbc.properties"/>
-配置数据源
-映射mapper配置
【3】jdbc.properties
-
【4】Mapper.xml配置【映射器】
-Sql映射语句
-动态sql
映射器配置&
查询语句:
<select id="selectDept" resultType="Dept">
select * from dept
</select><select id="selectDept1" resultType="Dept" parameterType="int">
select * from dept where deptno = #{dno}
</select><select id="selectDept2" resultType="Dept" parameterType="Dept">
select * from dept where deptno = #{deptno}
</select><select id="selectDept3" resultType="Dept" parameterType="Dept">
select * from dept where deptno = #{deptno} and dname = #{dname}
</select>
===============================where语句动态
<select id="selectDeptIn" resultType="Dept" parameterType="Dept">
select * from dept
<trim prefix="where" prefixOverrides="and | or">
<if test="deptnos != null">
and deptno in
<foreach collection="deptnos" item="deptno" open="(" close=")" separator=",">
#{deptno}
</foreach>
</if>
</trim>
</select>
=====================where条件拼接=========================
<select id="selectDeptDynamic" resultType="Dept" parameterType="Dept">
select * from dept where 1=1
<if test="deptno != null">
and deptno = #{deptno}
</if>
<if test="dname != null">
and dname like concat('%',#{dname},'%')
</if>
</select>
================= when只执行一条,switch功能类似 ===================
<select id="selectDeptDynamic2" resultType="Dept" parameterType="Dept">
select * from dept where 1=1
<choose>
<when test="deptno != null">
and deptno = #{deptno}
</when>
<when test="dname != null">
and dname like concat('%',#{dname},'%')
</when>
<otherwise>
and loc like concat('%',#{loc},'%')
</otherwise>
</choose>
</select><select id="selectDeptDynamic3" resultType="Dept" parameterType="Dept">
select * from dept
<where>
<if test="deptno != null">
deptno = #{deptno}
</if>
<if test="dname != null">
and dname like concat('%',#{dname},'%')
</if>
<if test="loc != null">
and loc like concat('%',#{loc},'%')
</if>
</where>
</select>
insertupdatedelete
<insert id="insertDept1" parameterType="Dept">
insert into dept values(#{deptno},#{dname},#{loc})
</insert><insert id="insertDept2" parameterType="Dept">
insert into dept values
<foreach collection="list" separator="," item="dept">
(#{dept.deptno},#{dept.dname},#{dept.loc})
</foreach>
</insert><update id="updateDept1" parameterType="Dept">
update dept set dname=#{dname},loc=#{loc} where deptno=#{deptno}
</update><update id="updateDept2" parameterType="Dept">
update dept
<set>
<if test="dname != null">
dname=#{dname},
</if>
<if test="loc != null">
loc=#{loc},
</if>
<if test="dname != null">
dname=#{dname},
</if>
</set>
<where>
<if test="deptno != null">
deptno=#{deptno}
</if>
</where>
</update><update id="updateDept3" parameterType="Dept">
update dept
<trim prefix="set" suffixOverrides=",">
<if test="dname != null">
dname=#{dname},
</if>
<if test="loc != null">
loc=#{loc},
</if>
<if test="dname != null">
dname=#{dname},
</if>
</trim>
<trim prefix="where" prefixOverrides="and | or">
<if test="deptno != null">
and deptno=#{deptno}
</if>
</trim>
</update>
<delete id="deleteDept1" parameterType="Dept">
delete from dept where deptno = #{deptno}
</delete>
java调用
-sqlsession:默认手动提交
String resource = "mysql-config.xml"; //config配置文件、jdbc放在Java资源文件的根目录
InputStream is = Resources.getResourceAsStream(resource);
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is) ;
SqlSession sqlSession = factory.openSession(); //openSession()-传true,则为自动提交
Mybatis的一二级缓存:
(1) 一级缓存(会话级别的缓存):映射文件当中开辟使用一级缓存,默认情况下,一级缓存是默认开启的;一级缓存是相对于同一个SqlSession而言的。在参数和sql完全一样的情况下,而且是在同一个SqlSession当中,这个时候,只会执行一次sql查询。第一次查完之后,mybatis会把查询结果发在缓存当中;他是使用一个map来保存查询结果,map匹配上,那么就不会再执行sql查询。
1) 一级缓存的生命周期:
a. 在一个sqlSession会话过程当中,会话关闭,则生命周期结束。
b. ClearCache:释放缓存数据,但是sqlSession依然存在
c. Close:关闭连接,释放sqlSession资源。
d. sqlSession当中只要执行了update,delete,insert,都会清空缓存
2) sqlSession是针对于一个用户的一次会话,如果有其他用户也在操作相同的数据,那么缓存中的数据就会产生差异,无法保证数据的一致性
(2) 二级缓存(项目级别的缓存)
1) 在SqlSessionFactory当中开辟缓存空间
2) 默认情况下,二级缓存是不开辟的。要求所有缓存的entity必须是可序列化模式
3) 在核心配置文件的settings当中配置全局二级缓存
4) 所有的select语句都会被缓存
5) 只要是执行了dml,update,delete,insert,都会清空缓存
6) 默认情况下的回收策略就是LRU
7) 用size指定可以保存多少查询结果
8) 所有的查询,缓存被视为read/writer的对象分开的缓存,检索的对象不是共享的。原则上来讲,这个是一个线程安全的缓存。
应用总结:
Note
-SqlSession对应着一次数据库会话,它的生命周期不是永久的,在每次访问数据库时都需要创建它
-每个线程都有自己的 SqlSession 实例,SqlSession 实例是不能被共享,也不是线程安全的