理解:一个特定问题的解决方案,底层实现了重复的功能代码,我们在这个框架的基础上,遵守框架的使用规范,添加自定义代码,完成自己的功能。
框架=jar+API文档+示例代码+源码

1.MyBatis框架

作用:数据持久层框架,就是封装JDBC
工作流程:

  • 通过Reader对象读取Mybatis映射主配置文件
  • 通过SqlSessionFactoryBuilder对象创建SqlSessionFactory对象
  • 事务默认开启
  • 通过SQLSession读取映射文件中的操作编号,从而读取SQL语句
  • 提交事务
  • 关闭资源

    1.1框架搭建

    jar包下载:https://qingzheng.lanzous.com/b01bkd0gh
    jar包结构图:
    image.png
    image.png
    框架使用日志来实现输出,不同框架使用的日志技术不同:
    日志门面: 接口
    日志实现:
    log4j MyBatis和Spring
    log4j2 hibernate
    logback SpringBoot
    主配置文件mybatis.xml:放在项目src下面
    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <!DOCTYPE configuration
    3. PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
    4. "http://mybatis.org/dtd/mybatis-3-config.dtd">//可在eclipse的widow->perferences->XML Catalog界面配置本地dtd文件
    5. <!-- 主配置文件名字可以任意,有意义,位置在src目录下 -->
    6. <configuration>
    7. <!-- jdbc的四大参数配置文件 -->
    8. <properties resource="jdbc.properties"/>
    9. <!-- 别名设置 -->
    10. <typeAliases>
    11. <!--typeAlias、package选其一-->
    12. <!-- 每个实体类都要配置,此处实体类为Student -->
    13. <typeAlias type="com.woniuxy.entity.Student" alias="Student"/>
    14. <!--或者可以设置检测包下的所有实体对象-->
    15. <package name="com.woniuxy.entity"/>
    16. </typeAliases>
    17. <!-- 运行的基本环境配置 -->
    18. <environments default="development">
    19. <environment id="development">
    20. <!-- 事务管理器,默认为JDBC的事务管理 -->
    21. <transactionManager type="JDBC"/>
    22. <!-- 数据源type配置连接池,默认使用内置连接池 -->
    23. <dataSource type="POOLED">
    24. <!-- JDBC的四大参数 -->
    25. <property name="driver" value="${jdbc.driver}"/>
    26. <property name="url" value="${jdbc.url}"/>
    27. <property name="username" value="${jdbc.username}"/>
    28. <property name="password" value="${jdbc.password}"/>
    29. </dataSource>
    30. </environment>
    31. </environments>
    32. <mappers>
    33. <!-- 指定映射文件所在包,名字必须和接口一样,包括大小写 -->
    34. <package name="com.woniuxy.dao"/>
    35. </mappers>
    36. </configuration>
    jdbc.properties文件:放在项目src下
    image.png
    StudentDao.xml映射文件(insert为例):
    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <!-- 根节点就是mapper -->
    3. <!DOCTYPE mapper
    4. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    5. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">//和主配置文件中的设置一样
    6. <!--
    7. Mapper动态代理:框架自动生成Dao接口实现类
    8. 1.映射文件放在dao接口的包下
    9. 2.namespace必须是对应接口的全名
    10. 3.sql语句的id要和对应的方法名一样
    11. -->
    12. <!-- 每个Dao有个映射文件 -->
    13. <mapper namespace="com.woniuxy.dao.StudentDao">//此处的名称必须和对应的Dao接口名相同
    14. <!-- parameterType使用方法参数的全类名,此处在主配置文件里面里设置了别名 -->
    15. <insert id="insertStudent" parameterType="Student">//sql语句的id必须和Dao接口中对应方法名相同
    16. insert into student values(null,#{name},#{age})
    17. </insert>
    18. </mapper>
    MyBatisUtil(单例,用来创建sqlSession对象) ```java package com.woniuxy.util;

import java.io.IOException; import java.io.InputStream;

import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class MyBatisUtil { private static SqlSessionFactory sqlSessionFactor = null;

  1. // 禁止外界通过new方法创建,实现单例
  2. private MyBatisUtil() {
  3. }
  4. public static SqlSession getSqlSession() {
  5. SqlSession sqlSession = null;
  6. try {
  7. if (sqlSessionFactor == null) {
  8. // 加载主配置文件
  9. InputStream inputStream = Resources.getResourceAsStream("mybatis.xml");
  10. // 创建sqlSessionFactor
  11. sqlSessionFactor = new SqlSessionFactoryBuilder().build(inputStream);
  12. }
  13. // 创建sqlSession对象
  14. sqlSession = sqlSessionFactor.openSession();
  15. } catch (IOException e) {
  16. e.printStackTrace();
  17. }
  18. return sqlSession;
  19. }

}

  1. <a name="Iy47q"></a>
  2. ## 1.2日志输出
  3. MyBatais使用的是log4j<br />添加日志的配置文件log4j.properties,名字不能改,位置必须在src下<br />日志输出级别<br />ALL>TRACE>DEBUG>INFO>WARM>ERROR>FATAL>NONE<br />log4j.properties:<br />![image.png](https://cdn.nlark.com/yuque/0/2020/png/1335005/1594812728934-07589a41-bdbe-4243-9a22-7f5fe46ac0a0.png#height=209&id=fNsdD&margin=%5Bobject%20Object%5D&name=image.png&originHeight=209&originWidth=982&originalType=binary&ratio=1&size=33221&status=done&style=none&width=982)
  4. <a name="3ESDA"></a>
  5. ## 1.3查询
  6. 本次实验是学生表和班级表的查询<br />表结构如图:<br />![image.png](https://cdn.nlark.com/yuque/0/2020/png/1335005/1595055497084-6819512b-7147-47cf-b20a-d346bb694ae2.png#height=133&id=NzQcC&margin=%5Bobject%20Object%5D&name=image.png&originHeight=133&originWidth=297&originalType=binary&ratio=1&size=6203&status=done&style=none&width=297)![image.png](https://cdn.nlark.com/yuque/0/2020/png/1335005/1595055542217-283114ea-1fbd-428f-b5e0-f637d2a12173.png#height=155&id=DQuzp&margin=%5Bobject%20Object%5D&name=image.png&originHeight=155&originWidth=439&originalType=binary&ratio=1&size=10989&status=done&style=none&width=439)<br />实体类:<br />![image.png](https://cdn.nlark.com/yuque/0/2020/png/1335005/1595055884749-ed4f65c8-cf0b-4282-9986-3be4ca8e3848.png#height=191&id=XuXit&margin=%5Bobject%20Object%5D&name=image.png&originHeight=191&originWidth=285&originalType=binary&ratio=1&size=9174&status=done&style=none&width=285)![image.png](https://cdn.nlark.com/yuque/0/2020/png/1335005/1595055895977-8b4f240f-52df-42c4-8ffe-506cc19d33d6.png#height=119&id=Jn9FG&margin=%5Bobject%20Object%5D&name=image.png&originHeight=119&originWidth=325&originalType=binary&ratio=1&size=7062&status=done&style=none&width=325)
  7. <a name="dkJx7"></a>
  8. ### 一对多查询(collection)
  9. ```xml
  10. <?xml version="1.0" encoding="UTF-8"?>
  11. <!-- 根节点就是mapper -->
  12. <!DOCTYPE mapper
  13. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  14. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  15. <!-- Mapper动态代理:框架自动生成Dao接口实现类
  16. 1.映射文件放在dao接口的包下
  17. 2.namespace必须是对应接口的全名
  18. 3.sql语句的id要和对应的方法名一样 -->
  19. <mapper namespace="com.woniuxy.dao.ClassDao">
  20. <!-- parameterType使用方法参数的全类名,此处在主配置文件里面里设置了别名 -->
  21. <!-- 连接查询的一对多 -->
  22. <!--id对应方法名,resultMap查询结果是cid编号班级下的所有学生信息,无法直接赋值-->
  23. <select id="selectClassById" resultMap="ClassMapper">
  24. select * from student stu,class cl where stu.cid=cl.cid and cl.cid=#{cid}
  25. </select>
  26. <!-- type返回类型,id对应resultMap -->
  27. <resultMap type="Class" id="ClassMapper">
  28. <id column="cid" property="cid" />
  29. <result column="cname" property="cname" />
  30. <!-- 一对多用collection -->
  31. <!--property是对象的属性,ofType是查询结果的返回类型,column对应数据库中的列属性-->
  32. <collection property="students" ofType="Student">
  33. <id column="sid" property="sid" />
  34. <result column="sname" property="sname" />
  35. <result column="sage" property="sage" />
  36. </collection>
  37. </resultMap>
  38. <!-- 分步查询的一对多 -->
  39. <select id="selectClassById" resultMap="classMapper">
  40. select * from class where cid=#{cid}
  41. </select>
  42. <resultMap type="Class" id="classMapper">
  43. <id column="cid" property="cid" />
  44. <result column="cname" property="cname" />
  45. <!-- 一对多 -->
  46. <!-- column中的cid在前一次的查询结果中要出现,并且作为参数传递到下一次查询中作为查询条件 -->
  47. <collection property="students" ofType="Student" select="selectStudentById" column="cid" />
  48. </resultMap>
  49. <select id="selectStudentById" resultType="Student">
  50. select * from student where cid=#{cid}
  51. </select>
  52. </mapper>

1.4一对一查询(association)

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- 根节点就是mapper -->
  3. <!DOCTYPE mapper
  4. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  5. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  6. <!-- Mapper动态代理:框架自动生成Dao接口实现类
  7. 1.映射文件放在dao接口的包下
  8. 2.namespace必须是对应接口的全名
  9. 3.sql语句的id要和对应的方法名一样 -->
  10. <mapper namespace="com.woniuxy.dao.ClassDao">
  11. <!-- parameterType使用方法参数的全类名,此处在主配置文件里面里设置了别名 -->
  12. <!-- 连接查询的一对一 -->
  13. <!--id对应方法名,resultMap查询结果是cid编号班级下的所有学生信息,无法直接赋值-->
  14. <select id="selectStudentById" resultMap="studentMapper">
  15. select * from student stu,class cl where stu.cid=cl.cid and stu.id=#{id}
  16. </select>
  17. <!-- type返回类型,id对应resultMap -->
  18. <resultMap type="Student" id="studentMapper">
  19. <id column="id" property="id" />
  20. <result column="sname" property="sname" />
  21. <!-- 一对一用association -->
  22. <!--property是对象的属性,javaType是查询结果的返回类型,column对应数据库中的列属性-->
  23. <association property="class" javaType="Class">
  24. <id column="id" property="id" />
  25. <result column="cname" property="cname" />
  26. </association>
  27. </resultMap>

动态sql

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- 根节点就是mapper -->
  3. <!DOCTYPE mapper
  4. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  5. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  6. <!-- Mapper动态代理:框架自动生成Dao接口实现类
  7. 1.映射文件放在dao接口的包下
  8. 2.namespace必须是对应接口的全名
  9. 3.sql语句的id要和对应的方法名一样 -->
  10. <mapper namespace="com.woniuxy.dao.StudentDao">
  11. <!-- parameterType使用方法参数的全类名,此处在主配置文件里面里设置了别名 -->
  12. <insert id="insertStudent" parameterType="Student">
  13. insert into student values(null,#{name},#{age})
  14. </insert>
  15. <update id="updateStudent" parameterType="Student">
  16. update student set name=#{name},age=#{age} where id=#{id}
  17. </update>
  18. <delete id="deleteStudentById">
  19. delete from student where id=#{id}
  20. </delete>
  21. <select id="selectStudentById" resultType="Student">
  22. select * from student where id=#{id}
  23. </select>
  24. <select id="selectAllStudents" resultType="Student">
  25. select * from student
  26. </select>
  27. <select id="selectStudentByName" resultType="Student">
  28. select * from student where name like '%' #{nam} '%'
  29. </select>
  30. <!-- 动态sql查询 -->
  31. <select id="selectStudentByIf" resultType="Student">
  32. <include refid="mysql"></include>
  33. <where>
  34. <if test="name!=null and name!=''">
  35. and name like '%' #{name} '%'
  36. </if>
  37. <if test="age>0">
  38. and age>#{age}
  39. </if>
  40. </where>
  41. </select>
  42. <!-- sql:select * from student where id in (?,?,?) -->
  43. <select id="selectStudentByForeachArray" resultType="Student">
  44. <include refid="mysql"></include>
  45. <where>
  46. id in
  47. <foreach collection="array" item="id" open="(" close=")" separator=",">
  48. #{id}
  49. </foreach>
  50. </where>
  51. </select>
  52. <select id="selectStudentByChoose" resultType="Student">
  53. <include refid="mysql"></include>
  54. <where>
  55. <choose>
  56. <when test="name!=null and name!=''">
  57. name like '%' #{name} '%'
  58. </when>
  59. <when test="age>0">
  60. age>#{age}
  61. </when>
  62. </choose>
  63. </where>
  64. </select>
  65. <!--重复sql片段-->
  66. <sql id="mysql">
  67. select * from student
  68. </sql>
  69. </mapper>

1.5延迟加载

使用延迟加载的前提:多表关联查询使用分步。
直接加载:多条语句一起执行。

image.png
在Mybatis的文件中配置全局延迟加载在configuration标签里面,注意标签是有顺序的
image.png
侵入式延迟,深度延迟的区别:
侵入式延迟加载执行查询时只执行第一条sql语句,但是只要你访问了查询对象的属性(无论什么属性)都会执行关联查询。而深度延迟加载只有当你访问了查询对象除第一次查询以外的属性时才会执行关联查询。

缓存(了解)

基本原理:查询数据,先判断缓存(内存,硬盘)中是否存在,缓存中没有,就查询数据库,存入缓存,缓存中有,就读取缓存数据。
作用:减小数据库的压力

一级缓存

又叫SqlSession缓存,默认开启。SqlSession关闭,缓存中的数据就没有了。
注意:只要执行了增删改的操作,sqlSession中的缓存将被清空,无论是否提交。
原理:
image.png
第一次发出一个查询sql,sql查询结果写入sqlsession的一级缓存中,缓存使用的数据结构是一个map
• key:hashcode+sql+sql输入参数+输出参数(sql的唯一标识)
• value:用户信息
同一个sqlsession再次发出相同的sql,就从缓存中取不走数据库。如果两次中间出现commit操作(修改、添加、删除),本sqlsession中的一级缓存区域全部清空,下次再去缓存中查询不到所以要从数据库查询,从数据库查询到再写入缓存。

二级缓存

和SQLSession无关,SqlSession关闭也可以使用。
注意:只要执行了增删改的操作,二级缓存一样会被清空。
原理:
image.png
二级缓存的范围是mapper级别(mapper同一个命名空间),mapper以命名空间为单位创建缓存数据结构,结构是map
主配置文件设置:
image.png
映射文件配置:
image.png
实体类需实现序列化:
image.png

注解开发(了解)

框架开发一般都支持配置开发和注解开发
配置
Servlet2.5
优点:独立的配置文件,修改配置后只需重启服务器,不需要打包和部署项目。
注解
Servlet3.0
优点:简单方便
MyBatis注解功能弱,官方不推荐。
实现步骤:
删除映射文件,直接在接口中抽象方法前使用对应注解。
image.png

逆向工程

开源项目,自动生成MyBatis单表的Dao接口和映射文件
java工程附件下载:
generatorSqlmapCustom.zip
修改附件中的xml文件:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE generatorConfiguration
  3. PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
  4. "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
  5. <generatorConfiguration>
  6. <context id="testTables" targetRuntime="MyBatis3">
  7. <commentGenerator>
  8. <!-- 是否去除自动生成的注释 true:是 : false:否 -->
  9. <property name="suppressAllComments" value="true" />
  10. </commentGenerator>
  11. <!--数据库连接的信息:驱动类、连接地址、用户名、密码,此处需自己更改 -->
  12. <jdbcConnection driverClass="com.mysql.jdbc.Driver"
  13. connectionURL="jdbc:mysql://localhost:3306/mybatisdb" userId="root"
  14. password="123">
  15. </jdbcConnection>
  16. <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和
  17. NUMERIC 类型解析为java.math.BigDecimal -->
  18. <javaTypeResolver>
  19. <property name="forceBigDecimals" value="false" />
  20. </javaTypeResolver>
  21. <!-- targetProject:生成POJO类的位置 -->
  22. <javaModelGenerator targetPackage="com.woniuxy.entity"
  23. targetProject=".\src">
  24. <!-- enableSubPackages:是否让schema作为包的后缀 -->
  25. <property name="enableSubPackages" value="false" />
  26. <!-- 从数据库返回的值被清理前后的空格 -->
  27. <property name="trimStrings" value="true" />
  28. </javaModelGenerator>
  29. <!-- targetProject:mapper映射文件生成的位置 -->
  30. <sqlMapGenerator targetPackage="com.woniuxy.mapper"
  31. targetProject=".\src">
  32. <!-- enableSubPackages:是否让schema作为包的后缀 -->
  33. <property name="enableSubPackages" value="false" />
  34. </sqlMapGenerator>
  35. <!-- targetPackage:mapper接口生成的位置 -->
  36. <javaClientGenerator type="XMLMAPPER"
  37. targetPackage="com.woniuxy.mapper"
  38. targetProject=".\src">
  39. <!-- enableSubPackages:是否让schema作为包的后缀 -->
  40. <property name="enableSubPackages" value="false" />
  41. </javaClientGenerator>
  42. <!-- 指定数据库表,此处需自己更改 -->
  43. <table schema="student" tableName="student"></table>
  44. <table schema="course" tableName="course"></table>
  45. <table schema="dep" tableName="dep"></table>
  46. <table schema="emp" tableName="emp"></table>
  47. <table schema="score" tableName="score"></table>
  48. <table schema="stu" tableName="stu"></table>
  49. </context>
  50. </generatorConfiguration>

测试:
image.png

分页

逆向工程more不支持分页,可以使用PageHleper
分页插件jar下载:https://qingzheng.lanzous.com/b01bkqtdg
My Batis主配置文件添加插件配置
image.png
测试:
image.png

mybatis的核心类

MyBatis - 图15
MyBatis - 图16