二、mybatis 入门

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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容