1)StudentDao.java

/**
* 持久层*/
public class StudentDao {
/**
* 增加学生
*/
public void add(Student student) throws Exception{
SqlSession sqlSession = MyBatisUtil.getSqlSession();
try{
sqlSession.insert("mynamespace.add",student);
}catch(Exception e){
e.printStackTrace();
sqlSession.rollback();
throw e;
}finally{
sqlSession.commit();
MyBatisUtil.closeSqlSession();
}
}
/**
* 无条件分页查询学生
*/
public List<Student> findAllWithFy(int start,int size) throws Exception{
SqlSession sqlSession = MyBatisUtil.getSqlSession();
try{
Map<String,Integer> map = new LinkedHashMap<String,Integer>();
map.put("pstart",start);
map.put("psize",size);
return sqlSession.selectList("mynamespace.findAllWithFy",map);
}catch(Exception e){
e.printStackTrace();
sqlSession.rollback();
throw e;
}finally{
sqlSession.commit();
MyBatisUtil.closeSqlSession();
}
}
/**
* 有条件分页查询学生
*/
public List<Student> findAllByNameWithFy(String name,int start,int size) throws Exception{
SqlSession sqlSession = MyBatisUtil.getSqlSession();
try{
Map<String,Object> map = new LinkedHashMap<String,Object>();
map.put("pname","%"+name+"%");
map.put("pstart",start);
map.put("psize",size);
return sqlSession.selectList("mynamespace.findAllByNameWithFy",map);
}catch(Exception e){
e.printStackTrace();
sqlSession.rollback();
throw e;
}finally{
sqlSession.commit();
MyBatisUtil.closeSqlSession();
}
}
public static void main(String[] args) throws Exception{
StudentDao dao = new StudentDao();
System.out.println("-----------Page1");
for(Student s:dao.findAllByNameWithFy("哈",0,3)){
System.out.println(s.getId()+":"+s.getName()+":"+s.getSal());
}
System.out.println("-----------Page2");
for(Student s:dao.findAllByNameWithFy("哈",3,3)){
System.out.println(s.getId()+":"+s.getName()+":"+s.getSal());
}
System.out.println("-----------Page3");
for(Student s:dao.findAllByNameWithFy("哈",6,3)){
System.out.println(s.getId()+":"+s.getName()+":"+s.getSal());
}
System.out.println("-----------Page4");
for(Student s:dao.findAllByNameWithFy("哈",9,3)){
System.out.println(s.getId()+":"+s.getName()+":"+s.getSal());
}
}
}

Normal
0

7.8 磅
0
2

false
false
false

EN-US
ZH-CN
X-NONE

/* Style Definitions */
table.MsoNormalTable
{mso-style-name:普通表格;
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-priority:99;
mso-style-qformat:yes;
mso-style-parent:"";
mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
mso-para-margin:0cm;
mso-para-margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:10.5pt;
mso-bidi-font-size:11.0pt;
font-family:"Calibri","sans-serif";
mso-ascii-font-family:Calibri;
mso-ascii-theme-font:minor-latin;
mso-hansi-font-family:Calibri;
mso-hansi-theme-font:minor-latin;
mso-font-kerning:1.0pt;}

2)StudentMapper.xml

<?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="mynamespace">
<insert id="add" parameterType="loaderman.Student">
insert into students(id,name,sal) values(#{id},#{name},#{sal})
</insert>
<select id="findAllWithFy" parameterType="map" resultType="loaderman.Student">
select id,name,sal from students limit #{pstart},#{psize}
</select>
<select id="findAllByNameWithFy" parameterType="map" resultType="loaderman.Student">
select id,name,sal from students where name like #{pname} limit #{pstart},#{psize}
</select>
</mapper>

/**

*持久层

*@authorAdminTC

*/

publicclass StudentDao {

/**

*增加学生

*/

publicvoid add(Student student) throws Exception{

SqlSession sqlSession = MyBatisUtil.getSqlSession();

try{

sqlSession.insert("mynamespace.add",student);

}catch(Exception e){

e.printStackTrace();

sqlSession.rollback();

throw e;

}finally{

sqlSession.commit();

MyBatisUtil.closeSqlSession();

}

}

/**

*无条件分页查询学生

*/

public List<Student> findAllWithFy(int start,int size) throws Exception{

SqlSession sqlSession = MyBatisUtil.getSqlSession();

try{

Map<String,Integer> map = new
LinkedHashMap<String,Integer>();

map.put("pstart",start);

map.put("psize",size);

return sqlSession.selectList("mynamespace.findAllWithFy",map);

}catch(Exception e){

e.printStackTrace();

sqlSession.rollback();

throw e;

}finally{

sqlSession.commit();

MyBatisUtil.closeSqlSession();

}

}

/**

*有条件分页查询学生

*/

public List<Student> findAllByNameWithFy(String name,int start,int size) throws Exception{

SqlSession sqlSession = MyBatisUtil.getSqlSession();

try{

Map<String,Object> map = new LinkedHashMap<String,Object>();

map.put("pname","%"+name+"%");

map.put("pstart",start);

map.put("psize",size);

return sqlSession.selectList("mynamespace.findAllByNameWithFy",map);

}catch(Exception e){

e.printStackTrace();

sqlSession.rollback();

throw e;

}finally{

sqlSession.commit();

MyBatisUtil.closeSqlSession();

}

}

publicstaticvoid main(String[] args) throws Exception{

StudentDao dao = new StudentDao();

System.out.println("-----------Page1");

for(Student s:dao.findAllByNameWithFy("哈",0,3)){

System.out.println(s.getId()+":"+s.getName()+":"+s.getSal());

}

System.out.println("-----------Page2");

for(Student s:dao.findAllByNameWithFy("哈",3,3)){

System.out.println(s.getId()+":"+s.getName()+":"+s.getSal());

}

System.out.println("-----------Page3");

for(Student s:dao.findAllByNameWithFy("哈",6,3)){

System.out.println(s.getId()+":"+s.getName()+":"+s.getSal());

}

System.out.println("-----------Page4");

for(Student s:dao.findAllByNameWithFy("哈",9,3)){

System.out.println(s.getId()+":"+s.getName()+":"+s.getSal());

}

}

}

Normal
0

7.8 磅
0
2

false
false
false

EN-US
ZH-CN
X-NONE

/* Style Definitions */
table.MsoNormalTable
{mso-style-name:普通表格;
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-priority:99;
mso-style-qformat:yes;
mso-style-parent:"";
mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
mso-para-margin:0cm;
mso-para-margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:10.5pt;
mso-bidi-font-size:11.0pt;
font-family:"Calibri","sans-serif";
mso-ascii-font-family:Calibri;
mso-ascii-theme-font:minor-latin;
mso-hansi-font-family:Calibri;
mso-hansi-theme-font:minor-latin;
mso-font-kerning:1.0pt;}

mybatis之分页查询的更多相关文章

  1. Mybatis包分页查询java公共类

    Mybatis包分页查询java公共类   分页----对于数据量非常大的查询中.是不可缺少的. mybatis底层的分页sql语句因为须要我们自己去手动写.而实现分页显示的时候我们须要依据分页查询条 ...

  2. springmvc+mybatis 实现分页查询

    为简化分页功能,设计了一个分页的JSP标签,只需要在页面使用分页标签,就可以完成所有页面的分页功能. 1. 项目结构和数据库设计 (1) 项目结构: (2) 数据库设计 2. PageModel.ja ...

  3. spring-boot 集合mybatis 的分页查询

    spring-boot 集合mybatis 的github分页查询 一.依赖包 <!-- mysql 数据库驱动. --> <dependency> <groupId&g ...

  4. 使用mybatis实现分页查询示例代码分析

    *******************************************分页查询开始*************************************************** ...

  5. SpringBoot整合Mybatis关于分页查询的方法

    最近公司在用到SpringBoot整合Mybatis时当web端页面数据增多时需要使用分页查询以方便来展示数据.本人对分页查询进行了一些步骤的总结,希望能够帮助到有需要的博友.如有更好的方式,也希望评 ...

  6. Mybatis 使用分页查询亿级数据 性能问题 DB使用ORACLE

    一般用到了mybatis框架分页就不用自己写了 直接用RowBounds对象就可以实现,但这个性能确实很低 今天我用到10w级得数据分页查询,到后面几页就迭代了很慢 用于记录 1.10万级数据如下 [ ...

  7. mybatis中分页查询

    1 如果在查询方法中有多个参数,可以使用map对象将所有数据都存储进去.比如分页查询,需要用到两个参数,可以将这两个参数包装到map中. 例子:分页查询 dao层方法 public List<S ...

  8. Mybatis的分页查询

    示例1:查询业务员的联系记录 1.控制器代码(RelationController.java) //分页列出联系记录 @RequestMapping(value="toPage/custom ...

  9. MyBatis学习总结(12)——Mybatis+Mysql分页查询

    package cn.tsjinrong.fastfile.util; /**  * @ClassName: Page  * @Description: TODO(分页组件的父类,用来封装分页的 通用 ...

随机推荐

  1. 移动端设备管理平台 atx server2实践

    目录 1.需求背景 2.初步调研 2.1.云测试平台 2.2.开源工具 2.3.VNC 2.4.企业内部自研云测试平台 3.ATX Server安装 依赖环境 安装rethinkdb 安装atx se ...

  2. liunx加载JX2410标准配置文件

  3. javascript只弹出一次框 再次刷新不弹出

    .打开页面自动弹出  当关闭弹框的时候  设置cookie生存时间  再次刷新页面判断cookie是否失效 <html> <head> <meta charset=&qu ...

  4. Python&Selenium借助HTMLTestRunner生成自动化测试报告

    一.摘要 本篇博文介绍Python和Selenium进行自动化测试时,借助著名的HTMLTestRunner生成自动化测试报告 HTMLTestRunner.py百度很多,版本也很多,自行搜索下载放到 ...

  5. ActiveMQ部署和代码尝试(二)

    部署和代码尝试 1. 部署在linux 上的acvtiveMQ 要可以通过前台windows 的页面访问,必须把linux 的IP和 windows的 IP 地址配置到同一个网关下 .这种情况一般都是 ...

  6. Circular view path [mydemo]: would dispatch back to the current handler URL [/mydemo] again. Check your ViewResolver setup!

    简单创建一个springboot工程 pom.xml <?xml version="1.0" encoding="UTF-8"?><proje ...

  7. IDEA中配置Jetty Server

    首先去 Eclipse官网下载Jetty jar包 鼠标移到Jetty上时 点击 Git it (得到它) 点击 .zip等待下载完成 然后 解压出来 接下就让我们 开始 使用IDEA了(创建一个We ...

  8. spark_rdd 一波怼完面试官系列

    Resilient Distributed dataset , 弹性分布式数据集. 分布式内存的抽象使用,实现了以操作本地集合的方式来操作分布式数据集的抽象实现. RDD是Spark最核心的东西,它表 ...

  9. Hivesql中的正则

    ================================================================================================= 一般 ...

  10. 01_初识redis

    1.redis和mysql mysql是一个软件,帮助开发者对一台机器的硬盘进行操作. redis是一个软件,帮助开发者对一台机器的内存进行操作 汽车之家,如果硬盘挂掉了,页面还能访问1个月 关键字: ...