文章目录
- 1. pom依赖
- 2. yml
- 3. 实体类
- 4. mapper映射文件
- 4. mapper接口
- 5. service接口
- 6. 实现类
- 7. controller
- 8. 浏览器验证
使用思路:
1.引入myabtis和pagehelper依赖
2.yml中配置mybatis扫描和实体类
3. 这2行代码
pageNum:当前第几页
pageNum:显示多少条数据
userList:数据库查询的数据数据列表
PageHelper.startPage(pageNum, pageSize);
PageInfo pageInfo = new PageInfo(userList);
最后返回一个pageInfo 对象即可,pageInfo 这个对象中只有数据一些信息,但是,没有成功失败的状态或者提示语。
真实企业中会封装一个返回对象,把pageInfo 放到对象中
1. pom依赖
<!--Myabtis依赖-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.2</version>
</dependency>
<!--pagehelper依赖-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.13</version>
</dependency>
<!--mysql依赖-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.20</version>
</dependency>
<!--数据源-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.21</version>
</dependency>
<!--提高效率lombok插件-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
2. yml
spring:
application:
name: springboot-vue
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/vue?characterEncoding=UTF-8&serverTimezone=GMT%2B8
username: root
password: root
server:
port: 8989
servlet:
context-path: /vue
#mybatis配置信息
mybatis:
configuration:
map-underscore-to-camel-case: true
#控制台日志配置 打印sql
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
mapper-locations: classpath:mappers/*.xml
type-aliases-package: com.baizhi.entity
#数据库方言
pagehelper:
helper-dialect: mysql
#控制台日志配置
logging:
pattern:
console: "[%thread] %-5level %logger{36} - %msg%n"
3. 实体类
package com.baizhi.entity;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import lombok.experimental.Accessors;
import java.io.Serializable;
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
@Accessors(chain = true)
public class TUser implements Serializable {
private static final long serialVersionUID = 745741804375055500L;
private Integer id;
private String name;
private Integer age;
@JsonFormat(pattern = "yyyy-MM-dd", timezone = "GTM+8")
private String bir;
}
4. mapper映射文件
<?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.baizhi.dao.TUserDao">
<resultMap type="com.baizhi.entity.TUser" id="TUserMap">
<result property="id" column="id" jdbcType="INTEGER"/>
<result property="name" column="name" jdbcType="VARCHAR"/>
<result property="age" column="age" jdbcType="INTEGER"/>
<result property="bir" column="bir" jdbcType="VARCHAR"/>
</resultMap>
<select id="selectUserAll" resultMap="TUserMap">
select
id, name, age, bir
from vue.t_user
</select>
</mapper>
4. mapper接口
package com.baizhi.dao;
import com.baizhi.entity.TUser;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
/**
* (TUser)表数据库访问层
*
* @author makejava
* @since 2020-05-05 18:39:21
*/
@Mapper
public interface TUserDao {
List<TUser> selectUserAll();
}
5. service接口
package com.baizhi.service;
import com.baizhi.entity.TUser;
import com.github.pagehelper.PageInfo;
import java.util.List;
/**
* (TUser)表服务接口
*
* @author makejava
* @since 2020-05-05 18:39:22
*/
public interface TUserService {
PageInfo list(Integer pageNum, Integer pageSize);
}
6. 实现类
package com.baizhi.service.impl;
import com.baizhi.dao.TUserDao;
import com.baizhi.entity.TUser;
import com.baizhi.service.TUserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
/**
* (TUser)表服务实现类
*
* @author makejava
* @since 2020-05-05 18:39:22
*/
@Service("tUserService")
public class TUserServiceImpl implements TUserService {
@Resource
private TUserDao tUserDao;
public PageInfo list(Integer pageNum, Integer pageSize) {
PageHelper.startPage(pageNum, pageSize);
List<TUser> userList = this.tUserDao.selectUserAll();
//如果返回前的数据 和 数据库实体 对象数据一致 采下面方式
//如果不一致,采用 PageInfo pageInfo = new PageInfo(); pageInfo.setList(shippingList);
PageInfo pageInfo = new PageInfo(userList);
return pageInfo;
}
}
7. controller
package com.baizhi.controller;
import com.baizhi.service.TUserService;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
/**
* (TUser)表控制层
*
* @author makejava
* @since 2020-05-05 18:39:22
*/
@RestController
@CrossOrigin
@RequestMapping("user")
public class TUserController {
/**
* 服务对象
*/
@Resource
private TUserService tUserService;
@GetMapping("findAll")
public PageInfo findAll(@RequestParam(required = false, defaultValue = "1") Integer pageNum,
@RequestParam(required = false, defaultValue = "10") Integer pageSize) {
PageInfo list = this.tUserService.list(pageNum, pageSize);
return list;
}
}
8. 浏览器验证
http://localhost:8989/vue/user/findAll?pageNum=0&pageSize=5