mybatis一对多分页查询

摘要:
今天遇到一个问题,当用户关联角色查询翻页时,如果一个用户有多个角色,会认为是多条记录,页面加载的时候就会发现记录数不对。

今天遇到一个问题,当用户关联角色查询翻页时,如果一个用户有多个角色,会认为是多条记录,页面加载的时候就会发现记录数不对。

为了解决这个问题,我打算分两次查询,第一次只按分页查询出当前页应该展示的用户id列表,然后根据这个idlist去关联查询用户和角色的所有要用到的字段

java
public Map<String, Object> selectPage(AccountVo params, intpageSize) {
        
        if (params.getCurrentPage() == null) {
            params.setCurrentPage(1);
        }
        
        if (pageSize <= 0) {
            pageSize =Const.PageSize;
        }
        
        PageHelper.startPage(params.getCurrentPage(), pageSize);
        
        AccountExample example = newAccountExample();
        Criteria criteria =example.createCriteria();
        
        if (null != params.getName() && !"".equals(params.getName())) {
            criteria.andNameLike("%" + params.getName() + "%");
        }
        
        example.setOrderByClause("a.id");
//example.setDistinct(true);
List<Long> idList =accountMapper.selectIdListByExample(example);
        PageInfo<Long> pageInfo = new PageInfo<Long>(idList);
        
        List<Account> list =accountMapper.selectWithRoleByIdList(idList);
        
        Map<String, Object> map = new HashMap<>();
        map.put("accountList", list);
        map.put("pageInfo", pageInfo);
        returnmap;
    }
/*** selectIdListByExample
     * selectWithRoleByIdList
     */List<Long>selectIdListByExample(AccountExample example);
    List<Account> selectWithRoleByIdList(List<Long> idList);
resultMap
<resultMap id="WithRoleResultMap"type="com.cetcht.entity.account.Account">
    <id column="id"jdbcType="BIGINT"property="id" />
    <result column="name"jdbcType="VARCHAR"property="name" />
    <result column="password"jdbcType="VARCHAR"property="password" />
    
    <collection property="roleIdList"ofType="java.lang.Long">
        <result column="role_id"jdbcType="BIGINT" />
    </collection>
    
    <collection property="roleList"ofType="com.cetcht.entity.account.Role">
        <id column="role_id"jdbcType="BIGINT"property="id" />
        <result column="role_name"jdbcType="VARCHAR"property="name" />
    </collection>
  </resultMap>
  
  <resultMap id="IdListResultMap"type="java.lang.Long">
    <result column="id"jdbcType="BIGINT" />
  </resultMap>
select
<select id="selectIdListByExample"parameterType="com.cetcht.entity.account.AccountExample"resultMap="IdListResultMap">select
    <if test="distinct">distinct
    </if>a.id
    from b_account a
    <if test="_parameter != null">
      <include refid="Example_Where_Clause_a" />
    </if>
    <if test="orderByClause != null">order by ${orderByClause}
    </if>
    <if test="limit != null">
      <if test="offset != null">limit ${offset}, ${limit}
      </if>
      <if test="offset == null">limit ${limit}
      </if>
    </if>
  </select>
  
  <select id="selectWithRoleByIdList"parameterType="java.util.ArrayList"resultMap="WithRoleResultMap">select
    <include refid="With_Role_Column_List" />from b_account a
    left join m_account_role m on a.id = m.account_id
    left join b_role r on m.role_id = r.id
    <where>a.id in (    
          <foreach collection="idList"item="id"index="index"separator=",">#{id}   
          </foreach>)    
    </where>order by a.id
  </select>
前端
account2page: function() {
      var that = this;

      Account.list(this.search).then(({ data }) =>{
        //console.log(data);
        var pageInfo =data.pageInfo;
        var accountList =data.accountList;

        that.accountList =accountList;

        that.pageSize =pageInfo.pageSize;
        that.search.currentPage =pageInfo.pageNum;
        that.rows = pageInfo.pages *pageInfo.pageSize;
      });
    },

免责声明:文章转载自《mybatis一对多分页查询》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇notepad++快捷键大全PHP(html)VSCode调试配置launch.json下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

(三)Mybatis类型转换器,接口传参类型,一对一,一对多查询resultMap配置

Mybatis类型转换器 首先明白什么时候用到它,当数据库的字段类型和java字段类型无法默认匹配时候进行转换,比如现在数据库类型是INTEGER,而java当中类型是Boolean,true表示1,false表示0,这时候你在执行sql语句插入或者查询获取结果集时,类型就会出现不匹配的情况,这时候我们只需要书写一个类型转换器,并进行配置,之后java遇到...

mybatis中@Param用法

用注解来简化xml配置的时候,@Param注解的作用是给参数命名,参数命名后就能根据名字得到参数值,正确的将参数传入sql语句中  我们先来看Mapper接口中的@Select方法 package Mapper; public interface Mapper { @Select("select s_id id,s_name n...

MyBatis与JDBC连接数据库所使用的url之间的差异

在Windows7 系统上安装了MySQL 8.0,然后创建Maven工程,配置pom.xml文件,添加了如下依赖: <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version&g...

Mybatis检查SQL注入

Mybatis 的 Mapper.xml 语句中 parameterType 向SQL语句传参有两种方式:#{ } 和 ${ }。 使用#{ }是来防止SQL注入,使用${ }是用来动态拼接参数。 如何排查出 1. 检查是否有$号 如果你使用的是ide代码编辑器,那么可以通过全局搜索${ , 快速定位到使用${ }拼接SQL的语句,在去找到外部传入参数的入...

mybatis父查询值嵌套传递/column传入多个参数值197

mybatis中collection的column传入多个参数值(使用父查询的映射值) property description column 数据库的列名或者列标签别名。与传递给resultSet.getString(columnName)的参数名称相同。注意: 在处理组合键时,您可以使用column=“{prop1=col1,prop2=co...

mybatis中的where

在多个查询条件下,由于需要拼接sql语句,所以会在前面加上 where 1 = 1   select id,name,gender,email from emp where 1 = 1 <if test="id != null and id != ''"> and id...