1.Spring-mybatis-更新实现

This commit is contained in:
2025-09-26 10:23:07 +08:00
parent ff1aa03490
commit be745c3d1a
2 changed files with 26 additions and 4 deletions

View File

@@ -1,10 +1,7 @@
package com.inmind.mapper;
import com.inmind.pojo.Emp;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.*;
@Mapper
public interface EmpMapper {
@@ -26,4 +23,13 @@ public interface EmpMapper {
@Insert("insert into emp (username, name, gender, image, job, entrydate, dept_id, create_time, update_time)" +
"values (#{username},#{name},#{gender},#{image},#{job},#{entrydate},#{deptId},#{createTime},#{updateTime})")
public void insert(Emp emp);
/*
update emp set username = '11', name = '22', gender = 1, image = '1.jpg', job = 2
, entrydate = '2008-01-01', dept_id = 1,update_time = now() where id = 21
*/
//更新员工
@Update("update emp set username = #{username}, name = #{name}, gender = #{gender}, image = #{image}, job = #{job}" +
", entrydate = #{entrydate}, dept_id = #{deptId},update_time = #{updateTime} where id = #{id}")
public void update(Emp emp);
}

View File

@@ -40,4 +40,20 @@ class SpringbootMybatisCrudApplicationTests {
System.out.println(emp);
System.out.println("主键id:"+emp.getId());
}
@Test
void testUpdate(){
Emp emp = new Emp();
emp.setId(21);
emp.setUsername("ls3");
emp.setName("李四3");
emp.setGender((short) 1);
emp.setImage("1.jpg");
emp.setJob((short) 1);
emp.setEntrydate(LocalDate.of(2015,1,1));
emp.setDeptId(1);
emp.setUpdateTime(LocalDateTime.now());
empMapper.update(emp);
}
}