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

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 测试类查询1对象

// 根据 ID 查询员工
@Test
void testGetbyId() {
    Emp emp = empMapper.getById(19);
    System.out.println(emp);
}

但是发现有的数据没被封装进来,可是数据都是有值的

在这里插入图片描述
在这里插入图片描述

Mybatis的数据封装

实体类属性名 和 数据库表查询返回的字段名一致,Mybatis会自动封装
如果实体类属性名 和 数据库表查询返回的字段名不一致,不能自动封装

比如我们的实例类和SQL表中的字段不一样

在这里插入图片描述
在这里插入图片描述

1. 给字段起别名
// 给字段起别名
不一样的字段为 dept_id create_time update_time,类中字段为驼峰,SQL表字段为下划线分隔

@Select("select id, username, password, name, gender, image, job, entrydate," +
        "dept_id deptId, create_time createTime, update_time updateTime from emp where id = #{id}")
Emp getById(Integer id);

在这里插入图片描述

2. 通过@Results,@Results注释手动映射封装
@Results({
        @Result(column = "dept_id", property = "deptId"),
        @Result(column = "create_time", property = "createTime"),
        @Result(column = "update_time", property = "updateTime")
})
@Select("select * from emp where id = #{id}")
Emp getById(Integer id);

在这里插入图片描述

3. 开启Mybatis的驼峰命名自动映射开关(经典最终方案)

application.properties 中定义 mybatis.configuration.map-underscore-to-camel-case=true

在这里插入图片描述
再使用

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

在这里插入图片描述

基础操作 - 条件查询

NULL

相关推荐

  1. Mybatis增删基础

    2024-03-27 14:10:02       34 阅读
  2. MyBaties-增删

    2024-03-27 14:10:02       40 阅读
  3. Mybatis增删

    2024-03-27 14:10:02       45 阅读
  4. Mybatis增删

    2024-03-27 14:10:02       30 阅读
  5. Mybatis批量增删

    2024-03-27 14:10:02       34 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-27 14:10:02       19 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-27 14:10:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-27 14:10:02       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-27 14:10:02       20 阅读

热门阅读

  1. 【Postman】如何给请求的参数设置随机数

    2024-03-27 14:10:02       20 阅读
  2. excel创建和部分使用

    2024-03-27 14:10:02       17 阅读
  3. 数据结构链栈实现(c语言)

    2024-03-27 14:10:02       17 阅读
  4. 软件工程的相关知识点

    2024-03-27 14:10:02       18 阅读
  5. 使用 React Hooks 管理状态和引用

    2024-03-27 14:10:02       15 阅读
  6. Web开发:深入探讨React Hooks的使用和最佳实践

    2024-03-27 14:10:02       17 阅读
  7. mysql怎么创建索引?

    2024-03-27 14:10:02       15 阅读
  8. Kotlin object

    2024-03-27 14:10:02       14 阅读