0
点赞
收藏
分享

微信扫一扫

list转树形,亲测可用

云朵里的佛光 03-30 15:30 阅读 2

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,我们可以指定查询结果字段和实体类属性字段的映射关系

例如

<resultMap id="AddressResultMap" type="Address" autoMapping="true">
    <result column="user_id" property="userId"/>
</resultMap>

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

4.动态SQL

4.1 if

例如

<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

例如

<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

例如

<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标签内部为空时,会自己的把自己去掉,造成语法错误

例如

<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:结束符号

例如

批量插入

<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>

批量删除

<delete id="deleteAddressBatch" parameterType="address">
    delete from t_address where id in
    <foreach collection="list" separator="," item="every" open="(" close=")">
        #{every}
    </foreach>
</delete>
举报

相关推荐

0 条评论