详细介绍:二十五、Mybatis-基础操作-新增、更新、查询、条件查询

新增员工

新增员工

主键返回

主键返回

对应EmpMapper代码块:

//新增员工
@Options(useGeneratedKeys = true,keyProperty = "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);

对应SpringbootMybatisCrudApplicationTests(测试类)代码块:

@Test
public 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(2000,1,1));
emp.setCreateTime(LocalDateTime.now());
emp.setUpdateTime(LocalDateTime.now());
emp.setDeptId(1);
//执行新增员工信息操作
empMapper.insert(emp);
System.out.println(emp.getId());
}

结果

结果

结果

新增总结

新增总结

更新操作

更新操作

对应EmpMapper代码块:

//更新员工
@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);

对应SpringbootMybatisCrudApplicationTests(测试类)代码块:

//更新员工
@Test
public void testUpdate(){
//构造员工对象
Emp emp = new Emp();
emp.setId(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());
emp.setDeptId(1);
//执行更新员工操作
empMapper.update(emp);
}

结果

结果

查询

查询

数据封装

数据封装

数据封装总结(推荐使用开启mybatis的驼峰命名规则映射,但前提是数据库中的字段名严格遵守下划线命名即a_column)

数据封装总结

对应application.properties(配置类)代码块:开启mybatis的驼峰命名自动映射开关的语句

#开启mybatis的驼峰命名自动映射开关:a_Column------>aCloumn
mybatis.configuration.map-underscore-to-camel-case=true

对应EmpMapper代码块:

//方案三: 开启mybatis的驼峰命名自动映射开关 --- a_cloumn ------> aColumn
//根据ID查询员工
@Select("select * from emp where id = #{id}")
public Emp getById(Integer id);

对应SpringbootMybatisCrudApplicationTests(测试类)代码块:

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

结果

查询结果

条件查询

条件查询

参数名说明(了解即可,以后看到能知道原因即可)

参数名说明(了解即可,以后看到能知道原因即可)

对应EmpMapper代码块:

//条件查询员工
@Select("select * from emp where name like concat('%',#{name},'%') and gender = #{gender} and " +
"entrydate between #{begin} and #{end} order by update_time desc ")
public List<
Emp> list(@Param("name") String name, @Param("gender") Short gender, @Param("begin") LocalDate begin ,@Param("end") LocalDate end);

对应SpringbootMybatisCrudApplicationTests(测试类)代码块:

//根据条件查询员工
@Test
public void testList(){
List<
Emp> empList = empMapper.list("张", (short) 1, LocalDate.of(2010, 1, 1), LocalDate.of(2020, 1, 1));
System.out.println(empList);
}

结果

查询结果

完整代码:

项目目录结构

项目目录结构

application.properties(配置)代码

#下面这些内容是为了让MyBatis映射
#指定MybatisMapper文件
mybatis.mapper-locations=classpath:mappers/*xml
#指定Mybatis的实体目录
mybatis.type-aliases-package=com.itheima.mybatis.entity
#驱动类名称
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#数据库连接的url
spring.datasource.url=jdbc:mysql://localhost:3306/db02
#连接数据库的用户名
spring.datasource.username=root
#连接数据库的密码
spring.datasource.password=root
#配置mybatis的日志,指定输出到控制台
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
#开启mybatis的驼峰命名自动映射开关:a_Column------>aCloumn
mybatis.configuration.map-underscore-to-camel-case=true

EmpMapper(接口)代码

package com.itheima.mapper;
import com.itheima.pojo.Emp;
import org.apache.ibatis.annotations.*;
import java.time.LocalDate;
import java.util.List;
@Mapper
public interface EmpMapper {
//根据ID删除数据
@Delete("delete from emp where id=#{id}")
public void delete(Integer id);
//新增员工
@Options(useGeneratedKeys = true,keyProperty = "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);
//更新员工
@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);
//方案三: 开启mybatis的驼峰命名自动映射开关 --- a_cloumn ------> aColumn
//根据ID查询员工
@Select("select * from emp where id = #{id}")
public Emp getById(Integer id);
//条件查询员工
@Select("select * from emp where name like concat('%',#{name},'%') and gender = #{gender} and " +
"entrydate between #{begin} and #{end} order by update_time desc ")
public List<
Emp> list(@Param("name") String name, @Param("gender") Short gender, @Param("begin") LocalDate begin ,@Param("end") LocalDate end);
}

Emp(实体类)代码

package com.itheima.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.time.LocalDate;
import java.time.LocalDateTime;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Emp
{
private Integer id;
private String username;
private String password;
private String name;
private Short gender;
private String image;
private Short job;
private LocalDate entrydate;
private Integer deptId;
private LocalDateTime createTime;
private LocalDateTime updateTime;
}

SpringbootMybatisCrudApplicationTests(测试类)代码

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;
import java.util.List;
@SpringBootTest
class SpringbootMybatisCrudApplicationTests
{
@Autowired
private EmpMapper empMapper;
@Test
public void testDelete(){
empMapper.delete(17);
}
@Test
public 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(2000,1,1));
emp.setCreateTime(LocalDateTime.now());
emp.setUpdateTime(LocalDateTime.now());
emp.setDeptId(1);
//执行新增员工信息操作
empMapper.insert(emp);
System.out.println(emp.getId());
}
//更新员工
@Test
public void testUpdate(){
//构造员工对象
Emp emp = new Emp();
emp.setId(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());
emp.setDeptId(1);
//执行更新员工操作
empMapper.update(emp);
}
//根据ID查询员工
@Test
public void testGetById(){
Emp emp = empMapper.getById(18);
System.out.println(emp);
}
//根据条件查询员工
@Test
public void testList(){
List<
Emp> empList = empMapper.list("张", (short) 1, LocalDate.of(2010, 1, 1), LocalDate.of(2020, 1, 1));
System.out.println(empList);
}
}
posted @ 2025-08-16 15:36  yfceshi  阅读(16)  评论(0)    收藏  举报