Mybatis配置分页

测试

package com.sky.test;

import com.sky.controller.UserController;
import com.sky.pojo.Emp;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.List;

public class Demo {

    public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserController userController = (UserController) applicationContext.getBean("userController");
        List<Emp> emps = userController.getEmps(2,5);
        System.out.println(emps);
    }

}

controller

package com.sky.controller;


import com.sky.pojo.Emp;
import com.sky.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import java.util.List;

@Controller
public class UserController {

    @Autowired
    private UserService userService;

    public List<Emp> getEmps(int pageNum,int pageSize){
        return userService.getEmps(pageNum,pageSize);
    }

}

service

package com.sky.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sky.mapper.EmpMapper;
import com.sky.pojo.Emp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserService {

    @Autowired
    private EmpMapper empMapper;

    public List<Emp> getEmps(int pageNum,int pageSize) {
//        开启分页查询
//        pageNum:第几页 ; pageSize:一页有几条数据
        PageHelper.startPage(pageNum,pageSize);
        List<Emp> emps = empMapper.getEmps();
//        用PageInfo对结果进行封装
//        PageInfo包含非常全面的分页属性
        PageInfo page = new PageInfo(emps);
        System.out.println(page);
        return emps;
    }
}

在这里插入图片描述

mapper

package com.sky.mapper;

import com.sky.pojo.Emp;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface EmpMapper {

    List<Emp> getEmps();
}

mapperxml


DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.sky.mapper.EmpMapper">

  <select id="getEmps" resultType="Emp">
      select * from emp
  select>
  
mapper>

spring、Mybatis选一个配置
MybatisConfig.xml配置分页


DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">


<configuration>

    <settings>

        <setting name="logImpl" value="STDOUT_LOGGING"/>

        <setting name="mapUnderscoreToCamelCase" value="true"/>

        <setting name="cacheEnabled" value="true" />
    settings>

    <typeAliases>
        <package name="com.sky.pojo"/>
    typeAliases>

    
    <plugins>
        
        <plugin interceptor="com.github.pagehelper.PageHelper">
            
            <property name="dialect" value="mysql"/>
            
            
            
            <property name="offsetAsPageNum" value="true"/>
            
            
            <property name="rowBoundsWithCount" value="true"/>
            
            
            <property name="pageSizeZero" value="true"/>
            
            
            
            <property name="reasonable" value="true"/>
        plugin>
    plugins>

configuration>

spring配置分页

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  <property name="dataSource" ref="dataSource"/>
  <property name="mapperLocations">
    <array>
      <value>classpath:mapper/*.xmlvalue>
    array>
  property>
  <property name="typeAliasesPackage" value="com.isea533.ssm.model"/>
  <property name="plugins">
    <array>
      <bean class="com.github.pagehelper.PageHelper">
        <property name="properties">
          <value>
            dialect=hsqldb
            reasonable=true
          value>
        property>
      bean>
    array>
  property>
bean>

你可能感兴趣的:(java,开发语言)