mybatis和分页插件的依赖配置:

1
2
3
4
5
6
7
8
9
10
11
12
<!-- mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<!--mybatis分页插件-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.1.0</version>
</dependency>

application.yml配置:

1
2
3
4
5
mybatis:
#mapper-locations: classpath:/mybatis/mysql/*Mapper.xml
#type-aliases-package: com.ybf.activity.web.entity
config-location: classpath:/mybatis/mybatis-config.xml
check-config-location: true

mybatis-config.xml配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?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="logImpl" value="LOG4J"/>
</settings>
<typeAliases>
<typeAlias type="com.ybf.activity.web.entity.Student" alias="Student" />
</typeAliases>
<plugins>
<!--mybatis分页插件-->
<plugin interceptor="com.github.pagehelper.PageHelper">
<property name="dialect" value="mysql"/>
<property name="offsetAsPageNum" value="false"/>
<property name="rowBoundsWithCount" value="false"/>
<property name="pageSizeZero" value="true"/>
<property name="reasonable" value="false"/>
<property name="supportMethodsArguments" value="false"/>
<property name="returnPageInfo" value="none"/>
</plugin>
</plugins>
<mappers>
<mapper resource="mybatis/mapper/mysql/StudentMapper.xml"/>
</mappers>
</configuration>

spring启动类增加Mapper扫描:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/**
* Created by Administrator on 2017/7/6.
*/
@SpringBootApplication
// Mapper扫描
@MapperScan("com.ybf.activity.web.mapper")
public class WebApplication extends SpringBootServletInitializer {
private final static Logger logger = LoggerFactory.getLogger(WebApplication.class);
// 编码过滤器
@Bean
Filter characterEncodingFilter() {
logger.info("==========初始化编码过滤器=================");
CharacterEncodingFilter filter = new CharacterEncodingFilter();
filter.setEncoding("UTF-8");
filter.setForceEncoding(true);
return filter;
}
public static void main(String[] args) {
SpringApplication.run(WebApplication.class,args);
logger.info("Application [activity-web] started!");
}
}

Mapper接口:

1
2
3
4
public interface StudentMapper extends BaseMapper{
Student getById(int id);
List<Student> sel();
}

StudentMapper.xml:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?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.ybf.activity.web.mapper.StudentMapper">
<resultMap id="stuMap" type="Student">
<id property="id" column="id" />
<result property="name" column="name" />
<result property="sumScore" column="score_sum" />
<result property="avgScore" column="score_avg" />
<result property="age" column="age" />
</resultMap>
<select id="getById" resultMap="stuMap" resultType="Student">
SELECT *
FROM STUDENT a
WHERE ID = #{id}
</select>
<select id="sel" resultType="Student" resultMap="stuMap">
select * from student a
</select>
</mapper>

用法:
在查询之前使用PageHelper.startPage()设置分页。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
@Controller
public class IndexController {
@Autowired
private StudentMapper sm;
@RequestMapping("/student/page/{pageNo}")
@ResponseBody
public List<Student> getStduentByPage(@PathVariable int pageNo) {
if (pageNo > 0) {
PageHelper.startPage(pageNo,3);
return sm.selectByCondition();
}
return null;
}
}

注:测试的spring boot版本为1.5.4.RELEASE.