MyBatis-Plus完成链表分页查询

本文介绍了如何在Java中使用MyBatis框架编写DAO接口和Mapper映射文件,以实现Emp实体的联表分页查询,包括IPage和QueryWrapper的使用以及Spring的@Autowired注解的测试部分。

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

1.编写dao接口方法

public interface EmpDao extends BaseMapper<Emp> {
    IPage<Emp> selectPageWithDept(IPage<Emp> page, @Param("ew") Wrapper<Emp> queryWrapper);
}

2.编写mapper映射文件的内容

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--namespace必须和dao接口的名称一模一样-->
<mapper namespace="com.aaa.dao.EmpDao">
    <!--查询所有员工携带部门信息-->
    <resultMap id="baseEmpMapper" type="com.aaa.entity.Emp" autoMapping="true">
          <id property="id" column="id"/>
        <result property="deptId" column="dept_id"/>
        <association property="dept" javaType="com.aaa.entity.Dept" autoMapping="true">
             <id property="id" column="id"/>
             <result property="dname" column="d_name"/>
        </association>
    </resultMap>
    <select id="selectPageWithDept" resultMap="baseEmpMapper">
        select * from tbl_emp e join tbl_dept d on e.dept_id=d.id
        <if test="ew!=null and ew.customSqlSegment!=null">
             ${ew.customSqlSegment}
        </if>
    </select>
</mapper>

3.测试

@Autowired
    private EmpDao empDao;
    //联表分页条件查询
    @Test
    public void testSelectPageWithDept(){
        IPage<Emp> iPage=new Page(1,2);
        QueryWrapper<Emp> wrapper=new QueryWrapper<>();
//        wrapper.like("name","燕");
        IPage<Emp> empIPage = empDao.selectPageWithDept(iPage, wrapper);

        System.out.println("总页码:"+empIPage.getPages());
        System.out.println("总条数:"+empIPage.getTotal());
        System.out.println("当前页的记录:");
        List<Emp> records = empIPage.getRecords();
        for(Emp e:records){
            System.out.println("姓名:"+e.getName()+";薪水:"+e.getSalary()+";所在部门名称:"+e.getDept().getDname());
        }
    }

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值