柚子快報邀請碼778899分享:Mybatis-基礎操作-更新
柚子快報邀請碼778899分享:Mybatis-基礎操作-更新
一.演示:
1.SQL語句:
-- 更新(修改)數據
update emp set username ='',name='',gender='',image='',job='',entrydate='',dept_id='',update_time='' where id=1;
/*有的字段不是字符串,寫''僅僅是為了不報錯*/
2.Mybatis演示:更新操作需要注解@Update
//更新員工
? @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);
二.演示:
1.EmpMapper接口里:
package com.itheima.mapper;
?
import com.itheima.pojo.Emp;
import org.apache.ibatis.annotations.*;
?
@Mapper
public interface EmpMapper {
?
? //根據ID刪除數據-->需要注解@Delete
? /* 刪除的id不確定,因此需要定義為動態(tài)的,在調用接口里的方法時要用到id,
? ? ? 所以要傳遞一個參數表示id。由于是動態(tài)的,還需要
? ? Mybatis里提供的參數占位符即#{參數名字}
? ? */
? @Delete("delete from emp where id = #{id}")
? public int delete(Integer id); //有返回值時代表一共操作了幾條記錄
?
?
?
? //新增員工
? @Options(useGeneratedKeys = true,keyProperty = "id")
? ? //useGeneratedKeys = true代表需要拿到生成的主鍵值,keyProperty = "id"代表獲取的主鍵最終會封裝到Emp對象的id屬性當中
? @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);//形參是pojo里的Emp類對象
?
?
?
? //更新員工
? @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);
?
}
?
2.測試類:
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 testDelete(){
int delete = empMapper.delete(16);
System.out.println(delete);//運行結果為0
/* 因為剛才已經把id為17的員工刪除了,此時就無法刪除了,
? 故操作了0條數據
*/
}
?
?
@Test //@Test可以寫多個
public void testInsert(){
//構造員工對象
Emp emp=new Emp();
emp.setUsername("Tom3");
emp.setName("湯姆3");
emp.setImage("1.jpg");
emp.setGender((short)1 );
emp.setJob((short)1);
emp.setEntrydate(LocalDate.of(2000,1,1));
emp.setCreateTime(LocalDateTime.now());
emp.setUpdateTime(LocalDateTime.now());
emp.setDeptId(1);
?
//執(zhí)行新增員工信息操作
empMapper.insert(emp);
System.out.println(emp.getId());//運行結果為21,代表id為21
}
?
?
@Test
public void testUpdate(){
//構造員工對象
Emp emp=new Emp();
emp.setId(18);//把id為18的員工數據進行更新
emp.setUsername("Tom1");
emp.setName("湯姆1");
emp.setImage("1.jpg");
emp.setGender((short)1 );
emp.setJob((short)1);
emp.setEntrydate(LocalDate.of(2000,1,1));
emp.setUpdateTime(LocalDateTime.now());//要設置為當前時間,每一次更新都需要更改updateTime
emp.setDeptId(1);
?
//執(zhí)行更新員工操作
empMapper.update(emp);
}
?
}
?
3.運行結果:
三.總結:
柚子快報邀請碼778899分享:Mybatis-基礎操作-更新
好文鏈接
本文內容根據網絡資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀點和立場。
轉載請注明,如有侵權,聯(lián)系刪除。