mybatis实战提升(关于各种属性的详解)

mybatis实战提升(关于各种属性的详解)

一:属性讲解

1.默认配置

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="study.dao.StudentDao">

   
</mapper>

2.namespace的讲解
namespace属性:用于指定当前的映射文件和那个接口进行映射,需要知道那个接口的文件路径,需要标注包的完整路径接口

3.resultMap和resultType讲解

<select id="findByUsername" resultMap="UserEntityMap">
        SELECT * FROM t_user username = #{username}
    </select>

resultType:表示查询的结果集类型,只需要指定对应映射类的类型,并且包含完整包接口: resultType="com.example.store.entity.User

resultMap: 表示当表的字段和类的对象属性的字段名称不一致值,来自定义映射规则
(由于实体类中采用了驼峰命名方式,导致实体类中的字段名和数据库中的列名不一致,需要自定义映射规则)
4.自定义映射规则resultMap
在定义映射规则时主键是不可以省略的<result column="uid" property="uid"></result>

<!--    id属性:标签给这个映射负责分配一个唯一的id值,对应的就是resultMap=“id"的属性的取值-->
<!--    type属性:取值是一个类,表示的是数据库中的查询结果与java中那个实体类进行结果映射-->

    <resultMap id="UserEntityMap" type="com.example.store.entity.User">
<!--        将表的资源和类的睡醒不一样的字段进行匹配指定,名称一直的字段可以省略不写-->
<!--
            配合完成名称不一致的映射
            column属性:表示表中的资源名称
            property属性:表示类中的属性名称
-->
<!--        在定义映射规则时主键是不可以省略的-->
        <result column="uid" property="uid"></result>
        <result column="is_delete" property="isDelete"/>
        <result column="created_user" property="createdUser"/>
        <result column="created_time" property="createdTime"></result>
        <result column="modified_user" property="modifiedUser"></result>
        <result column="modified_time" property="modifiedTime"></result>
    </resultMap>

5.useGeneratedKeys的意义
useGeneratedKeys属性:开启某个键的值的递增(主键设置为递增
keyProperty属性:标签将表中的那个字段作为主键进行递增

    <insert id="insert" useGeneratedKeys="true" keyProperty="uid">
        INSERT INTO t_user(username, salt, phone, email,
                           gender, avatar, is_delete,
                           created_user, created_time,
                           modified_user, modified_time)
        values (
                #{username}, #{salt}, #{phone}, #{email},
                #{gender}, #{avatar}, #{isDelete},
                #{createdUser}, #{createdTime},
                #{modifiedUser}, #{modifiedTime})
    </insert>

6.mybatis中的if标签
mybatis中的if标签通常用来判断是否为空

if表示条件判断标签,test接收的是一个返回值为boolean类型的条件,如果test条件结果为true则执行if标签内部的语句
 <update id="updateInfoByUid">
--         if表示条件判断标签,test接收的是一个返回值为boolean类型的条件,如果test条件结果为true则执行if标签内部的语句
        UPDATE t_user SET
            <if test="phone!=null and phone != ''">phone = #{phone},</if>
            <if test="email!=null and phone != ''">email = #{email},</if>
            <if test="gender!=null and gender != ''">gender = #{gender},</if>
            <if test="modified_user!=null and modified_user !=''">modified_user = #{modifiedUser},</if>
            <if test="modified_time!=null and modified_time !=''">modified_time = #{modifiedTime}</if>
        WHERE uid = #{uid}

    </update>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

@黑夜中的一盏明灯

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值