[MyBatis]--(3)映射器--(2)增删改

版权声明: https://blog.csdn.net/zhaoyaxuan001/article/details/80785818

声明:

Author:赵志乾

Date:2018-6-23

Declaration:All Right Reserved!!!



背景知识:

    CURD就是常说的增删改查,对应的词汇分别为增(Create)、删(Delete)、改(Update)、查(Retrieve)。通常而言,在数据库操作中,四个操作都会涉及。其中最复杂的就是查询操作,而增、删和改比较类似,相对而言也比较简单。毕竟增删改的返回值都只是一个整数,用以表征这一操作对多少条记录生效。


增删改的配置属性

    增删改在映射器中分别对应3个元素:insert、delete和update。这3个元素对应到sql语句中,都是对表记录进行操作,且返回值均为一个整数,用于表征这一操作实际影响了多少条表记录。因功能相似,它们的配置属性也是相同的。最常用的是以下2个:

id:其值对应于Mapper接口文件中的方法名,供MyBatis调用时使用。其和Mapper的命名空间结合在一起,需要在MyBatis上下文中唯一。

paramterType:指定入参类型,其值可以是全限定名或别名。常用的是JavaBean或者Map。

注:除了上面的两个配置属性外,还有其他的一些配置属性,通常应用中使用默认值即可。它们分别如下表:


增删改配置示例:

insert元素配置示例:

<insert id="insertRole" parameterType="com.zzq.model.Role">
    insert into tbl_role(role_name,note) value(#{roleName},#{note})
</insert>

update元素配置示例:

<update id="updateRole" parameterType="com.zzq.model.Role">
    update tbl_role 
    set role_name=#{roleName}, note=#{note}
    where id=#{id}
</update>

delete元素配置示例:

<delete id="deleteRole" parameterType="long">
    delete from tbl_role where id=#{id}
</delete>


邮箱:[email protected]

参考资料:《深入浅出MyBatis技术原理与实践》--杨开振


猜你喜欢

转载自blog.csdn.net/zhaoyaxuan001/article/details/80785818
今日推荐