1:原理
底层修改SQL语句。
2:修改pom.xml文件,添加依赖
<!-- 整合mybatis分页插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.5</version>
</dependency>
3:用法
package com.zhaoy.datasourceipp1.mapper;
import java.util.List;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import com.zhaoy.entity.Comment;
public interface CommentMapper1 {
@Insert("INSERT INTO COMMENT(ID,PRODUCT_ID,COMMENT_TIME,IS_DELETED) VALUE "
+ "(#{id}, #{productId}, #{commentTime}, #{isDeleted})")
int insertComment(@Param("id") String id, @Param("productId") String productId,
@Param("commentTime") String commentTime, @Param("isDeleted") Boolean isDeleted);
@Results({@Result(property = "id", column = "ID", id = true),
@Result(property = "productId", column = "product_id"),
@Result(property = "CommentTime", column = "comment_time"),
@Result(property = "isDeleted", column = "is_deleted")})
@Select("SELECT * FROM COMMENT")
List<Comment> selCommentList();
}
package com.zhaoy.datasourceipp1.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhaoy.datasourceipp1.mapper.CommentMapper1;
import com.zhaoy.datasourceipp2.mapper.CommentMapper2;
import com.zhaoy.entity.Comment;
@Service
public class CommentService1 {
@Autowired
private CommentMapper1 commentMapper1;
public PageInfo<Comment> selCommentList(int pageNum, int pageSize){
System.out.println("pageNum: " + pageNum + " pageSize: " + pageSize);
PageHelper.startPage(pageNum, pageSize);
List<Comment> selCommentList = commentMapper1.selCommentList();
PageInfo<Comment> list = new PageInfo<Comment>(selCommentList);
return list;
}
}