zoukankan      html  css  js  c++  java
  • Mybatis(1)——映射文件,缓存,整合

    映射文件学习过程: 1.增删改查;2.获取自增主键;3.参数处理:单个,多个参数;4.查询返回值处理;5.动态SQl;6.缓存;7.整合Spring

     1.增删改查:

        <!--id 对应里边的方法-->
        <select id="getStudentById" resultType="mybatis.model.Student">
        select * from student where id = #{id}
        </select>
        <!--void  addStudent(Student student);-->
        <insert id="addStudent" parameterType="mybatis.model.Student" useGeneratedKeys="true" keyProperty="id">
            insert into student (name,address,phone) values (#{name},#{address},#{phone})
        </insert>
        <!--void updateStudent(Student student);-->
        <update id="updateStudent" >
            update student set name=#{name},address=#{address},phone=#{phone} where id=#{id}
        </update>
        <!--void deleteStudent(int id);-->
        <delete id="deleteStudent">
            delete from student where id=#{id}
        </delete>
    

    2.获取自增主键的值:useGeneratedKeys="true" keyProperty="id"  

        <!--void  addStudent(Student student);-->
        <insert id="addStudent" parameterType="mybatis.model.Student" useGeneratedKeys="true" keyProperty="id">
            insert into student (name,address,phone) values (#{name},#{address},#{phone})
        </insert>
    

     3.传入SQL语句的参数处理:

     4.查询返回值处理:

      1)ResultMap的使用:自定义resultMap

        <!--resultMap的使用-->
        <resultMap id="studentMap" type="mybatis.model.Student">
            <!--主键-->
            <id column="id" property="id"></id>
            <!--其他列-->
            <result column="name" property="name"/>
            <result column="address" property="address"/>
            <result column="phone" property="phone"/>
        </resultMap>
        <!--Student getStudentById_resultMap(int id);-->
        <select id="getStudentById_resultMap" resultMap="studentMap">
            select * from student where id=#{id}
        </select>
    
                //测试使用ResultMap
                Student student2 = studentMapper.getStudentById_resultMap(2);
                session.commit();
                //打印对象
                System.out.println(student2);
    

       2)ResultMap的使用:(里边各种各样的标签属性 ) A.关联查询:对级联属性的封装;B. 使用association指定哪个属性是关联对象; C.使用association分步查询(并设置属性可 进行延迟加载 / 懒加载);

                 D. 使用collection指定哪个属性是关联对象(这个属性有多个,所以是一个集合);E.使用collection分步查询(并设置属性可 进行延迟加载 / 懒加载);

                    F.在ResultMap里边,使用鉴别器:discriminator

     

     5.动态SQl:动态地拼接SQL语句

        <if test="  id != null "> id=#{ id } </if> : 一般从传进来的参数进行判断。 test里边使用OGNL语法规则

        <where>  </where>   用来代替SQL语句里的where 条件,将所有的查询条件包括在内,如果条件多出来 and 和 or 会自动被删掉(和 <if> and name=#{name}</if> 嵌套使用)。

        <trim></trim> 自定义字符串截取规则,比<where>标签的截取更加灵活,可以给整个字符串加上(或去掉)前后缀

        <choose></choose> 分支选择,相当于switch语句 里面有 <when>和<otherwise>,相当于 switch的 case 和default 语句

        <set></set>相当于update  ----->update table_name set id=1, name='yang'  语句中的set,会自动去掉<if>标签后多出来的 , 逗号。 与<where>标签类似,可用<trim>标签代替   

        <foreace>批量保存信息

        动态sql有两个内置参数:_parameter   _databaseId: 可以用于<if></if>标签里面进行判断

        <bind>标签可以将参数绑定,再拼接符号,如%Name%  ,再从SQL语句中使用

        *  SQL片段:抽取可重用的SQL片段

    6.缓存:

    https://www.cnblogs.com/whgk/p/6722497.html

    使一级缓存失效的四种情况:

        1)使用不同sqlsession

        2)清空sqlsession.clearCache();      ------>清理的是一级缓存

        3)查询条件不同

        4)查询条件相同,两次查询之间有进行  增删改查 ,相对应的缓存会被清理。

        5)给查询的mapper标签设置flushCache="true"(select默认为false, insert、delete、update默认true),每次调用映射文件里的方法,都会清理掉缓存。

     注:二级缓存时,sqlsession关闭后,缓存才会从一级缓存提交到二级缓存。

        6)localCacheScrop 本地缓存作用域 也可以设置一级缓存不起作用

    EHcahe:

        入门:https://www.cnblogs.com/jingmoxukong/p/5975994.html

  • 相关阅读:
    docker删除所有服务service,停止并删除所有容器container
    harbor
    yml文件
    linux 上安装portainer.io
    凤凰之谜 1/4 潜行者
    凤凰之谜 4/4 猎人
    凤凰之谜 3/4 德鲁伊 迷宫
    Dijkstra最短路径算法
    LeetCode 到底怎么刷?GitHub 上多位大厂程序员亲测的高效刷题方式
    How do I run a Python script from C#?
  • 原文地址:https://www.cnblogs.com/Lemonades/p/11099731.html
Copyright © 2011-2022 走看看