Mybatid基础操作–更新
package com.itheima.mapper;
import com.itheima.pojo.Emp;
import org.apache.ibatis.annotations.*;
@Mapper //在运行时,会自动生成该接口的实现类对象(代理对象),并且将该对象交给IOC容器管理
public interface EmpMapper {
//根据ID删除数据
//#:占位符;#{id}:动态把id传进去,id不是固定
//注意事项:如果mapper接口方法形参只有一个普通类型的参数,#{...}里面的属性名可以随便写,如:#{id},#{value}
@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);
}
package com.itheima;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.Emp;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.time.LocalDate;
import java.time.LocalDateTime;
@SpringBootTest
class SpringbootMybatisCrudApplicationTests {
@Autowired
private EmpMapper empMapper;
//更新员工
@Test
public void update(){
Emp emp = new Emp();
//更新的是id为20的记录
emp.setId(20);
emp.setUsername("lsd2");
emp.setName("林森2");
emp.setImage("22.png");
emp.setGender((short)1);
emp.setJob((short)1);
emp.setEntrydate(LocalDate.of(2020,1,2));
emp.setDeptId(1);
emp.setUpdateTime(LocalDateTime.now());
empMapper.update(emp);
}
}