增加数据
原生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}