【无标题】

标题 mybatis plus分页 、 xml中多表联合条件查询

mybatis plus分页 、 xml中多表联合条件查询

mybatis plus分页 、 xml中多表联合条件查询

其实就三步

  1. xxxxMapper.java 中声明定义
  2. xxxMapper.xml 中实现具体逻辑
  3. xxxxController.class中调用

导入mybatis plus分页插件

@Configuration
@MapperScan("com.******.mapper")
public class MybatisPlusConfig {


    /**
     * 添加分页插件
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));//如果配置多个插件,切记分页最后添加
        //interceptor.addInnerInterceptor(new PaginationInnerInterceptor()); 如果有多数据源可以不配具体类型 否则都建议配上具体的DbType
        return interceptor;
    }
}

编写xxxMapper.xml

注意位置要和xxxMapper保持一致(resource下)
如果xxxMapper.xml文件夹位置和xxxxMapper.java不一致,则需要在yml中进行配置

<?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">
<mapper namespace="com.******.mapper.UserinfoMapper">



    <resultMap id="toUserVo" type="com.******.entity.vo.UserVo">
        <id column="user_id" property="userId"/>
        <result column="user_name" property="userName"></result>

        <result column="dep_id" property="depId"></result>
        <result column="dep_name" property="depName"></result>
		<...........>
    </resultMap>


    <select id="selectUserVoPage" resultMap="toUserVo">
        select user_id, user_name,
        dep_id, dep_name,
        **********
        from userinfo INNER JOIN depInfo on userinfo.user_depid = depinfo.dep_id
        INNER JOIN roleinfo on userinfo.user_roleid = roleinfo.role_id
        where 1=1
        <if test="search != null and search != ''">
            and user_name like concat('%', #{search}, '%')
        </if>
        <if test="depId != null and depId != -1">
            and dep_id = #{depId}
        </if>
    </select>
</mapper>

编写xxxxMapper.java

Page<UserVo> selectUserVoPage(Page<UserVo> page, @Param("search") String search, @Param("depId") Integer depId);

xxxxController.class中调用

    @GetMapping("/selectUserVoPage")
    public R<?> selectUserVoPage(@RequestParam(defaultValue = "1") Integer pageNum,
                                 @RequestParam(defaultValue = "10") Integer pageSize,
                                 @RequestParam(defaultValue = "") String search,
                                 @RequestParam(defaultValue = "") Integer depId) {

        log.info("进入/user/selectUserVoPage中");
        Page<UserVo> page = new Page<>(pageNum, pageSize);
        Page<UserVo> userVoPage = userinfoMapper.selectUserVoPage(page, search, depId);


        return R.success(userVoPage);
    }

注意:假设现在又不需要进行分页了,只需要pageSize=-1即可;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值