美文网首页
二、mybatis 入门

二、mybatis 入门

作者: cqzhangjian | 来源:发表于2017-12-28 08:30 被阅读0次

1.动态sql

根据传入参数的个数以及类型,动态的拼接 SQL, 要求看得懂

<!-- 提取公用的sql片段 -->
    <sql id="baseSql">
        select id,name,password,birthday
        from tbl_user
    </sql>
    
    
    

    <select id="selectforeach" parameterType="map" resultType="com.xingxue.mybatis.model.UserModel">
        <include refid="baseSql"></include>
        where id  in 
        <foreach collection="ids" item="id" open="(" close=")" separator=",">
            #{id}
        </foreach>
    </select>




        <update id="updateSet" parameterType="com.xingxue.mybatis.model.UserModel">
        
            update tbl_user
            <set>
                <if test="name !=null">
                    name = #{name},
                </if>
                <if test="password !=null">
                    password = #{password},
                </if>
                <if test="birthday !=null">
                    birthday=#{birthday},
                </if>
            </set>
            <where>
                <if test="id != null">
                    id = #{id}
                </if>
            </where>
    
    </update>

    <!-- 分支判断,只要有一个条件满足后,其他的条件就不会执行了 -->
    <select id="selectChoose" parameterType="com.xingxue.mybatis.model.UserModel" resultType="com.xingxue.mybatis.model.UserModel">
        select id,name,password,birthday
        from tbl_user
        <where>
            <choose>
                <when test="id != null">
                    and id =#{id}
                </when>
                <when test="name != null">
                    and name =#{name}
                </when>
                <otherwise>
                    and 1 = 1
                </otherwise>
            </choose>
        </where>
    </select>


    <!-- mybatis框架使用的是OGNL 来取值(OGNL存取的值,都是放于上下文中的根区域root),可以通过放在root区域的对象,就可以通过对象的属性直接获取
            if 标签就支持 ognl 表达式
            where 1 = 1  这种方式就不能使用数据库的索引查询,效率低
            
            
            where 标签会自动去掉多余的 and 、 or
         -->
     <select id="selectIf" parameterType="com.xingxue.mybatis.model.UserModel" resultType="com.xingxue.mybatis.model.UserModel">
            
            select id,name,password,birthday
            from tbl_user
            
            <where>
                <if test="id !=null">
                    and id = #{id} 
                </if>
                <if test="name !=null">
                    and name = #{name}
                </if>
                <if test="password !=null">
                    and password = #{password}
                </if>
                <if test="birthday !=null">
                    and birthday = #{birthday}          
                </if>
            </where>
     </select>

2.关联查询 详见代码

3.查询缓存

Mybatis框架中缓存分:

  • 一级缓存:SqlSession对象作为一级缓存对象缓存数据

  • 二级缓存:Mybatis中sqlsessionFactroy对象作为二级缓存对象,只不过自带了实现,不需要带入第三方的缓存实现包,但是mybatis毕竟是专业做orm持久层框架的,二级缓存往往还是使用专业的二级缓存实现包:

Ehcache 、 oscache 、 redis 、 menercacher 等

注意事项:

  • 如果使用二级缓存,缓存对象必须实现Serializable接口


    图片.png
  • 使用第三方的ehcahe作为mybatis的二级缓存
    导入jar包


    图片.png
  • 在mapper使用ehcache


    图片.png

相关文章

  • Mybatis的入门

    一.Mybatis介绍 二.Mybatis的架构 三.Mybatis入门程序开发 下载mybatis 导包核心+依...

  • MyBatis

    MyBatis学习总结(一)——MyBatis快速入门 超详细MyBatis入门讲解

  • 第二天:mybatis基本使用

    第二天:mybatis基本使用 mybatis框架 学习计划 共四天第一天:mybatis入门mybatis的概述...

  • MyBatis入门(二)

    MyBatis 的真正强大在于它的映射语句,也是它的魔力所在。由于它的异常强大,映射器的 XML 文件就显得相对简...

  • MyBatis入门(二)

    学习内容: 使用Mybatis开发Dao,通常有两个方法: 原生Dao开发 Mapper接口开发方法 1. 原生D...

  • MyBatis入门(二)

    Configuration.xml中配置的内容和顺序如下: 以下内容可以有, 也可以没有, 但是有的时候, 顺序必...

  • 二、mybatis 入门

    1.动态sql 根据传入参数的个数以及类型,动态的拼接 SQL, 要求看得懂 2.关联查询 详见代码 3.查询缓...

  • 深入浅出Mybatis-Mybatis-Generator

    目录 入门 Mybatis Generator 是什么 Mybatis Generator是Mybatis的代码生...

  • Mybatis快速入门

    Mybatis 学习内容 Mybatis框架的简单入门 Mybatis框架基本的使用 Mybatis框架的深入和多...

  • MyBatis之快速入门

    title: MyBatis之快速入门tags: MyBatiscategories: MyBatis 若图片无法...

网友评论

      本文标题:二、mybatis 入门

      本文链接:https://www.haomeiwen.com/subject/lskmgxtx.html