创建SpringBoot项目
image.png
image.png
application.properties配置
#设置访问端口
server.port=8080
#thymeleaf配置,这里是可以省略的,因为默认配置已经足够
#关闭缓存,及时刷新页面,这一点很重要
spring.thymeleaf.cache=false
#注释的部分是Thymeleaf默认的配置,如有其它需求可以自行更改
#spring.thymeleaf.prefix=classpath:/templates/
#spring.thymeleaf.suffix=.html
#spring.thymeleaf.mode=HTML5
#spring.thymeleaf.encoding=UTF-8
#spring.thymeleaf.servlet.content-type=text/html
#设置数据源
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://47.92.245.9:3306/peo-test?&useUnicode=true&characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=Btkx1234!
#连接池类型
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
#连接池配置,因为springboot默认是开启了连接池的,它有默认配置,这一段可以忽略
# 初始化大小,最小,最大
spring.datasource.initialSize=5
spring.datasource.minIdle=5
spring.datasource.maxActive=20
# 配置获取连接等待超时的时间
spring.datasource.maxWait=60000
# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
spring.datasource.timeBetweenEvictionRunsMillis=60000
# 配置一个连接在池中最小生存的时间,单位是毫秒
spring.datasource.minEvictableIdleTimeMillis=300000
spring.datasource.validationQuery=SELECT 1 FROM DUAL
spring.datasource.testWhileIdle=true
spring.datasource.testOnBorrow=false
spring.datasource.testOnReturn=false
# 打开PSCache,并且指定每个连接上PSCache的大小
spring.datasource.poolPreparedStatements=true
spring.datasource.maxPoolPreparedStatementPerConnectionSize=20
# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
spring.datasource.filters=stat,wall,log4j
# 通过connectProperties属性来打开mergeSql功能;慢SQL记录
spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
#配置分页插件
#pagehelper分页插件
pagehelper.helper-dialect=mysql
pagehelper.reasonable=true
pagehelper.support-methods-arguments=true
pagehelper.params=count=countSql
在src/main/resources/templates创建/admin/list.html页面
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Test</title>
<script src="http://libs.baidu.com/jquery/2.1.4/jquery.min.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body>
<table>
<th>id</th>
<th>姓名</th>
<!-- <th>性别</th>-->
<!-- <th>年龄</th>-->
<tr th:each="list:${findAll}">
<td th:utext="${list.q_id}"></td>
<td th:utext="${list.title}"></td>
<!-- <td th:utext="${list.sex}"></td>-->
<!-- <td th:utext="${list.age}"></td>-->
</tr>
</table>
<table>
<tr>
<td><a href="/test?num=0">首页</a></td>
<td th:if="${pageInfo.getPrePage()} != '0' "><a href="javascript:;"
th:href="${'/test?num='+pageInfo.getPrePage()}">上一页</a></td>
<td th:if="${pageInfo.getPrePage()} eq '0' ">上一页</td>
<td><span th:text="${pageInfo.getPageNum()}"> </span></td>
<td>/</td>
<td><span th:text="${pageInfo.getPages()}"> </span></td>
<td th:if="${pageInfo.getNextPage()} != '0' "><a href="javascript:;"
th:href="${'/test?num='+pageInfo.getNextPage()}">下一页</a></td>
<td th:if="${pageInfo.getNextPage()} eq '0' ">下一页</td>
<td><a href="javascript:;" th:href="${'/test?num='+pageInfo.getPages()}">末页</a></td>
<td>
<form action="/test" method="get">
<input type="hidden" id="nn" th:value="${pageInfo.getPages()}">
跳转到: <input type="text" id="ss" name="num" value="" style="width: 20px">
<input type="submit" onclick="f()" value="确定">
</form>
</td>
</tr>
</table>
</body>
<script>
function f() {
var num = document.getElementById("nn").value;
var nums = document.getElementById("ss").value;
if (nums > num || nums < 0) {
alert("无此页,请重新输入");
document.getElementById("ss").value = "";
}
}
</script>
</html>
创建目录
image.png
创建DAO 在上图mapper下创建接口
package com.pagehelper.demo.mapper;
import com.pagehelper.demo.entity.Question;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@Mapper
public interface QuestionMapper {
@Select("<script> select * from pms_question </script>")
List<Question> listAll();
}
创建service及实现类
//接口
package com.pagehelper.demo.service;
import com.pagehelper.demo.entity.Question;
import java.util.List;
public interface QuestionService {
List<Question> listAll();
}
//实现类
package com.pagehelper.demo.service.impl;
import com.pagehelper.demo.entity.Question;
import com.pagehelper.demo.mapper.QuestionMapper;
import com.pagehelper.demo.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class QuestionServiceImpl implements QuestionService {
@Autowired
private QuestionMapper questionMapper;
@Override
public List<Question> listAll() {
return questionMapper.listAll();
}
}
创建Controller
package com.pagehelper.demo.controller;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pagehelper.demo.entity.Question;
import com.pagehelper.demo.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import java.util.List;
@Controller
public class TestController {
@Autowired
private QuestionService questionService;
@GetMapping("/test")
public String test(Model model, Integer num) {
if (num == null) {
PageHelper.startPage(0, 10);
} else {
PageHelper.startPage(num, 10);
}
List<Question> findAll = questionService.listAll();
PageInfo pageInfo = new PageInfo<Question>(findAll);
model.addAttribute("findAll", findAll);
model.addAttribute("pageInfo", pageInfo);
return "admin/list";
}
}
结果
页面没做美化
image.png