mybatis分页
原创
©著作权归作者所有:来自51CTO博客作者kq1983的原创作品,请联系作者获取转载授权,否则将追究法律责任
1. pom.xml
<!-- mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<!-- 当前最新版本是3.6.2 -->
<version>3.6.2</version>
</dependency>
<dependency>
<groupId>com.github.jsqlparser</groupId>
<artifactId>jsqlparser</artifactId>
<version>0.9.1</version>
</dependency>
spring-mybatis.xml
<!-- myBatis文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mapperLocations" value="classpath:com/kq/mapping/*.xml" />
<!-- 添加分页插件 -->
<property name="plugins">
<list>
<bean class="com.github.pagehelper.PageHelper">
<property name="properties">
<props>
<prop key="dialect">mysql</prop>
<prop key="reasonable">true</prop>
</props>
</property>
</bean>
</list>
</property>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
<property name="basePackage" value="com.kq.dao" />
</bean>
public interface EmployeeDao {
/**
* get employee by id
* @param id
* @return
*/
public Employee getEmployee(Integer id);
/**
* find employee
* @return
*/
public List<Employee> getEmployeeList();
}
EmployeeDao.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.kq.dao.EmployeeDao">
<select id="getEmployeeList" resultType="com.kq.entity.Employee">
SELECT * FROM employee
</select>@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:appcontext.xml")
public class EmployeeServiceTest {
@Autowired
private EmployeeService employeeService;
@Test
public void testPage(){
PageInfo<Employee> page = employeeService.getEmployeeList(1);
System.out.println(page);
page = employeeService.getEmployeeList(10);
System.out.println(page);
}
}
<!--<insert id="insert" parameterType="com.csst.entity.Employee">
insert into employee(name) values(#{name})
</insert> -->
<select id="getEmployee" parameterType="int" resultType="com.kq.entity.Employee">
SELECT * FROM employee where id=#{id}
</select>
</mapper>
public interface EmployeeService {
/**
* get employee by id
* @param id
* @return
*/
public Employee getEmployee(Integer id);
/**
* find employee
* @return
*/
public PageInfo<Employee> getEmployeeList(int curPage);
}
@Service("employeeService")
public class EmployeeServiceImpl implements EmployeeService {
@Autowired
private EmployeeDao employeeDao;
@Override
public Employee getEmployee(Integer id) {
return employeeDao.getEmployee(id);
}
@Override
public PageInfo<Employee> getEmployeeList(int curPage) {
PageHelper.startPage(curPage, 1);
List<Employee> list = employeeDao.getEmployeeList();
PageInfo<Employee> page = new PageInfo<>(list);
return page;
}
}
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:appcontext.xml")
public class EmployeeServiceTest {
@Autowired
private EmployeeService employeeService;
@Test
public void testPage(){
PageInfo<Employee> page = employeeService.getEmployeeList(1);
System.out.println(page);
page = employeeService.getEmployeeList(10);
System.out.println(page);
}
}