MySQL增删改操作

本文详细介绍了如何使用原生SQL和MyBatis进行数据的插入与更新操作。提供了完整的语法示例,包括插入指定字段、全量字段以及更新数据时的条件设置。同时展示了Mapper接口和对应的Mapper层SQL语句,帮助开发者更好地理解和应用这些操作。
摘要由CSDN通过智能技术生成

增加数据

原生SQL

插入指定字段
语法:

insert into 表名称(字段1,字段2,字段3) values (值1,值2,值3);

示例:

INSERT INTO test(id, name, parent_id, deep, type, create_time, update_time) VALUES('12ab9807bda','分类','2222',2,3,'2022-08-07 12:45:12', '2021-09-08 02:12:56');

插入全量字段
语法:

insert into 表名称 values(值1,值2,值3,...,所有字段值);

示例:

INSERT INTO test VALUES('ab129856abc40','分类','2222',2,3,'2022-08-07 12:45:12', '2021-09-08 02:12:56');

mybatis

mapper接口:

    void insert(@Param("entity") Category category);

mapper层SQL语句:

    <insert id="insert">
        INSERT INTO category(id, name, parent_id, deep, type, create_time, update_time)
        VALUES(#{entity.id}, #{entity.name}, #{entity.parentId}, #{entity.deep}, #{entity.type}, #{entity.createTime}, #{entity.updateTime});
    </insert>

更新数据

原生SQL

语法:

 UPDATE 表名称
 SET 字段名1= 值1,
 字段名2= 值2,
 字段名3 = 值3

示例:

 UPDATE category
 SET name = '三生三世',
 parent_id = '222222',
 level = 2
 WHERE id = '4'

mybatis

mapper语句:

    void updateCategory(@Param("entity") Item item);

mapper层SQL语句:

        UPDATE item
        <trim prefix="SET" suffixOverrides=",">
            <if test="entity.catId != null and entity.catId != ''">
                cat_id = #{entity.catId},
            </if>
            <if test="entity.catName != null and entity.catName != ''">
                cat_name = #{entity.catName},
            </if>
            <if test="entity.updateId != null and entity.updateId != ''">
                update_id = #{entity.updateId},
            </if>
        </trim>
        WHERE id = #{entity.id}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值