本案例是使用IDEA创建的maven的webapp项目
注意parent标签不在< dependencies>标签中
<!--声明springboot版本-->
<parent><!--注意该标签不在<dependencies>中-->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.5.RELEASE</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
<!-- mvc,aop的依赖包 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!-- 由于我们在上面指定了parent,这里就不需要指定版本号 -->
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<!--tomcat-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<!--mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.41</version>
</dependency>
<!-- 阿里巴巴的Druid数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.9</version>
</dependency>
<!--pageHelper分页插件-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.0.0</version>
</dependency>
#application.yml文件对格式要求很严,下面每一个空格都要对齐。application.yml用于读取定义sql语句的文件和配置数据库信息
spring:
datasource:
name: dataSource
type: com.alibaba.druid.pool.DruidDataSource
#druid相关配置,数据库配置
druid:
#监控统计拦截的filters
filters: stat
driver-class-name: com.mysql.jdbc.Driver
#基本属性
url: jdbc:mysql://localhost:3306/day06
username: root
password: root
#配置初始化大小/最小/最大
initial-size: 1
min-idle: 1
max-active: 20
#获取连接等待超时时间
max-wait: 60000
#间隔多久进行一次检测,检测需要关闭的空闲连接
time-between-eviction-runs-millis: 60000
#一个连接在池中最小生存的时间
min-evictable-idle-time-millis: 300000
#配置mabatis文件,扫描定义sql语句的文件,即扫描mapper下的所有xml文件
mybatis:
mapper-locations: classpath:mapper/*.xml
#这是给包起别名的
#type-aliases-package: com.qf.bean
本案例将该xml文件命名为:UsersDaoMapper.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.qf.dao.UsersDao"><!--namespace属性值是接口路径(包名加接口名)-->
<select id="getall" resultType="com.qf.entity.Users">/*id值为调用该sql语句对应的方法名,resultType为表对应的实体类。多表用resultMap*/
SELECT * FROM users
</select>
</mapper>
package com.qf.entity;
import java.util.Date;
public class Users {
private Integer userId;
private String username;
private Date birthday;
private Integer age;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Users{" +
"userId=" + userId +
", username='" + username + '\'' +
", birthday=" + birthday +
", age=" + age +
'}';
}
}
该案例将该接口命名为:UsersDao
package com.qf.dao;
import com.qf.entity.Users;
import org.apache.ibatis.annotations.Select;
import java.util.List;
//mybatis框架操作数据库有两种方式,一种是配置文件方式,一种是注解方式
public interface UsersDao {
public List<Users> getall();
}
该案例将该接口命名为:UsersService
package com.qf.service;
import com.qf.entity.Users;
import java.util.List;
public interface UsersService {
public List<Users> getall();
}
该案例将该实现类命名为:UsersServiceImpl
package com.qf.service.impl;
import com.qf.dao.UsersDao;
import com.qf.entity.Users;
import com.qf.service.UsersService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
@Service
public class UsersServiceImpl implements UsersService{
@Resource
private UsersDao usersDao;
@Override
public List<Users> getall() {
return usersDao.getall();
}
}
本案例将该类命名为:PageConfig
package com.qf.util;
import com.github.pagehelper.PageHelper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.Properties;
//由于分页插件的mybatis-config.xml配置部分没有了,因此单独见一个配置类配置
@Configuration//标记当前类是配置工具类
public class PageConfig {
@Bean
public PageHelper pb(){
PageHelper pageHelper=new PageHelper();
Properties p=new Properties();
//设置为true时,会将RowBounds第一个参数offset当成pageNum页码使用
//和startPage中的pageNum效果一样
p.setProperty("offsetAsPageNum","true");//标记
//设置为true时,使用RowBounds分页会进行count查询
p.setProperty("rowBoundsWithCount","true");//自动生成统计行数
//启用合理化时,如果pageNum<1会查询第一页,如果pageNum>pages会查询最后一页
// 禁用合理化时,如果pageNum<1或pageNum>pages会返回空数据
p.setProperty("reasonable","true");//上一页下一也页返回具体数据
pageHelper.setProperties(p);
return pageHelper;
}
}
该案例将该类命名为:TestController
package com.qf.controller;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.entity.Users;
import com.qf.service.UsersService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import javax.annotation.Resource;
import java.util.List;
@Controller
public class TestController {
@Resource
private UsersService usersService;
@RequestMapping("/test")//访问此请求的地址是localhost:8080/test
public String test(@RequestParam(defaultValue = "1") Integer pageindex, ModelMap map){
//设置分页数据
PageHelper.startPage(pageindex,2);
System.out.println("testjsp-----------");
List<Users> usersList = usersService.getall();
//将数据放在PageInfo中
PageInfo<Users> pageInfo = new PageInfo(usersList);
map.addAttribute("pageInfo",pageInfo);
map.addAttribute("userList",usersList);
return "/show.jsp";//此时实际跳去的页面时/show.jsp.
}
}
本案例将该页面命名为:show.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@page contentType="text/html; charset=utf-8" pageEncoding="UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>TestController的test请求跳转成功</h1>
<h1></h1>
<c:forEach items="${pageInfo.list}" var="Users">
${Users.username}---${Users.birthday}---${Users.userId}
</c:forEach>
</body>
</html>
补充如果相加前后缀只需在application.yml添加对应语句即可
#application.yml文件对格式要求很严,下面每一个空格都要对齐。application.yml用于读取定义sql语句的文件和配置数据库信息
spring:
mvc:
view:
#前缀
prefix: /
#后缀
suffix: .jsp
datasource:
name: dataSource
type: com.alibaba.druid.pool.DruidDataSource
#druid相关配置,数据库配置
druid:
#监控统计拦截的filters
filters: stat
driver-class-name: com.mysql.jdbc.Driver
#基本属性
url: jdbc:mysql://localhost:3306/day06
username: root
password: root
#配置初始化大小/最小/最大
initial-size: 1
min-idle: 1
max-active: 20
#获取连接等待超时时间
max-wait: 60000
#间隔多久进行一次检测,检测需要关闭的空闲连接
time-between-eviction-runs-millis: 60000
#一个连接在池中最小生存的时间
min-evictable-idle-time-millis: 300000
#配置mabatis文件,扫描定义sql语句的文件,即扫描mapper下的所有xml文件
mybatis:
mapper-locations: classpath:mapper/*.xml
#这是给包起别名的
#type-aliases-package: com.qf.bean
添加了application.yml中的前后缀后记得删除controller层中return“/show.jsp”的前后缀