RowBounds实现分页
工具类Mybatis
package Utils;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.IOException;
import java.io.InputStream;
public class Mybatis {
static SqlSessionFactory factory;
static {
InputStream inputStream = null;
String resources="mybatis-config.xml";
try{ inputStream = Resources.getResourceAsStream(resources);
factory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace(); } }
public static SqlSession openSession(){
return factory.openSession();
}}
Mapper.xml映射文件
<select id="getUserByRowBounds" resultType="pojo.EmployeeUser">
select * from employees;
</select>
Controller控制层调用方法实现查询
public class Menu {
public static void main(String[] args) {
Service_Fun fu_tn = new Service_impl() ;//引入接口
fu_tn.getUserByRowBounds(1,10);//调用方法并传值,开始staratIndex,结束endIndex
}
}
Service层接口
List<EmployeeUser> getUserByRowBounds(int staratIndex,int endIndex);
Service_impl
MapperEmployeeDao fun = new Funtion_impl();//引入接口
@Override
public List<EmployeeUser> getUserByRowBounds(int startIndex,int endIndex) {
return fun.getUserByRowBounds(startIndex,endIndex);
}
Dao层接口
List<EmployeeUser> getUserByRowBounds(int staratIndex,int endIndex);
Dao_impl
@Override
public List<EmployeeUser> getUserByRowBounds(int staratIndex,int endIndex) {
SqlSession sqlSession = Mybatis.openSession();
RowBounds rowBounds = new RowBounds(staratIndex, endIndex);
List<EmployeeUser> userList = sqlSession.selectList("Dao.MapperEmployeeDao.getUserByRowBounds",
null, rowBounds);
for (EmployeeUser user : userList
) {
System.out.println(user);
}
sqlSession.close();
return null;
}
实现效果