Mybatis-02

Mybatis

1.${} 和 #{}的区别

${}:表示拼接sql串,可能会发生sql注入

#{}:表示一个占位符号,可以预解析,防止sql注入

2@Param注解

当涉及到多个参数传参的时候,我们直接使用变量名会发现控制台有错误提示:

Parameter 'XXX' not found.Available parameters are [arg1,arg0,param1,param2] 意思是XXX变量没有生命

There is no getter for property name 'username' in class java.lang.Integer

这时我们可以使用@Param注解的方式来解决问题

3.parameterType和parameterType

3.1 parameterType

参数类型,可以是基本数据类型,也可以是引用数据类型,还可以是实体类类型

3.2 resultType

resultType属性可以指定结果集的类型,它支持基本数据类型和实体类类型

++parameterType和parameterType一样,如果注册过类型别名,可以直接使用别名,如果没有注册过别名,则必须使用全限定类名++

3.3 resultMap

我们在进行数据库设计的时候,多个单词往往是使用_来连接的,但是在实体类中的属性往往采用小驼峰的方式来命名,这就导致了字段名往往无法对应上,这个时候我们就需要resultMap来解决这个问题

通过resultMap,我们可以指定查询结果字段和实体类属性字段的映射关系

例如

xml 复制代码
<resultMap id="AddressResultMap" type="Address" autoMapping="true">
    <result column="user_id" property="userId"/>
</resultMap>

其中column为数据库表中的列名,property为实体类中的属性名

4.动态SQL

4.1 if

例如

xml 复制代码
<select id="getAddressByDynamic" parameterType="address" resultType="address" resultMap="AddressResultMap">
    select * from t_address
        <if test="id!=null">
        	id=#{id}
        </if>
</select>

4.2 choose、when、otherwise

例如

xml 复制代码
<select id="list" parameterType="User" resultMap="userResult">
    select * from t_user where 1=1
    <choose>
        <when test="id != null">
            and id = #{id}
        </when>
        <when test="username != null and username != ''">
            and username = #{username}
        </when>
        <otherwise>
            and nickname = #{nickname}
        </otherwise>
    </choose>
</select>

4.3 where

使用where标签的好处:①where标签内部为空时,会自动把where自己去掉

​ ②where标签会帮我们去掉内部的第一个and或or

例如

xml 复制代码
<select id="getAddressByDynamic" parameterType="address" resultType="address" resultMap="AddressResultMap">
    select * from t_address
    <where>
        <if test="id!=null">
            and id=#{id}
        </if>
        <if test="addr!=null">
            and addr=#{addr}
        </if>
        <if test="phone!=null">
            and phone=#{phone}
        </if>
        <if test="postcode!=null">
            and postcode=#{postcode}
        </if>
        <if test="userId!=null">
            and userId=#{userId}
        </if>
    </where>
</select>

4.4 set

使用set标签的好处:set标签可以帮我们去掉内部的最后一个逗号

使用set标签而带来的问题:当set标签内部为空时,会自己的把自己去掉,造成语法错误

例如

xml 复制代码
<update id="updateAddressByDynamic" parameterType="address">
    update t_address
    <set>
        <if test="addr!=null">
            addr = #{addr},
        </if>
        <if test="phone!=null">
            phone=#{phone},
        </if>
        <if test="postcode!=null">
            postcode=#{postcode},
        </if>
        <if test="userId!=null">
            userId=#{userId},
        </if>
    </set>
    where id = #{id}
</update>

4.5 foreach

foreach主要用于批量插入或批量删除等类似的批量操作

foreach标签中的属性:①collection:需要遍历的列表

​ ②item:每一项的形参名

​ ③index:每一项的索引名

​ ④separator:分隔符

​ ⑤open:开始符号

​ ⑥close:结束符号

例如

批量插入

xml 复制代码
<insert id="addAddressBatch" parameterType="address">
    insert into t_address (addr,phone,postcode,user_id) values
    <foreach collection="list" separator="," item="every">
        (#{every.addr},#{every.phone},#{every.postcode},#{every.userId})
    </foreach>
</insert>

批量删除

xml 复制代码
<delete id="deleteAddressBatch" parameterType="address">
    delete from t_address where id in
    <foreach collection="list" separator="," item="every" open="(" close=")">
        #{every}
    </foreach>
</delete>
相关推荐
疯狂的挖掘机6 小时前
记一次基于QT的图片操作处理优化思路(包括在图上放大缩小,截图,画线,取值等)
开发语言·数据库·qt
奇树谦7 小时前
Qt | 利用map创建多个线程和定时器
网络·数据库·qt
用户47949283569158 小时前
性能提升 4000%!我是如何解决 运营看板 不能跨库&跨库查询慢这个难题的
数据库·后端·postgresql
电商API&Tina8 小时前
跨境电商 API 对接指南:亚马逊 + 速卖通接口调用全流程
大数据·服务器·数据库·python·算法·json·图搜索算法
侠客行03178 小时前
Mybatis二级缓存实现详解
java·mybatis·源码阅读
robinson19888 小时前
验证崖山数据库标量子查询是否带有CACHE功能
数据库·oracle·cache·自定义函数·崖山·标量子查询
老华带你飞8 小时前
农产品销售管理|基于java + vue农产品销售管理系统(源码+数据库+文档)
java·开发语言·前端·数据库·vue.js·spring boot·后端
SelectDB9 小时前
5 倍性能提升,Apache Doris TopN 全局优化详解|Deep Dive
数据库·apache
JIngJaneIL9 小时前
基于springboot + vue房屋租赁管理系统(源码+数据库+文档)
java·开发语言·前端·数据库·vue.js·spring boot·后端
陈平安安10 小时前
设计一个秒杀功能
java·数据库·sql