From 1faf6709b39366a16e2e8523afa2d7a18e541618 Mon Sep 17 00:00:00 2001 From: javahongxi Date: Mon, 22 Jan 2018 18:47:34 +0800 Subject: [PATCH] PageHelper --- .../spring/boot/config/MybatisConfig.java | 25 +++++++++++++++++++ .../whatsmars/spring/boot/dao/UserMapper.java | 2 ++ .../spring/boot/service/UserService.java | 3 +++ .../boot/service/impl/UserServiceImpl.java | 7 ++++++ .../src/main/resources/mapper/UserMapper.xml | 6 ++++- 5 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 whatsmars-spring-boot/src/main/java/org/hongxi/whatsmars/spring/boot/config/MybatisConfig.java diff --git a/whatsmars-spring-boot/src/main/java/org/hongxi/whatsmars/spring/boot/config/MybatisConfig.java b/whatsmars-spring-boot/src/main/java/org/hongxi/whatsmars/spring/boot/config/MybatisConfig.java new file mode 100644 index 00000000..6cdf3118 --- /dev/null +++ b/whatsmars-spring-boot/src/main/java/org/hongxi/whatsmars/spring/boot/config/MybatisConfig.java @@ -0,0 +1,25 @@ +package org.hongxi.whatsmars.spring.boot.config; + +import com.github.pagehelper.PageHelper; +import com.github.pagehelper.SqlUtilConfig; +import org.apache.ibatis.plugin.Interceptor; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class MybatisConfig { + + @Bean + public Interceptor pageHelper() { + PageHelper pageHelper = new PageHelper(); + SqlUtilConfig sqlUtilConfig = new SqlUtilConfig(); + sqlUtilConfig.setDialect("mysql"); + sqlUtilConfig.setOffsetAsPageNum(true); + sqlUtilConfig.setRowBoundsWithCount(true); + sqlUtilConfig.setPageSizeZero(true); + sqlUtilConfig.setReasonable(false); + sqlUtilConfig.setSupportMethodsArguments(false); + pageHelper.setSqlUtilConfig(sqlUtilConfig); + return pageHelper; + } +} diff --git a/whatsmars-spring-boot/src/main/java/org/hongxi/whatsmars/spring/boot/dao/UserMapper.java b/whatsmars-spring-boot/src/main/java/org/hongxi/whatsmars/spring/boot/dao/UserMapper.java index bc19a148..84cd7a99 100644 --- a/whatsmars-spring-boot/src/main/java/org/hongxi/whatsmars/spring/boot/dao/UserMapper.java +++ b/whatsmars-spring-boot/src/main/java/org/hongxi/whatsmars/spring/boot/dao/UserMapper.java @@ -16,4 +16,6 @@ public interface UserMapper { void insert(User user); void insertBatch(List users); + + List query(); } diff --git a/whatsmars-spring-boot/src/main/java/org/hongxi/whatsmars/spring/boot/service/UserService.java b/whatsmars-spring-boot/src/main/java/org/hongxi/whatsmars/spring/boot/service/UserService.java index 9f7a13ff..ed7a1176 100644 --- a/whatsmars-spring-boot/src/main/java/org/hongxi/whatsmars/spring/boot/service/UserService.java +++ b/whatsmars-spring-boot/src/main/java/org/hongxi/whatsmars/spring/boot/service/UserService.java @@ -1,5 +1,6 @@ package org.hongxi.whatsmars.spring.boot.service; +import com.github.pagehelper.Page; import org.hongxi.whatsmars.spring.boot.model.User; /** @@ -10,4 +11,6 @@ public interface UserService { User findByUsername(String username); void add(User user); + + Page query(int offset, int limit); } diff --git a/whatsmars-spring-boot/src/main/java/org/hongxi/whatsmars/spring/boot/service/impl/UserServiceImpl.java b/whatsmars-spring-boot/src/main/java/org/hongxi/whatsmars/spring/boot/service/impl/UserServiceImpl.java index 888f5bdb..99c317a6 100644 --- a/whatsmars-spring-boot/src/main/java/org/hongxi/whatsmars/spring/boot/service/impl/UserServiceImpl.java +++ b/whatsmars-spring-boot/src/main/java/org/hongxi/whatsmars/spring/boot/service/impl/UserServiceImpl.java @@ -1,5 +1,7 @@ package org.hongxi.whatsmars.spring.boot.service.impl; +import com.github.pagehelper.Page; +import com.github.pagehelper.PageHelper; import org.hongxi.whatsmars.spring.boot.dao.UserMapper; import org.hongxi.whatsmars.spring.boot.model.User; import org.hongxi.whatsmars.spring.boot.service.UserService; @@ -29,4 +31,9 @@ public class UserServiceImpl implements UserService { userMapper.insert(user); logger.info("add user success, username: {}", user.getUsername()); } + + @Override + public Page query(int offset, int limit) { + return PageHelper.offsetPage(offset, limit).doSelectPage(() -> userMapper.query()); + } } diff --git a/whatsmars-spring-boot/src/main/resources/mapper/UserMapper.xml b/whatsmars-spring-boot/src/main/resources/mapper/UserMapper.xml index a197b4c6..a0439f56 100644 --- a/whatsmars-spring-boot/src/main/resources/mapper/UserMapper.xml +++ b/whatsmars-spring-boot/src/main/resources/mapper/UserMapper.xml @@ -14,8 +14,12 @@ insert into user(username, nickname, gender, age, create_date, update_date) values - (#{username}, #{nickname}, #{gender}, #{age}, NOW(), NOW()) + (#{username}, #{nickname}, #{gender}, #{age}, #{createDate}, #{updateDate}) + + \ No newline at end of file -- GitLab