学习Mybatis的入门总结

 

1、什么是Mybatis?

  Mybatis是一个持久化的框架

  功能:

  主要实现封装连接数据库,实现动态SQL

  核心思想:

  通过ORM将瞬时性数据转变成持久化数据

什么是ORM?

  对象关系映射,直接建立在对象与数据库中的表中每个字段进行一一对应的映射关系

俩个核心配置文件 !

  Mybatis核心配置 文件 / SQL映射文件

2、Mybatis核心接口和类

  1. SqlSessionFactoryBuilder

    • SqlSessionFactoryBuilder是通过xml配置文件或者configuration类的实例去构造这个对象。然后调用build()方法去构造SqlSessionFactory对象。

    • 生命周期只存在方法体类,用完即失

  2. sqlSessionFactory

    • SqlSessionFactory是我们MyBatis整个应用程序的中心;整个程序都是以SqlSessionFactory的实例为核心的。

    • 使用SqlSessionFactory对象的openSession()方法来获取SqlSession对象,有了SqlSession对象,我们就可以去进行数据库操作了,因为SqlSession里面包含了以数据库为背景所有执行sql操作的方法。

    • 在使用openSession()方法创建一个SqlSession对象的时候传了一个布尔型的参数 "TRUE" 可以开启事务自动提交

  3. sqlSession

    • 在SqlSession里可以执行多次SQL语句,但一旦关闭了SqlSession就需要重新创建。

    • SqlSession示例不能被共享,也不是线程安全的。所以其最佳作用域是reqeust作用域内或者方法体内的作用域内。

 

3、Mybatis环境的搭建与CRUD  

 

  1. 创建一个Java项目

  2. 配置一个web环境

  3. 在web环境下建立一个依赖,导入Mybatis所需要的jia包

    mybatis-3.2.2.jar mybatis-3.2.2-sources.jar mysql-connector-java-5.1.0-bin.jar

  4. 在项目目录下建立一个resources包,并设置成资源配置包

  5. 核心配置文件:配置Mybatis配置文件,主要是封装数据库连接

  6. SQL映射文件:在根目录下建立com下建立Dao层,dao层里面配置xxxMapper.xml文件,此文件是Mybaits第二个核心配置文件主要针对于动态SQL的配置,namespace要与接口绑定。拿到这个接口的类文件便可以利用多态的思想调用接口绑定的配置文件中的方法

  7. 该配置文件中涉及到结果集的处理:当数据库表字段与实体类里的属性写法不一致时需要对其进行处理。此时用到<resultMap>,想实现连表查询,还需要用到<association>对齐进行处理。(可以将这种处理结果的方式看成数据库表给属性赋值的过程)

  8. 封装Mybatis核心接口与类(不用每次都创建读核心配置文件和创建工厂。)

  9. 进行测试

  10. Mybatis核心配置文件:

  SQL映射文件:

  

 

 

 

 

4、resultMap与SQL

  1. resultMap :处理结果集 (处理别的表字段)

    <!--1对多关系连表查询-->
    <resultMap id="getResult" type="Result">
       <result property="studentResult" column="StudentResult"></result>
    </resultMap>
    <resultMap id="selectStudentMap" type="Student">
       <result property="studentNo" column="StudentNo"></result>
       <result property="loginPwd" column="LoginPwd"></result>
       <result property="studentName" column="StudentName"></result>
       <association property="resultList" resultMap="getResult"></association>
    </resultMap>
    <select id="selectStudent" parameterType="Map" resultMap="selectStudentMap">
       select stu.StudentNo,stu.StudentName,stu.LoginPwd, res.StudentResult from student stu LEFT JOIN
        result res ON
       stu.StudentNo = res.StudentNo WHERE StudentName=#{studentName} and LoginPwd=#{loginPwd};
    </select>
  2. SQL代码块 :简化代码

     <sql id="a">
           <if test="studentName!=null and studentName!=''">
               StudentName=#{studentName},
           </if>
           <if test="loginPwd!=null and loginPwd!=''">
               LoginPwd=#{loginPwd}
           </if>
       </sql>

5、动态SQL

什么是动态SQL :针对参数的控制

  1. <if-where> 有一个条件成立,sql语句就不会出错

  2. <set-if> 修改时用

  3. <trim-if> 拼接where set 关键字的同时还可以自动忽略和添加and/or/, 等

  4. <choose-when-otherwise> 等值判断

  5. <foreach> 动态传参的类型可以为Array /List /Map(也可也和普通参数混用)

     

    <select id="selectArray" resultMap="selectStudentMap">
    select * from student WHERE  StudentNo in
    <foreach collection="array" item="studentNo" open="(" separator="," close=")">
           #{studentNo}
    </foreach>
    </select>

    <select id="selectList" resultMap="selectStudentMap">
       select * from student WHERE  StudentNo in
       <foreach collection="list" item="studentNo" open="(" separator="," close=")">
           #{studentNo}
       </foreach>
    </select>
    <select id="selectMap" resultMap="selectStudentMap">
       select * from student where  StudentName=#{studentName} and StudentNo in
       <foreach collection="list" item="studentNo" open="(" separator="," close=")">
           #{studentNo}
       </foreach>
    </select>

 

  自我学习的总结,自己复习或者回顾观看

 

 

posted on 2022-08-29 14:29  Sustain博客  阅读(31)  评论(0编辑  收藏  举报

导航