mapper分页排序指定字段查询模板
StudentQuery:
package Student; import java.util.ArrayList;
import java.util.List; public class StudentQuery {
private Integer id;
private String no;
private String name;
private String age;
private float score;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getNo() {
return no;
}
public void setNo(String no) {
this.no = no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public float getScore() {
return score;
}
@Override
public String toString() {
return "StudentQuery [id=" + id + ", no=" + no + ", name=" + name
+ ", age=" + age + ", score=" + score + ", fields=" + fields
+ "]";
}
public void setScore(float score) {
this.score = score;
} private String fields;
public String getFields() {
return fields;
}
public void setFields(String fields) {
this.fields = fields;
} public class FieldOrder{
private String field; //id , name imgUrl
private String order; // desc asc public FieldOrder(String field, String order) {
super();
this.field = field;
this.order = order;
}
public String getField() {
return field;
}
public void setField(String field) {
this.field = field;
}
public String getOrder() {
return order;
}
public void setOrder(String order) {
this.order = order;
}
} //orderby 集合
private List<FieldOrder> fieldOrders = new ArrayList<FieldOrder>(); //按照Id排序
public void orderbyId(boolean isAsc){
fieldOrders.add(new FieldOrder("id",isAsc == true ? "asc" : "desc"));
} private Integer pageNo = 1;
private Integer startRow;
private Integer pageSize = 10;
public Integer getPageNo() {
return pageNo;
}
public void setPageNo(Integer pageNo) {
this.startRow = (pageNo - 1) * pageSize;
this.pageNo = pageNo;
}
public Integer getStartRow() {
return startRow;
}
public void setStartRow(Integer startRow) {
this.startRow = startRow;
}
public Integer getPageSize() {
return pageSize;
}
public void setPageSize(Integer pageSize) {
this.startRow = (pageNo - 1) * pageSize;
this.pageSize = pageSize;
}
}
mapper语句
<sql id="studentSelector">
select
<if test="fields != null">
${fields}
</if>
<if test="fields == null">
id, no, name, age, score
</if>
from student
</sql> <sql id="studentOrderBy">
<if test="fieldOrders != null and fieldOrders.size > 0">
order by
<foreach collection="fieldOrders" item="fieldOrder" separator=",">
${fieldOrder.field} ${fieldOrder.order}
</foreach>
</if>
</sql> <sql id="studentLimit">
<if test="startRow != null">
limit ${startRow}, ${pageSize}
</if>
</sql> <select id="getStudentList" parameterType="StudentQuery" resultMap="BaseResultMap">
<include refid="studentSelector"></include>
<include refid="studentOrderBy"></include>
<include refid="studentLimit"></include>
</select>
test
public class Test {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext-dao.xml");
StudentMapper studentMapper = (StudentMapper) ctx.getBean("studentMapper");
StudentQuery studentQuery = new StudentQuery();
studentQuery.setFields("id, name");
studentQuery.orderbyId(false);
studentQuery.setPageNo(2);
studentQuery.setPageSize(2);
List<Student> students = studentMapper.getStudentList(studentQuery);
for(Student s : students){
System.out.println(s);
}
}
}
mapper分页排序指定字段查询模板的更多相关文章
- MySQL select 语句指定字段查询
指定字段查询 SELECT 语法 SELECT [ALL | DISTINCT] {* | table.* | [table.field1[as alias1][,table.field2[as al ...
- 在使用mybatis中指定字段查询
1:需求:查询学过“叶平”老师所教的所有课的同学的学号.姓名: List<Map<String,Object>> selectYepingAllCourse(@Param(&q ...
- mybatis-plus查询指定字段
show me the code :mybais-plus版本:3.1.1 1,排除某些字段,可以同时排除多个字段排除多个字段写法: .setEntity(new User()) .select(c ...
- mysql查询order by 指定字段排序
当MySQL查询时排序的字段不是数字时而是汉字的时候也可以用when then 来指定排序. 列如yewu_check表的status 字段不是0,1,2而是汉字待办,已办,退回.可以如下写法: S ...
- spring mongodb分页,动态条件、字段查询
使用MongRepository public interface VideoRepository extends MongoRepository<Video, String> { Vid ...
- 对于Oracle中分页排序查询语句执行效率的比较分析
转自:http://bbs.csdn.net/topics/370033478 对于Oracle中分页排序查询语句执行效率的比较分析 作者:lzgame 在工作中我们经常遇到需要在Oracle中进行分 ...
- 把一个select查询结果插入到一个表(可选指定字段和值实例)
把一个select查询结果插入到一个表(可选指定字段和值实例) insert into bak (cc,yf) select cc,9 from ket insert into bak (cc,yf ...
- lucene 查询+分页+排序
lucene 查询+分页+排序 1.定义一个工厂类 LuceneFactory 1 import java.io.IOException; 2 3 import org.apache.lucene.a ...
- Navicat查询哪些表有指定字段名
通常需要查询某个字段来自于哪张表,在navicat中没有直接查哪些表有指定字段名的功能,只能用sql来查. 1.(按字段名查表)查询哪些表有指定字段名(比如查字段名article_id)的SQL: S ...
随机推荐
- php curl模拟post请求提交数据样例总结
在php中要模拟post请求数据提交我们会使用到curl函数,以下我来给大家举几个curl模拟post请求提交数据样例有须要的朋友可參考參考.注意:curl函数在php中默认是不被支持的,假设须要使用 ...
- Win32下C++遍历目录和文件的源码
#include<windows.h> #include<iostream> #include<string> using namespace std; //只能处 ...
- 2014.9.15HTML
<html> <title> </title> ——页面标题 <head> </head> ——网页上的控制信息 <body> ...
- 【转】iOS隐藏导航条1px的底部横线
默认情况下会有这条线 第一种方法: 1 2 3 4 5 6 UINavigationBar *navigationBar = self.navigationController.navigationB ...
- svn的使用总结(待补充)
1.直接选择文件右键--diff比较的是(本地上次与svn同步的文件)与工作区的比较.(每次更新后,会自动备份本次更新的文件) 2.若是要跟 svn最新版本比较的话,可以选择版本找到对应文件,点击sh ...
- <a>元素生成多个<a>的问题,元素标签结尾影响
告诉我不是真的吧,<a/>和<a></a>真的有区别...
- Hibernate知识总结(一)——Hibernate原理概述
一.Hibernate是什么: 它是一个持久化框架,它对JDBC进行了轻量级的封装,简化对数据库的操作,提高开发效率.和另一个持久化框架MyBatis一样,他们操作数据库都是通过一个session对象 ...
- selenium css(转)
如果button上有class属性的,如: <button id="ext-eng=1026" class="x-right-button"...&g ...
- js点击事件代理时切换图片如何防抖动
由于图片的加载速度比较慢,我们可以直接用64base对图片进行编码,把编码加在图片的url中~~~这样加载会快一些,也不会有切换图片时出现的抖动效果
- firebug如何使用
1.怎么安装firebug: a.打开火狐浏览器--------b.点击火狐浏览器的右上角这个小图标-------c.点击<获取附件组件>,在右上角的搜索框()内,输入firebug,点击 ...