springboot2.0基于Mybatis集成PageHelper分页插件

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_36940806/article/details/91315069

一,mysql数据库测试脚本

use  test;

CREATE TABLE USER (
    id int PRIMARY KEY AUTO_INCREMENT,
    name varchar(20) NOT NULL,
    age int
    );
select * from user;

二,创建maven工程,项目目录结构如下图所示

三,类实现

pom.xml

<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.springboot</groupId>
	<artifactId>mybatis-pagehelper</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<!-- spring-boot-starter-parent 整合常用第三方框架依赖信息 -->
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.0.RELEASE</version>
	</parent>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
			<!-- 这里省略了版本号,是因为在parent里面封装好了版本号 -->
		</dependency>
		<!-- lombok插件 (注意:eclipse需要集成lombok插件,可自行百度) -->
		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
		</dependency>
		<!-- 测试 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<!-- 这里省略了版本号,是因为在parent里面封装好了版本号 -->
		</dependency>
		<!-- mybatis依赖 -->
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>1.1.1</version>
		</dependency>
		<!-- mysql依赖 -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<!-- 这里省略了版本号,是因为在parent里面封装好了版本号 -->
		</dependency>
		<!-- springboot - web组件 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
			<!-- 这里省略了版本号,是因为在parent里面封装好了版本号 -->
		</dependency>
		<!-- springboot 整合 pagehelper分页组件 -->
		<dependency>
			<groupId>com.github.pagehelper</groupId>
			<artifactId>pagehelper-spring-boot-starter</artifactId>
			<version>1.2.5</version>
		</dependency>
		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-lang3</artifactId>
		</dependency>

	</dependencies>
</project>

application.properties

###datasource
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource..password=a
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

loggin.level.com.example.demo.dao=DEBUG
pagehelper.helperDialect=mysql
pagehelper.response=true
pagehelper.supportMaxArguments=true
pagehelper.params=count=countSql
pagehelper.page-size-zero=true

UserController

package com.springboot.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.springboot.entity.User;
import com.springboot.service.UserService;

@RestController
public class UserController {

	@Autowired
	private UserService userService;

	/**
	 * 分页查询
	 * 
	 * @return
	 */
	@RequestMapping("findUserList")
	public PageInfo<User> findUserList(int page, int pageSize) {
		return userService.findUserList(page, pageSize);

	}

	@RequestMapping("findByName")
	public User findByName(String name) {
		return userService.findByName(name);

	}

	@RequestMapping("insert")
	public int insert(String name, Integer age) {
		return userService.insert(name, age);
	}
}

User

package com.springboot.entity;

/**
 * 实体类
 */
import lombok.Data;

@Data // 集成了lombok之后,可自动生成get,set方法
public class User {

	private Integer id;
	private String name;
	private Integer age;

}

UserMapper

package com.springboot.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import com.springboot.entity.User;

public interface UserMapper {

	// 查询
	@Select("select * from user   ")
	List<User> findUserList();

	// 条件查询
	@Select("select * from user where name=#{name}")
	User findByName(@Param("name") String name);

	// 添加
	@Insert("insert into user(name,age) values(#{name},#{age})")
	int insert(@Param("name") String name, @Param("age") Integer age);

}

UserService

package com.springboot.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.springboot.entity.User;
import com.springboot.mapper.UserMapper;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j // 等同于private Logger logger = LoggerFactory.getLogger(this.getClass());
public class UserService {

	@Autowired
	private UserMapper userMapper;

	/**
	 * 
	 * @param page
	 *            (当前页数)
	 * @param pageSize
	 *            (每页个数)
	 * @return
	 */
	public PageInfo<User> findUserList(int page, int pageSize) {
		// mysql查询使用limit
		PageHelper.startPage(page, pageSize); // 底层实现原理采用改写sql语句
		List<User> userList = userMapper.findUserList();
		PageInfo<User> pageInfoUserList = new PageInfo<User>(userList);
		log.info("分页结果:" + pageInfoUserList);
		return pageInfoUserList;

	}

	public User findByName(String name) {
		User user = userMapper.findByName(name);
		log.info("查询结果:" + user.toString());
		return user;

	}

	public int insert(String name, Integer age) {
		int result = userMapper.insert(name, age);
		log.info("插入结果:" + result);
		return result;
	}
}

MybatisApp

package com.springboot;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * 将启动的代码抽取出来,单独运行
 *
 * @author vean
 *
 */
@SpringBootApplication // 等同于@EnableAutoConfiguration+@ComponentScan(只扫描同级包<包含子包>和当前包)
@MapperScan(basePackages = { "com.springboot.mapper" }) // 扫描映射文件。
// 注意:mybatis启东时候,在mapper层可以不加mapper注解,但是一定要在启动类上加@mapperScan注解
// 也可以在mybatis接口上(xxxMapper)加上@Mapper来注入mybatis容器,就不需要在启动类的时候加上@mapperscan
public class MybatisApp {
	public static void main(String[] args) {
		// 整个程序的入口,启动springboot项目,创建内置tomcat服务器,使用tomcat加载springmvc 注解启动类
		SpringApplication.run(MybatisApp.class, args);
	}
}

四,项目演示

1,添加多条数据到数据库中,添加方式如下图所示

数据库中的数据:

2,查询(如设置每页大小 paheSize=2,查询第三页 page=3的数据)

猜你喜欢

转载自blog.csdn.net/qq_36940806/article/details/91315069