本文根据个人喜好记录“腾讯课堂”的《Java项目之Maven+SpringMVC+Spring+Mybatis+MySql消费查询系统》视频教程关键步骤信息,视频地址:https://ke.qq.com/course/193395
1. pom文件添加依赖
<!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>4.2.1</version> </dependency>
2. mybatis-config文件配置
<!-- 配置分页插件 --> <plugins> <plugin interceptor="com.github.pagehelper.PageHelper"> <!-- 设置数据库类型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库--> <property name="dialect" value="mysql"/> </plugin> </plugins>
3. 编写测试
3.1 修改mapper class
public interface HelloMapper { public List<String> select() ; }
3.2 修改mapper xml
<select id="select" resultType="string"> select text from tbl_test_hello </select>
3.2 修改业务类
@Service public class HelloService { @Autowired HelloMapper helloMapper ; public String getHelloString(int page){ PageHelper.startPage(page,3) ; List<String> list = helloMapper.select(); StringBuilder sb = new StringBuilder() ; for (int i = 0; i < list.size(); i++) { sb.append(list.get(i)) ; sb.append("<br>") ; } return sb.toString(); } }
3.3 修改视图层
@Controller public class HelloController { @Autowired HelloService helloService ; @ResponseBody @RequestMapping("/hello.do") public String testHello(@RequestParam(value = "page",defaultValue = "1") int page){ return helloService.getHelloString(page) ; } }
3.4 测试结果