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>
相关推荐
xrkhy4 小时前
微服务之ShardingSphere
数据库·微服务·oracle
JIngJaneIL5 小时前
停车场管理|停车预约管理|基于Springboot的停车场管理系统设计与实现(源码+数据库+文档)
java·数据库·spring boot·后端·论文·毕设·停车场管理系统
煎蛋学姐5 小时前
SSM儿童福利院管理系统ys9w2d07(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
数据库·ssm 框架·儿童福利院管理系统
sg_knight5 小时前
MySQL 空间索引(SPATIAL)详解:地理位置数据的高效查询利器
数据库·mysql·database·索引·关系型数据库·空间索引·spatial
梦子yumeko6 小时前
第五章Langchain4j之基于内存和redis实现聊天持久化
数据库·redis·缓存
IndulgeCui7 小时前
【金仓数据库产品体验官】KSQL Developer Linux版安装使用体验
linux·运维·数据库
一马平川的大草原7 小时前
基于n8n实现数据库多表数据同步
数据库·数据同步·dify·n8n
老华带你飞9 小时前
商城推荐系统|基于SprinBoot+vue的商城推荐系统(源码+数据库+文档)
java·前端·数据库·vue.js·spring boot·毕设·商城推荐系统
一 乐9 小时前
物业管理系统|小区物业管理|基于SprinBoot+vue的小区物业管理系统(源码+数据库+文档)
java·前端·数据库·vue.js·spring boot·后端
这周也會开心9 小时前
Spring框架
java·数据库·spring