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;}

最新文章

  1. C#开发笔记
  2. 【Jsch】使用SSH协议连接到远程Shell执行脚本
  3. bottlepy template
  4. ThreadPool原理介绍
  5. windos多线程编程
  6. 用block响应button的点击事件
  7. POJ 2421 Constructing Roads (最小生成树)
  8. PHP -Session 深入解剖 ① session的基本操作 【大成出品 --必是精品】。
  9. angularjs filter 详解
  10. Mysql找回管理员password
  11. [HDU 2102] A计划(搜索题,典型dfs or bfs)
  12. 论如何把JS踩在脚下 —— JQuery基础及Ajax请求详解
  13. redis可视化工具redisClient
  14. c#接口和抽象类比较
  15. 常用的js正则验证整理
  16. rem 自适应、整体缩放
  17. Linux内核设计与实现 第四章
  18. vue教程1-08 交互 get、post、jsonp
  19. Visual Leak Detector简明使用教程
  20. SQLServer创建用户、数据库、表、约束、存储过程、视图

热门文章

  1. PHP扩展模块php_igbinary和php_redis的安装
  2. yum list报一些error的组件
  3. Python 列出 windows 安装的软件
  4. Spring 注解无效 空指针异常
  5. JavaScript(JS)入门篇
  6. Gym - 101981B Tournament (WQS二分+单调性优化dp)
  7. Mybayis的项目使用的Mapping文件使用总结参考(一)
  8. 【bzoj3083】遥远的国度(树链剖分+线段树)
  9. BZOJ 2178: 圆的面积并 (辛普森积分)
  10. [Cypress] Use the Most Robust Selector for Cypress Tests