1、在数据库中创建d_department表
CREATE TABLE `d_department` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`departmentCode` varchar(20) DEFAULT NULL COMMENT '所属部门编码',
`departmentName` varchar(20) DEFAULT NULL COMMENT '所属部门名称',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
INSERT INTO `d_department` VALUES (NULL, 'xinlingchanpinbu', '信令产品部');
INSERT INTO `d_department` VALUES (NULL, 'yunyingbu', '运营部');
2、在mybatis目录下新建mybatis-config.xml文件,配置分页插件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<!-- 开启驼峰自动映射 -->
<setting name="mapUnderscoreToCamelCase" value="true" />
</settings>
<plugins>
<plugin interceptor="com.github.pagehelper.PageHelper">
<property name="dialect" value="mysql"/>
</plugin>
</plugins>
</configuration>
3、在mybatis/mappers目录下创建DepartmentMapper.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" >
<!-- 配置SQL语句,与实体类操作的对应关系。 -->
<!-- 保证唯一性 -->
<mapper namespace="com.fullmark.dao.DepartmentMapper">
<!--
将返回的数据 与对象对应。将返回的数据,按照配置组装成对应的对象。
-->
<select id="selectAll" resultType="com.fullmark.model.Department">
select id,departmentCode,departmentName from d_department
</select>
</mapper>
4、修改spring目录下spring-mybatis.xml文件
之前是被我注释掉了,把注释去掉就可以了
<!-- 加载mybatis全局配置文件 -->
<property name="configLocation" value="classpath:mybatis/mybatis-config.xml" />
<!-- 扫描 mappers目录以及子目录下所有的xml文件 -->
<property name="mapperLocations" value="classpath:mybatis/mappers/**/*.xml" />
5、编写model包下创建实体类Department
public class Department {
private int id;
private String departmentCode;
private String departmentName;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getDepartmentCode() {
return departmentCode;
}
public void setDepartmentCode(String departmentCode) {
this.departmentCode = departmentCode;
}
public String getDepartmentName() {
return departmentName;
}
public void setDepartmentName(String departmentName) {
this.departmentName = departmentName;
}
}
6、在dao包下创建接口DepartmentMapper(注意是interface不是class)
public interface DepartmentMapper {
//查询所有
public List<Department> selectAll();
}
7、在test包下创建编写测试类DepartmentServiceTest
package com.fullmark.service;
import com.alibaba.fastjson.JSON;
import com.fullmark.dao.DepartmentMapper;
import com.fullmark.model.Department;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
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 java.util.List;
@RunWith(SpringJUnit4ClassRunner.class)
//告诉junit spring的配置文件
@ContextConfiguration({"classpath:spring/spring-mybatis.xml"})
public class DepartmentServiceTest {
@Autowired
private DepartmentMapper departmentMapper;
@Test
public void testSelectAll() throws Exception {
List<Department> Departments = departmentMapper.selectAll();
for (Department department : Departments) {
System.err.println(department.getId() + " " + department.getDepartmentCode()
+ " " + department.getDepartmentName());
}
}
@Test
public void testSelectByPage() throws Exception {
PageHelper.startPage(1, 1);
List<Department> Departments = departmentMapper.selectAll();
PageInfo<Department> pageInfo = new PageInfo<Department>(Departments);
JSON json = (JSON) JSON.toJSON(pageInfo);
System.err.println(json.toJSONString());
}
}
运行testSelectAll查询全部得到结果如下,说明在不使用分页插件的情况下,查询全部已经成功了。
1 xinlingchanpinbu 信令产品部
2 yunyingbu 运营部
3 xin 信令产品部
运行testSelectByPage得到分页数据如下,说明使用PageHelper分页插件成功
{"total":3,"lastPage":3,"nextPage":2,"hasNextPage":true,"pages":3,"pageSize":1,"navigatePages":8,"list":[{"id":1,"departmentCode":"xinlingchanpinbu","departmentName":"信令产品部"}],"hasPreviousPage":false,"navigatepageNums":[1,2,3],"size":1,"pageNum":1,"prePage":0,"endRow":1,"isLastPage":false,"startRow":1,"firstPage":1,"isFirstPage":true}