zoukankan      html  css  js  c++  java
  • Mybatis的使用中的一些不太注意的技巧

    以下就总结一下Mybatis的使用中的一些不太注意的技巧,算是Mybatis的总结笔

    1、插入时主键返回

         我们向数据库插入一条记录是,使用Mybatis的<insert>是无法返回插入的主键的,而我们需要这个刚插入的主键,可以如下返回

             自增主键:使用last_insert_id()查询刚插入的key的id,该方法需要和insert配合使用,是插入之后获取。

             result返回的是插入成功条数,而主键id返回到CourseInfo的id属性中

            

    1. <insert id="insert" parameterType="org.andy.shop.model.CourseInfo" >  
    2.     insert into course_info (id, cname, caddress)  
    3.     values (#{id,jdbcType=INTEGER}, #{cname,jdbcType=VARCHAR}, #{caddress,jdbcType=VARCHAR})  
    4.     
    5.      <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer">  
    6.        select last_insert_id()  
    7.      </selectKey>  
    8.   </insert>  


    或者如下的写法更简单:

    1. <insert id="insert" parameterType="org.andy.shop.model.CourseInfo" useGeneratedKeys="true" keyProperty="id">  
    2.     insert into course_info (id, cname, caddress  
    3.       )  
    4.     values (#{id,jdbcType=INTEGER}, #{cname,jdbcType=VARCHAR}, #{caddress,jdbcType=VARCHAR}  
    5.       )  
    6.   </insert>  


           非自增主键:但我们的ID为uuid字符类型的32为长度时,我们使用mysql的uuid()查询主键,是在查询之后在插入。

    1. <insert id="insert" parameterType="org.andy.shop.model.CourseInfo" >  
    2.     <selectKey keyProperty="id" order="BEFORE" resultType="java.lang.String">  
    3.       select uuid()  
    4.     </selectKey>  
    5.     insert into course_info (id, cname, caddress)  
    6.     values (#{id,jdbcType=VARCHAR}, #{cname,jdbcType=VARCHAR}, #{caddress,jdbcType=VARCHAR})  
    7.   </insert>  
     

        使用oracle数据库时:使用 序列名.nextval()函数获取。


    2、别名的使用

          一般定义别名,在Mapper配置的可以使用别名,定义单个别名如下

    1. <configuration>  
    2. <typeAliases>  
    3.  <typeAlias type="org.andy.shop.model.CourseUserInfo" alias="CourseUserInfo"/>  
    4. </typeAliases>  
    5. </configuration>  

          自动定义别名:Mybatis 将自动扫描包名下的po,别名就是类名(大写小写都可以)
    1. <typeAliases>  
    2.   <package name="org.andy.shop.model"/>  
    3. </typeAliases>  


    3、动态sql使用

          一般我们会涉及多个动态查询条件,一般我们是通过 where 1 = 1,这样可以处理where后面对应全空的情况,我们可以使用<where>标签,该标签可以自动处理。

    1.        <where>  
    2.   <if test="name != nulll">  
    3.      and name like concat('%',trim(#{name,jdbcType=VARCHAR}),'%')   
    4.   </if>  
    5. </where>  


    4、sql片段

         我们可以将常用的重复的sql定义sql片段

        定义如下:

        

    1. <sql id="Base_Column_List" >  
    2.     id, name, url, priority, logo, img  
    3.   </sql>  

       引用该片段如下:

      

    1. <include refid="Base_Column_List" />  

    5、关联查询

    一对一关联查询

           

            一对多关联查询


    6、延迟加载

           延迟加载:先从单表查询,在需要时从关联表查询,可以大大的提高数据库性能,单表查询的性能要快于多表联合查询的速度。

    而Mybatis的associate、collection就支持延迟加载功能。

        

        开启延迟加载:

          需要在Mybatis的核心配置文件中配置configuration开启setting两个配置

          

         设置lazyLoadingEnabled为true, aggressiveLazyLoading为false


    未完待续。

          

           mybatis文档地址:http://www.mybatis.org/mybatis-3/zh/index.html

  • 相关阅读:
    Java中NIO和IO区别和适用场景
    JDK和CGLIB动态代理原理
    java中的Serializable接口的作用
    redis采用序列化方案存对象
    在时间复杂度为O(n)且空间复杂度为O(1)的情况下翻转链表
    给定一个排好序的数组,然后求出丢失的数字
    求字符串里超过字符长度一半的元素
    求你给定两字符串包含的字母数是否完全一致
    动态规划,求数组不相邻数字的最大子串值
    JWT 工具
  • 原文地址:https://www.cnblogs.com/lykxqhh/p/5691185.html
Copyright © 2011-2022 走看看