• 【SSM框架】Mybatis详解08(源码自取)之优化注册,#{}与¥{}区别,返回主键,UUID


    • 🧛‍♂️个人主页:杯咖啡
    • 💡进步是今天的活动,明天的保证!
    • ✨目前正在学习:SSM框架,算法刷题
    • 👉本文收录专栏:SSM框架解析
    • 🙌牛客网,刷算法过面试的神级网站,用牛客你也牛。 👉免费注册和我一起学习刷题👈
    • 🐳希望大家多多支持🥰一起进步呀!
    • 😎The man who fears losing has already lost.
      怕输的人已经输了。 - 《权力的游戏》

    ✨前言

    上一篇我们实现了动态代理
    这一篇我们将继续复习,优化mapper.xml文件注册,#{}占位符,${}字符串拼接或字符串替换,返回主键值,UUID

    和我一起复习下去你可以获得一个比较完美框架demo,并且深刻体会框架
    坚持到最后的源码解析你会收获更多哦,加油坚持!!!



    优化mapper.xml文件注册

    mapper.xml四种注册方式

    
        <mappers>
            
            <mapper url="/">mapper>
            
            <mapper resource="StudentMapper.xml">mapper>
            
            <mapper class="com.bjpowernode.mapper.UsersMapper">mapper>
            
            <package name="com.bjpowernode.mapper">package>
        mappers> 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    相比较而言,绝对路径注册比较局限,程序移植比较麻烦;第二种资源路径注册,少量mapper文件还可以,量比较多的时候注册就比较繁琐;第三种在动态代理中必须这样使用,以后使用比较多;第四种批量注册,更加方便,注意默认注册名称是 mapper文件名首字母小写的驼峰命名法,也比较常用。

    #{}占位符

    传参大部分使用#{}传参,它的底层使用的是PreparedStatement对象,是安全的数据库访问 ,防止sql注入.在使用#{}与¥{}的时候,大多数时候我们都是选用#{}哦。
    #{}里如何写,看parameterType参数的类型
    1)如果parameterType的类型是简单类型(8种基本(封装)+String),则#{}里随便写.

    <select id="getById" parameterType="int" resultType="users">  ===> 入参类型是简单类型
         select id,username,birthday,sex,address
         from users
         where id=#{zar}  ===>随便写
     select>
    
    
     <select id="getById" parameterType="int" resultType="users">
         select <include refid="allColumns">include>
         from users
         where id=#{id}
     select>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    2)parameterType的类型是实体类的类型,则#{}里只能是类中成员变量的名称,而且区分大小写.

     <insert id="insert" parameterType="users" >  ==>  入参是实体类
            insert into users (username, birthday, sex, address) 
            values(#{userName},#{birthday},#{sex},#{address})  ==>成员变量名称
        insert>
    
    
        <insert id="add" parameterType="users">
        <selectKey keyProperty="id" resultType="int" order="AFTER">
            select last_insert_id()
        selectKey>
            insert into users (username,birthday,sex,address)
            values (#{userName},#{birthday},#{sex},#{address})
        insert>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    ${}字符串拼接或字符串替换

    1)字符串拼接,一般用于模糊查询中.建议少用,因为有sql注入的风险.
    也分两种情况,同样的看parameterType的类型

    A. 如果parameterType的类型是简单类型,则${}里随便写,但是分版本,如果是3.5.1及以下的版本,只以写value.

    <select id="getByName" parameterType="string" resultType="users">  
            select id,username,birthday,sex,address
            from users
            where username like '%${zar}%'   
        select> 
    
    • 1
    • 2
    • 3
    • 4
    • 5

    B. 如果parameterType的类型是实体类的类型,则${}里只能是类中成员变量的名称.(现在已经少用)
    C. 优化后的模糊查询(以后都要使用这种方式)

     <select id="getByNameGood" parameterType="string" resultType="users">
         select id,username,birthday,sex,address
         from users
         where username like concat('%',#{name},'%')
     select>
    
    
     <select id="getByNameGood" parameterType="string" resultType="users">
         select <include refid="allColumns">include>
         from users
         where username like concat("%",#{name},"%")
     select>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    2)字符串替换
    需求:模糊地址或用户名查询
    select * from users where username like ‘%小%’;
    select * from users where address like ‘%市%’

     
       <select id="getByNameOrAddress" resultType="users">
           select id,username,birthday,sex,address
           from users
           where ${columnName} like concat('%',#{columnValue},'%') 
       select>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    返回主键

    当我们插入一条数据之后,我们需要拿着这个主键id去查找其他的值,或者需要查询新插入的id是多少,那么这个方法就用到了。
    在插入语句结束后, 返回自增的主键值到入参的users对象的id属性中.

    <insert id="insert" parameterType="users" >
          <selectKey  keyProperty="id" resultType="int" order="AFTER">
              select last_insert_id()
          selectKey>
          insert into users (username, birthday, sex, address) values(#{userName},#{birthday},#{sex},#{address})
    insert>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    标签的参数详解:
    keyProperty: users对象的哪个属性来接返回的主键值
    resultType:返回的主键的类型
    order:在插入语句执行前,还是执行后返回主键的值

    UUID

    这是一个全球唯一随机字符串,由36个字母数字中划线组.
    目前数据库中主键使用较多,当然获取唯一随机字符串的方法不止这一种。

      UUID uuid = UUID.randomUUID();
      System.out.println(uuid.toString().replace("-","").substring(20));
    
    • 1
    • 2

    ✨总结

    今天主要复习了优化mapper.xml文件注册,#{}占位符,${}字符串拼接或字符串替换,返回主键值,UUID,小知识点比较碎,需要多看哦!!
    下一篇我们将复习,动态sql!!!
    本次源码放在代码仓库gitee,自取链接

    原创不易,还希望各位大佬支持一下 \textcolor{blue}{原创不易,还希望各位大佬支持一下} 原创不易,还希望各位大佬支持一下

    点赞,你的认可是我创作的动力! \textcolor{green}{点赞,你的认可是我创作的动力!} 点赞,你的认可是我创作的动力!

    收藏,你的青睐是我努力的方向! \textcolor{green}{收藏,你的青睐是我努力的方向!} 收藏,你的青睐是我努力的方向!

    评论,你的意见是我进步的财富! \textcolor{green}{评论,你的意见是我进步的财富!} 评论,你的意见是我进步的财富!

  • 相关阅读:
    C++异常处理throw try catch
    【uniapp】小程序中input输入框的placeholder-class不生效以及解决办法
    FastXML代码编译和调试支持
    开源WebRTC库放大器模式在采集桌面图像时遇到的DPI缩放与内存泄漏问题排查
    计算机系统 流水线技术
    python3中的宏HAVE_VFORK
    链表(单链表、双链表)
    Java Static关键字 单例设计模式
    【编程题】【Scratch四级】2021.06 计算三角形面积
    MySQL表分区
  • 原文地址:https://blog.csdn.net/muzi_longren/article/details/126377572