【Mybatis 基础】增删改查(@Insert, @Delete, @Update, @Select)

本文详细介绍了MyBatis的基础操作,包括删除、插入、更新、查询和条件查询,还提及了数据封装的方法。同时,阐述了XML映射文件的使用,以及动态SQL中if、where、foreach、sql和include等标签的应用,帮助开发者更好地掌握MyBatis。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Mybatis用法

基础操作 - 删除

delete 传参

@Mapper
public interface EmpMapper {
   
   

    // 根据ID动态删除数据
    @Delete("delete from emp where id = #{id}") // Mybatis提供的参数占位符 #{param}
    public void delete(Integer id);
}

SpringbootMybatisCrudApplicationTests 测试类删除

@SpringBootTest
class SpringbootMybatisCrudApplicationTests {
   
   

    @Autowired
    private EmpMapper empMapper;

    @Test
    void testDelete() {
   
   
        empMapper.delete(16);
    }
}

预编译SQL

application.properties

#配置mybatis日志输出位置,输出到控制台
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

再启动测试类,控制台就会输出对应日志,这就叫做预编译SQL

==>  Preparing: delete from emp where id = ?
==> Parameters: 15(Integer)
<==    Updates: 0

基础操作 - 插入

Insert 插入

// 新增员工
@Insert("insert into emp(username, name, gender, image, job, entrydate, dept_id, create_time, update_time)\n" +
        "           values (#{username}, #{name}, #{gender}, #{image}, #{job}, #{entrydate}, #{deptId}, #{createTime}, #{updateTime})")
void insert(Emp emp);

SpringbootMybatisCrudApplicationTests 测试类插入对象

@Test
void testInsert() {
   
   
    // 构造员工对象
    Emp emp = new Emp();
    emp.setUsername("Tom");
    emp.setName("汤姆");
	emp.setImage("1.jpg");
	emp.setGender((short) 1);
	emp.setJob((short) 1);
	emp.setEntrydate(LocalDate.of(2005, 1, 1));
	emp.setCreateTime(LocalDateTime.now());
	emp.setUpdateTime(LocalDateTime.now());
	emp.setDeptId(1);

	// 调用员工Mapper接口的insert方法
	empMapper.insert(emp);
}

主键返回

// 新增员工
@Options(useGeneratedKeys = true, keyProperty = "id")
@Insert("insert into emp(username, name, gender, image, job, entrydate, dept_id, create_time, update_time)\n" +
        "           values (#{username}, #{name}, #{gender}, #{image}, #{job}, #{entrydate}, #{deptId}, #{createTime}, #{updateTime})")
void insert(Emp emp);


// 测试类
@Test
void testInsert() {
   
   
	// 构造员工对象
	Emp emp = new Emp();
	emp.setUsername("Tom2");
	emp.setName("汤姆2");
	emp.setImage("1.jpg");
	emp.setGender((short) 1);
	emp.setJob((short) 1);
	emp.setEntrydate(LocalDate.of(2005, 1, 1));
	emp.setCreateTime(LocalDateTime.now());
	emp.setUpdateTime(LocalDateTime.now());
	emp.setDeptId(1);
	// 调用员工Mapper接口的insert方法
	empMapper.insert(emp);
	System.out.println(emp.getId());
}

@Options(useGeneratedKeys = true, keyProperty = "id") 注解是 MyBatis 框架中的一个注解,它用于 MyBatis 映射器方法上,其目的是在执行 insert 操作后,能够将数据库生成的主键值回写到之前插入数据的实体对象中。

解释这个注解的各部分:

  • useGeneratedKeys: 这个属性设为 true,表示我们希望使用数据库自动生成的键值(例如:自动递增的 ID)。
  • keyProperty: 该属性指定了哪一个属性或字段应该被填充。通常,这个属性会被设置为实体类中代表主键的属性名。

基础操作 - 更新

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}")
void update(Emp emp);

SpringbootMybatisCrudApplicationTests 测试类更新对象

// 更新员工
@Test
void testUpdate() {
   
   
    // 构造员工对象
    Emp emp = new Emp();
    emp.setId(1);
    emp.setUsername("TomTOPONE");
    emp.setName("汤姆1111111");
    emp.setImage("1.jpg");
    emp.setGender((short) 1);
    emp.setJob((short) 1);
    emp.setEntrydate(LocalDate.of(2000, 1, 1));
    emp.setUpdateTime(LocalDateTime.now());
    emp.setDeptId(1);
    // 调用员工Mapper接口的update方法
    empMapper.update(emp);
}

基础操作 - 查询

SELECT 查询

// 根据Id查询员工
@Select("select * from emp where id = #{id}")
Emp getById(Integer id);

SpringbootMybatisCrudApplicationTests 测试类查询对象

// 根据 ID 查询员工
@Test
void testGetbyId() {
   
   
    Emp emp = empMapper.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

符韬OvO

你真是个富哥

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值