【mybatis-plus问题集锦系列】mybatis使用xml配置文件实现数据的基础增删改查

发布于:2025-02-11 ⋅ 阅读:(103) ⋅ 点赞:(0)

简单的数据查询,我们可以在mapper接口里面去实现,但是如果是复杂的查询,我们就可以使用xml配置文件去做,
官网链接xml配置文件
在这里插入图片描述

实现效果

在这里插入图片描述

实现代码

  • 根据mapper接口的包结构,在resources包里面新建同名同结构的xml文件

    mapper接口文件

@Mapper
public interface EmpMapper2 {
    public List<Emp> getAllEmpList();
}
  • 在这里插入图片描述
    在这里插入图片描述
    resources里面对应的xml文件
    namespace对应的是mapper接口的全类名
    select里面的id对应的就是mapper接口里面的方法名
    resultType 单条记录封装的类型
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- mamper接口的全类名-->
<mapper namespace="com.gaofeng.mapper.EmpMapper2">
<!-- resultType 单条记录封装的类型-->
    <select id="getAllEmpList" resultType="com.gaofeng.pojo.Emp">
        select * from tb_emp
    </select>
</mapper>
  • 查询测试
@SpringBootTest
public class mybatisTestxml {
   @Autowired
   private EmpMapper2 empMapper2;

    @Test
    public void getEmpList(){
        List<Emp> allEmpList = empMapper2.getAllEmpList();
        System.out.println(allEmpList);
    }
}
  • 条件查询

EmpMapper2文件

public List<Emp> listEmp(String name,Short gender, LocalDate begin,LocalDate end);

xml文件配置

 <select id="listEmp" resultType="com.itheima.pojo.Emp">
        select * from tb_emp where name like concat('%',#{name},'%') and gender = #{gender}
        and entrydate between #{begin} and #{end} order by update_time desc;
 </select>

测试文件

    @Test
    public void testGetEmp(){
        List<Emp> empList = empMapper2.listEmp("张", (short) 1, LocalDate.of(2010, 1, 1), LocalDate.of(2020, 1, 1));
        System.out.println(empList);
    }

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


网站公告

今日签到

点亮在社区的每一天
去签到