1.项目搭建
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/d7a594c29abd7fb0d9a79c4b902e7b55.png)
2.pom.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.yd.app</groupId>
<artifactId>06-springboot-mybatis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>06-springboot-mybatis</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--springboot和mybatis的集成的jar-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
<!--mysql的驱动jar包-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!--阿里的数据库连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.9</version>
</dependency>
<!--阿里的分页插件-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.5</version>
</dependency>
<!--jsp的解析器-->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<!--jstl依赖-->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3.application.yml配置
server:
port: 8080
servlet:
context-path: /
#配置数据库连接信息
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/mybatis
username: root
password: root
type: com.alibaba.druid.pool.DruidDataSource #数据库连接池
mvc:
view: #配置视图解析器
prefix: /
suffix: .jsp
mybatis:
#加载mybatis的映射文件
mapper-locations: classpath:/mapper
4.创建users实体类
public class Users {
private Integer uid;
private String uname;
private String password;
private String address;
5.创建userMapper接口
@Mapper
public interface UsersMapper {
public List<Users> loadUsersListMapper();
}
6.创建UserMapper.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.yd.app.mapper.UsersMapper">
<!--实现用户信息分页查询-->
<select id="loadUsersListMapper" resultType="Users">
select * from users
</select>
</mapper>
7.创建UsersService接口
public interface UsersService {
public Map<String,Object> loadUsersListService(Integer page,Integer rows);
}
8.创建UsersServiceImpl
@Service
public class UsersServiceImpl implements UsersService {
@Autowired
private UsersMapper usersMapper;
@Override
public Map<String, Object> loadUsersListService(Integer page, Integer rows) {
Page<Object> ps = PageHelper.startPage(page, rows);
List<Users> users = usersMapper.loadUsersListMapper();
Map<String, Object> result=new HashMap<>();
result.put("users",users);
result.put("pages",ps.getPages());
result.put("total",ps.getTotal());
return result;
}
}
9.创建UsersController
@Controller
public class UsersController {
@Autowired
private UsersService usersService;
@RequestMapping("/list")
public String loadUsersList(
@RequestParam(defaultValue = "1") Integer page,
@RequestParam(defaultValue = "2") Integer rows, Model model){
Map<String, Object> result = usersService.loadUsersListService(page, rows);
model.addAttribute("users",result.get("users"));
model.addAttribute("pages",result.get("pages"));
model.addAttribute("total",result.get("total"));
model.addAttribute("page",page);
return "list";
}
}
10.创建list.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<table align="center" border="1">
<tr>
<td>编号</td>
<td>姓名</td>
<td>密码</td>
<td>地址</td>
</tr>
<c:forEach items="${users}" var="u">
<tr>
<td>${u.uid}</td>
<td>${u.uname}</td>
<td>${u.password}</td>
<td>${u.address}</td>
</tr>
</c:forEach>
<tr>
<td colspan="4">
当前${page}页
<c:if test="${page<=1}">
<a href="list?page=${page+1}">下一页</a>
</c:if>
<c:if test="${page>1 && page<pages}">
<a href="list?page=${page-1}">上一页</a>
<a href="list?page=${page+1}">下一页</a>
</c:if>
<c:if test="${page>=pages}">
<a href="list?page=${page-1}">上一页</a>
</c:if>
最大${pages}页
</td>
</tr>
</table>
</body>
</html>