SpringBoot整合Mybatis分页插件
程序员文章站
2022-07-15 10:26:45
...
添加pom.xml
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.5</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<!-- MYSQL包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
配置application.properties
spring.datasource.url=jdbc:mysql://localhost:3306/springboot-learning
spring.datasource.password=root
spring.datasource.username=root
# 驼峰命名规范 如:数据库字段是 order_id 那么 实体字段就要写成 orderId
mybatis.configuration.map-underscore-to-camel-case=true
#配置mybatis xml配置文件
mybatis.mapper-locations=classpath:mapper/*.xml
# 设置打印mybatis SQL语句
logging.level.com.lzc.mybatis.mapper=debug
########## 分页插件 ##########
pagehelper.helper-dialect=mysql
pagehelper.params=count=countSql
pagehelper.reasonable=false
pagehelper.support-methods-arguments=true
新建一张user表
CREATE TABLE `user` (
`user_id` int(8) NOT NULL AUTO_INCREMENT COMMENT '主键自增',
`name` varchar(50) NOT NULL COMMENT '用户名',
`password` varchar(50) NOT NULL COMMENT '密码',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='用户表';
User实体类
package com.lzc.mybatis.dataobject;
public class User {
private Integer userId;
private String name;
private String password;
public User() {
}
public User(String name, String password) {
this.name = name;
this.password = password;
}
public User(Integer userId, String name, String password) {
this.userId = userId;
this.name = name;
this.password = password;
}
// 省略get、set方法
}
新建UserMapper.java
package com.lzc.mybatis.mapper;
import com.lzc.mybatis.dataobject.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@Mapper
public interface UserMapper{
@Select("select * from user")
List<User> getAll();
}
编写测试类
@RunWith(SpringRunner.class)
@SpringBootTest
public class UserMapperTest {
@Autowired
private UserMapper userMapper;
@Test
public void getAll() {
List<User> users = new ArrayList<>();
PageHelper.startPage(2, 4);
PageInfo<User> userPageInfo = new PageInfo<>(userMapper.getAll());
System.out.println(userPageInfo);
}
}
推荐阅读
-
SpringBoot无废话入门04:MyBatis整合
-
SpringBoot项目中分页插件PageHelper无效的问题及解决方法
-
springboot+springmvc+mybatis项目整合
-
SpringBoot 整合jdbc和mybatis
-
SpringBoot整合mybatis访问时报错Invalid bound statement (not found)
-
mybatis中的分页插件
-
Mybatis分页PageHelper插件代码实例
-
MyBatis 分页插件 PageHelper 使用
-
SpringBoot整合Mybatis,SpringMVC简单例子
-
【SpringBoot】廿六、SpringBoot中整合MyBatis-Plus