zoukankan      html  css  js  c++  java
  • mybatis使用小记

    参考资料:http://blog.csdn.net/hupanfeng/article/details/9098453

    1、设置不缓存每次查询的结果:

    如题,通过设置 flushCache="true" useCache="false" 两个属性来达到目的。

    flushCache

    将其设置为true,不论语句什么时候被调用,都会导致缓存被清空。默认值:false。

    useCache

    将其设置为true,将会导致本条语句的结果被缓存。默认值:true。

    2、返回类型的设置:

    resultType、resultMap不可同时使用(虽然都指定这两个属性时不会报错)。

    前者将数据库表的字段名映射成指定的字段名,并封装成指定的对象;后者将数据库表字段名映射成返回类型里同名字段,若有些字段在返回类型里没有对应字段则可能出错。

    3、insert语句返回自增主键值

    方法之一是在insert标签里加上useGeneratedKeys="true", keyProperty="addedTime" 这两个属性,其中addedTime是传入参数之一,插入后返回的最新主键值将赋给该变量,程序中就可以通过该传入参数获得新主键值。

    4、MyBatis SQL语句中 foreach的使用

      相关资料:http://blog.csdn.net/isea533/article/details/21237175

      示例如下。foreach可对三种对象做循环,分别为List,[](array),Map三种。collection指出相对哪个元素做循环,item循环中的具体对象,index指序号(List、数组中)或key(map中)。

        <insert id="insertPostImgsInfo" parameterType="map"> <!--传入的参数为map对象:{'postId':xxx, 'imgPath': [...] }-->
            insert into post_img (pid,img_order,img_location) values
            <foreach collection="imgPath" item="item" index="index"
                separator=",">
                (
                #{postId},#{index},#{item}
                )
            </foreach>
        </insert>

    5、关于#{}和${}

      默认情况下,前者有占位符的效果,使用#{}语法,MyBatis会产生PreparedStatement语句,并且安全地设置PreparedStatement参数,这个过程中MyBatis会进行必要的安全检查和转义;而后者则是直接拼接,传入什么就是什么。示例如下:

    执行SQL:Select * from emp where name = #{employeeName}
    参数:employeeName=>Smith
    解析后执行的SQL:Select * from emp where name = ?
    并设置参数为 'Smith'
    
    执行SQL:Select * from emp where name = ${employeeName}
    参数:employeeName传入值为:Smith
    解析后执行的SQL:Select * from emp where name =Smith

    总的来说:$y=2x+x^2$

      ${}方式会引发SQL注入的问题,同时也会影响SQL语句的预编译,所以从安全性和性能的角度出发,能使用#{}的情况下就不要使用 ${}

      而对于动态表名、动态字段、排序字段,则只能使用${}才能达到预期的功能。注意:当使用${}参数作为字段名或表名时,需要指定statementType=STATEMENT,而使用#{}时不能有该语句

    6、MyBatis使用like查询:select * from person where name  like "%"#{name}"%"

    7、MyBatis批量插入:

     1 <!--参数是一个map,包含userId和arrayJourneyData两字段,后者是一个数组-->
     2     <insert id="insertJourneyBatch" parameterType="map">
     3         insert into
     4         train_journey
     5         (
     6         user_id,
     7         local_journey_id,
     8         begin_time,
     9         end_time,
    10         journey_type,
    11         begin_latitude,
    12         end_latitude,
    13         begin_longitude,
    14         end_longitude,
    15         driving_mode,
    16         duration,
    17         rapid_acc_times,
    18         rapid_turn_times,
    19         acc_score,
    20         turn_score,final_score
    21         )
    22         values
    23         <foreach collection="arrayJourneyData" item="item" index="index"
    24             separator=",">
    25             (
    26             #{userId},
    27             #{item.localJourneyId},
    28             #{item.beginTime},
    29             #{item.endTime},
    30             #{item.journeyType},
    31             #{item.beginLatitude},
    32 
    33             #{item.endLatitude},
    34             #{item.beginLongitude},
    35             #{item.endLongitude},
    36             #{item.drivingMode},
    37             #{item.duration},
    38 
    39             #{item.rapidAccTimes},
    40             #{item.rapidTurnTimes},
    41             #{item.accScore},
    42             #{item.turnScore},
    43             #{item.finalScore}
    44             )
    45         </foreach>
    46     </insert>
    View Code

     8、MyBatis批量查询

     1     <!--参数是个[{},{},..],数组每个元素包含userId、beginTime两字段-->
     2     <select id="selectLatestGpsInJourneyByUseridBegintime" parameterType="list" resultType="map">
     3         select
     4         user_id as userId,
     5         timestamp,
     6         latitude,
     7         longitude,
     8         speed,
     9         bearing
    10         from train_feature_new
    11         where
    12         <foreach item="item" index="index" collection="list" open="(" separator="or" close=")">user_id=#{item.userId} and journey_time=#{item.beginTime}</foreach>
    View Code
  • 相关阅读:
    108. Convert Sorted Array to Binary Search Tree
    How to check if one path is a child of another path?
    Why there is two completely different version of Reverse for List and IEnumerable?
    在Jenkins中集成Sonarqube
    如何查看sonarqube的版本 how to check the version of sonarqube
    Queue
    BFS广度优先 vs DFS深度优先 for Binary Tree
    Depth-first search and Breadth-first search 深度优先搜索和广度优先搜索
    102. Binary Tree Level Order Traversal 广度优先遍历
    How do I check if a type is a subtype OR the type of an object?
  • 原文地址:https://www.cnblogs.com/z-sm/p/4655678.html
Copyright © 2011-2022 走看看