梳理后端分页的数据流
- 控制台输出结果:
上面控制台打印的分页数据,对应的@Test类的代码内容如下:
(代码中创建service层对应的对象,调用方法返回page进行打印)
public class ApplicationContextTest {
private ApplicationContext ctx = null;
private SalesChanceService salservice = null;
{
ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
salservice = ctx.getBean(SalesChanceService.class);
}
@Test
public void testCustomerServiceGetPage(){
int currentPage = 2;
int pageSize = 3;
Page<SalesChance> page = salservice.findByPage(currentPage,pageSize);
System.out.println("page:" + page);
System.out.println("getCurrPage:" + page.getCurrPage());
System.out.println("getPageSize:" + page.getPageSize());
System.out.println("getTotalCount:" + page.getTotalCount());
System.out.println("getTotalPage:" + page.getTotalPage());
System.out.println("getlist:" + page.getList());
}
- 上面@Test类中调用的service层的类和方法,代码如下:
(SalesChanceService类中,对应的findByPage方法,会传入当前页码,每页显示条数,然后返回page)
@Service
public class SalesChanceService {
@Autowired
PageMapper qmap;
public Page<SalesChance> findByPage(int currentPage, int pageSize){
... //省略号部分,表示省略部分通用的service层分页业务代码
map.put("start", (currentPage-1)*pageSize);
map.put("size", page.getPageSize());
List<SalesChance> list=qmap.findByPage(map);
page.setList(list);
return page;
}
}
我们可以从上面最后几行代码中看到,service层中findByPage方法内部,调用了dao层的findByPage方法,传入了一个map集合
map集合中分别传入了key和value,这里"start"和"size"对应的value的值将作为参数替换mapper.xml文件sql对应参数
map.put("start", (currentPage-1)*pageSize);
map.put("size", page.getPageSize());
mapper.xml文件示例内容:
select id, cust_name as custName,
title, contact,
contact_tel as contactTel,
create_date as createDate
from SALES_CHANCES limit #{start},#{size}
说明:page对象是:
Page<SalesChance>
对应的Page和SalesChance类,属性如下:
(SalesChance类对应封装数据库中SALES_CHANCE表)
- dao层的PageMapper.java代码如下:
public interface PageMapper {
public int selectCount();
public List<SalesChance> findByPage(HashMap<String,Object> map);
}
- dao 层的PageMapper.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="com.yanglun.crm.mapper.PageMapper" >
<resultMap id="BaseResultMap" type="com.yanglun.crm.entity.SalesChance" />
<!-- 分页SQL语句 -->
<select id="findByPage" resultMap="BaseResultMap">
select id, cust_name as custName,
title, contact,
contact_tel as contactTel,
create_date as createDate
from SALES_CHANCES limit #{start},#{size}
</select>
<!-- 取得记录的总数 -->
<select id="selectCount" resultType="java.lang.Integer">
SELECT COUNT(*) FROM SALES_CHANCES
</select>
</mapper>
1)上面SQL语句对应的<select中的id各自对应了上面PageMapper.java中方法名
如:findByPage,selectCount
2)result 结果分别返回 map和integer类型,
其中map类型通过上面resultMap标签,关联返回类型为:
type=“com.yanglun.crm.entity.SalesChance”
3)传递的参数#{start},#{size} 如下
service层代码中方法,调用dao层中方法,传递map
map.put("start", (currentPage-1)*pageSize);
map.put("size", page.getPageSize());
List<SalesChance> list=qmap.findByPage(map);
- spring-db.xml 通过该配置文件中对应的配置项扫描到mapper包下的sql对应的.xml文件
<!--spring与mybatis完美整合,不需要mybatis配置映射文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 自动扫描mapping.xml文件,classpath后需要输入自己的mapper文件的路径 -->
<property name="mapperLocations" value="classpath:com/yanglun/crm/mapper/*.xml"></property>
</bean>
同时关联该文件下,对应dataSource
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:jdbc.properties" />
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${driver}" />
<!--数据库连接的url -->
<property name="url" value="${url}" />
<!--用户名 -->
<property name="username" value="${username}" />
<!--密码 -->
<property name="password" value="${password}" />
<!--初始化连接大小 -->
<property name="initialSize" value="${initialSize}" />
<!--连接池最大数量 -->
<property name="maxActive" value="${maxActive}" />
<!--连接池最大空闲 -->
<property name="maxIdle" value="${maxIdle}" />
<!--连接池最小空闲 -->
<property name="minIdle" value="${minIdle}" />
<!--获取连接最长等待时间 -->
<property name="maxWait" value="${maxWait}" />
</bean>
- spring-db.xml 中dataSource配置关联jdbc.properties中对应的数据库连接信息,与数据库建立连接,获取到数据
jdbc.properties:
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mysql
username=root
password=root
initialSize=0
maxActive=20
maxIdle=20
minIdle=1
maxWait=60000
- 数据库表数据第二页,对应步骤1中控制台输出结果
(数据转载说明:数据库表中数据来源于atguigu school教学使用)