欧美free性护士vide0shd,老熟女,一区二区三区,久久久久夜夜夜精品国产,久久久久久综合网天天,欧美成人护士h版

首頁綜合 正文
目錄

柚子快報邀請碼778899分享:Mybatis-基礎操作-更新

柚子快報邀請碼778899分享:Mybatis-基礎操作-更新

http://yzkb.51969.com/

一.演示:

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-基礎操作-更新

http://yzkb.51969.com/

好文鏈接

評論可見,查看隱藏內容

本文內容根據網絡資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀點和立場。

轉載請注明,如有侵權,聯(lián)系刪除。

本文鏈接:http://gantiao.com.cn/post/19536063.html

發(fā)布評論

您暫未設置收款碼

請在主題配置——文章設置里上傳

掃描二維碼手機訪問

文章目錄