1 首先需要在mybatis的配置文件SqlMapConfig.xml文件中配置pagehelper插件
<plugins> <plugin interceptor="com.github.pagehelper.PageHelper">
<!--配置数据库的方言--> <property name="dialect" value="mysql"/> </plugin> </plugins>
2 在spring配置文件中添加
<!--配置mybatis --> <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml"></property> <property name="dataSource" ref="dataSource"></property> </bean>
3 使用步骤
/**
*page为页码,rows为每页要显示多少条数据
*/
@Override public DatagridBean selectAllUser(Integer page, Integer rows) throws Exception { PageHelper.startPage(page, rows); /*UserExample example = new UserExample(); List<User> users = userMapper.selectByExample(example);*/ List<User> users = customUserMapper.selectAllUsers(); PageInfo<User> pageInfo = new PageInfo<>(users); DatagridBean result = new DatagridBean(); result.setTotal(pageInfo.getTotal()); result.setRows(pageInfo.getList()); return result; }