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 0000000000000000000000000000000000000000..6cdf3118d559f48d3c376236bb2690671a06aa0e --- /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 bc19a14825abcc9f804b77f1b7e117734c367a68..84cd7a99c8c553a8d2f49900435b084b3ad4a3e1 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 9f7a13ff07c70eb161a805b0fb4b93f516058d7c..ed7a117628c314dc5efb5b9bddb6aade9b29e1a3 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 888f5bdb7dc9c5a5cf3dfbec2366438be735cad3..99c317a60d6cec598db0d368e08692f7f3571ff2 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 a197b4c687e0bab49ebea0c5a126a500e5987abe..a0439f56d61be4bb9a31054501a7daf8fff3a745 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