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>
相关推荐
007php0072 分钟前
某大厂MySQL面试之SQL注入触点发现与SQLMap测试
数据库·python·sql·mysql·面试·职场和发展·golang
YS_Geo34 分钟前
Redis 深度解析:数据结构、持久化与集群
数据结构·数据库·redis
IvanCodes37 分钟前
六、Docker 核心技术:Dockerfile 指令详解
java·数据库·docker
寻星探路42 分钟前
数据结构造神计划第三天---数据类型
数据库
lunzi_fly1 小时前
【源码解读之 Mybatis】【基础篇】-- 第1篇:MyBatis 整体架构设计
java·mybatis
l5657581 小时前
第五十五天(SQL注入&增删改查&HTTP头&UA&XFF&Referer&Cookie&无回显报错&复盘)
数据库
上海达策TECHSONIC2 小时前
经验分享:如何让SAP B1数据库性能提升50%
运维·数据库·运维开发
言之。2 小时前
深入解析Django重定向机制
数据库·django·sqlite
时序数据说2 小时前
国内开源时序数据库IoTDB介绍
大数据·数据库·物联网·开源·时序数据库·iotdb