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>
相关推荐
吻等离子32 分钟前
解决 MySQL 数据库无法远程连接的问题
数据库·mysql·adb
八股文领域大手子1 小时前
密码学基石:哈希、对称/非对称加密与HTTPS实践详解
数据库
林九生1 小时前
【SQLSERVER】Ubuntu 连接远程 SQL Server(MSSQL)
数据库·ubuntu·sqlserver
Wnq100721 小时前
巡检机器人数据处理技术的创新与实践
网络·数据库·人工智能·机器人·巡检机器人
后院那片海1 小时前
MySQL数据库操作
数据库·mysql
python算法(魔法师版)2 小时前
数据库故障排查指南:从连接问题和性能优化
服务器·网络·数据库·性能优化
vortex52 小时前
新手上路之 NoSQL 数据库学习
数据库·学习·nosql
杨云龙UP2 小时前
SQL Server 中的 GO 及其与其他数据库的对比
数据库·sql·mysql·oracle·sqlserver
Linux运维技术栈2 小时前
MySQL 8.0 单节点部署与一主两从架构搭建实战
数据库·mysql·架构
又逢乱世3 小时前
Ubuntu 安装 HAProxy
服务器·网络·数据库