数据库:mysql5.7
分页查询主要用到的还是limit
limit 10,2 查询从第11条开始的后两条,即查询结果为11,12
limit 10 查询前10条
limit 10,0 查询第11条开始后0条,即结果为0
limit 10,-1 查询第11条开始后所有记录(早期版本支持,比较新的版本可以设置第二个参数为一个很大的值)
第一种方式,自定义分页查询
(1)工具类
package com.lzy.util;
public class FenyeParam {
private String field;
private Integer start=0,count=0;
public void setField(String[] fields) {
this.field="";
for(String f:fields) {
this.field+=f+",";
}
//抹掉最后的逗号
this.field=this.field.substring(0, this.field.length()-1);
}
public void setField1(String sqlFields) {
//like "field,field2,field3..."
this.field=sqlFields;
}
public void setStart(Integer start) {
this.start=start;
}
public void setCount(Integer count) {
this.count=count;
}
public String getField() {
return this.field;
}
public Integer getStart() {
return this.start;
}
public Integer getCount() {
return this.count;
}
@Override
public String toString() {
return "FenyeParam [field=" + field + ", start=" + start + ", count=" + count + "]";
}
}
自定义了一个分页参数类FenyeParam:
field参数为查询的字段,start为开始位置,count为查询记录条数;
field参数设置提供两种方法,setField(String[])方法接收数组,自动拼接sql查询字段;setField1(String)接收String字符串,为sql字段样式(例:"id,name,sex");
(2)相关mapper.xml文件配置:
<!-- 分页查询 -->
<select id="selectFenYe" parameterType="com.lzy.util.FenyeParam" resultType="com.lzy.bean.User">
SELECT ${fenyeParam.field} FROM User limit #{fenyeParam.start,jdbcType=INTEGER},#{fenyeParam.count,jdbcType=INTEGER}
</select>
mybatis中${}和#{}区别:
${}为原样输入,不会修改或转义字符串
#{}为字符串类型输入,根据PreparedStatement安全设置参数
扩充:
PreparedStatement为JDBC sql预处理,替换对应sql字符串中的?,安全,如下代码比较直观:
package sdfadf;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class JDBCUtil {
public static void main(String[] args) {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/testpq?characterEncoding=utf8&useSSL=true&serverTimezone=UTC","xxx","xxxxxx");
String sql="select id,name from user where id=?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, "11");
ResultSet rs = ps.executeQuery();
while(rs.next()) {
System.out.println(rs.getString("id")+","+rs.getString("name"));
}
rs.close();
ps.close();
conn.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
本文用的是mysql5.7,Driver和url根据需自己版本
(3)调用(没有写完完整的controller层和service层,只完成了dao层和mapper.xml,这里的测试用的是junit的单元测试)
package com.lzy.test;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.lzy.bean.User;
import com.lzy.dao.UserMapper;
import com.lzy.util.FenyeParam;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations= {"classpath:applicationContext.xml"})
public class MyTest {
@Autowired
UserMapper uMapper;
@Test
public void Test2() {
FenyeParam fenyeParam=new FenyeParam();
String[] fields=new String[] {"id","name"};
fenyeParam.setField(fields);
//fenyeParam.setField1("id,name");
fenyeParam.setStart(10);
fenyeParam.setCount(2);
List<User> user = uMapper.selectFenYe(fenyeParam);
System.out.println(user);
}
}
结果: