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不确定,因此需要定义为动态的,在调用接口里的方法时要用到id,
       所以要传递一个参数表示id。由于是动态的,还需要
     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);
​
        //执行新增员工信息操作
        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);
​
        //执行更新员工操作
        empMapper.update(emp);
    }
​
}
​

3.运行结果:


三.总结:


  • 5
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
mybatis-plus-generator和mybatis-plus是用于简化MyBatis开发的两个工具。mybatis-plus是一个MyBatis的增强工具包,提供了一些便捷的操作,节约了编简单SQL的时间。而mybatis-plus-generator是一个代码生成器,可以自动生成一些基本的Controller、Service、Mapper和Mapper.xml文件。 通过整合mybatis-plus和mybatis-plus-generator,我们可以更高效地开发项目中的单表增删改查功能。使用mybatis-plus-generator可以自动生成一些基本的文件,例如Controller、Service、Mapper和Mapper.xml,极大地减少了手动创建这些文件的时间和工作量。而mybatis-plus提供的便捷操作可以节约编简单SQL的时间。 然而,对于一些逻辑复杂、多表操作或动态SQL等情况,建议使用原生SQL来处理。mybatis-plus支持原生SQL的使用,通过原生SQL可以更灵活地满足这些复杂需求。 综上所述,通过整合mybatis-plus和mybatis-plus-generator,我们可以在开发中更高效地处理单表的增删改查功能,并且对于复杂的需求可以使用原生SQL来满足。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Spring cloud整合MyBatis-plus和mybatis-plus-generator](https://blog.csdn.net/cssweb_sh/article/details/123767029)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [mybatis-plus-generator(mybatisplus代码生成器篇)](https://blog.csdn.net/b13001216978/article/details/121690960)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值