MyBatis-Plus完成链表分页查询

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());
        }
    }

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
MyBatis-Plus是一个基于MyBatis的增强工具,提供了许多便捷的功能来简化开发。其中,MyBatis-Plus-join是MyBatis-Plus的一个扩展模块,用于支持关联查询。 在使用MyBatis-join的依赖。可以在项目的pom.xml文件中添加如下依赖: ```xml <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-extension</artifactId> <version>3.4.3.1</version> </dependency> ``` 2. 在实体类中定义关联关系。使用MyBatis-Plus-join时,需要在实体类中定义关联关系,可以使用`@TableField`注解来指定关联字段。例如: ```java public class User { private Long id; private String name; @TableField(exist = false) private List<Role> roles; // getter and setter } public class Role { private Long id; private String roleName; // getter and setter } ``` 3. 编写Mapper接口。在Mapper接口中,可以使用`@Select`注解来编写关联查询的SQL语句。例如: ```java @Mapper public interface UserMapper extends BaseMapper<User> { @Select("SELECT u.*, r.* FROM user u LEFT JOIN user_role ur ON u.id = ur.user_id LEFT JOIN role r ON ur.role_id = r.id") @Results({ @Result(property = "id", column = "id"), @Result(property = "name", column = "name"), @Result(property = "roles", column = "id", many = @Many(select = "com.example.mapper.RoleMapper.selectByUserId")) }) List<User> selectUserWithRoles(Page<User> page); } ``` 4. 编写关联查询的子查询。在上面的例子中,使用了`@Many`注解来指定关联查询的子查询方法。例如: ```java @Mapper public interface RoleMapper extends BaseMapper<Role> { @Select("SELECT r.* FROM role r LEFT JOIN user_role ur ON r.id = ur.role_id WHERE ur.user_id = #{userId}") List<Role> selectByUserId(Long userId); } ``` 5. 调用分页查询方法。在Service层或者Controller层中,可以调用Mapper接口中定义的分页查询方法来进行查询。例如: ```java @Service public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Override public IPage<User> getUserWithRoles(Page<User> page) { return userMapper.selectUserWithRoles(page); } } ``` 以上就是使用MyBatis-Plus-join进行分页查询的基本步骤。通过定义关联关系和编写关联查询的SQL语句,可以方便地进行关联查询并实现分页功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值