提交 1faf6709 编写于 作者: 武汉红喜's avatar 武汉红喜

PageHelper

上级 977c8675
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;
}
}
......@@ -16,4 +16,6 @@ public interface UserMapper {
void insert(User user);
void insertBatch(List<User> users);
List<User> query();
}
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<User> query(int offset, int limit);
}
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<User> query(int offset, int limit) {
return PageHelper.offsetPage(offset, limit).doSelectPage(() -> userMapper.query());
}
}
......@@ -14,8 +14,12 @@
<insert id="insertBatch" parameterType="list">
insert into user(username, nickname, gender, age, create_date, update_date) values
<foreach collection="list" item="item" separator=",">
(#{username}, #{nickname}, #{gender}, #{age}, NOW(), NOW())
(#{username}, #{nickname}, #{gender}, #{age}, #{createDate}, #{updateDate})
</foreach>
</insert>
<select id="query" resultType="User">
select * from user
</select>
</mapper>
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册