mybatis分页代码举个例子
时间: 2023-04-04 10:01:47 浏览: 65
好的,以下是一个简单的 MyBatis 分页代码示例:
```
// 在 Mapper 接口中定义方法
List<User> selectUserByPage(@Param("start") int start, @Param("pageSize") int pageSize);
// 在 Mapper XML 文件中编写 SQL 语句
<select id="selectUserByPage" resultType="User">
SELECT * FROM user LIMIT #{start}, #{pageSize}
</select>
// 在 Service 层中调用 Mapper 方法并传入分页参数
int currentPage = 1;
int pageSize = 10;
int start = (currentPage - 1) * pageSize;
List<User> userList = userMapper.selectUserByPage(start, pageSize);
```
相关问题
mybatis分页查询 多条件查询 有in
可以使用MyBatis提供的分页插件PageHelper进行分页查询,同时可以在Mapper.xml文件中使用动态SQL进行多条件查询和in查询。以下是一个例子:
```
<!-- Mapper.xml文件中的查询语句 -->
<select id="findUsers" resultMap="userResultMap">
SELECT * FROM users
<where>
<if test="name != null and name != ''">
AND name = #{name}
</if>
<if test="age != null">
AND age = #{age}
</if>
<if test="ids != null and ids.size() > 0">
AND id IN
<foreach collection="ids" item="id" open="(" separator="," close=")">
#{id}
</foreach>
</if>
</where>
</select>
```
在Java代码中,使用PageHelper.startPage方法设置分页参数,并调用Mapper中的findUsers方法进行查询:
```
// 设置分页参数,第一页,每页2条数据
PageHelper.startPage(1, 2);
// 构造查询条件
Map<String, Object> params = new HashMap<>();
params.put("name", "张三");
params.put("age", 20);
List<Integer> ids = new ArrayList<>();
ids.add(1);
ids.add(2);
params.put("ids", ids);
// 调用Mapper中的findUsers方法进行查询
List<User> users = userMapper.findUsers(params);
// 获取分页信息
PageInfo<User> pageInfo = new PageInfo<>(users);
System.out.println("总记录数:" + pageInfo.getTotal());
System.out.println("总页数:" + pageInfo.getPages());
System.out.println("当前页码:" + pageInfo.getPageNum());
System.out.println("每页记录数:" + pageInfo.getPageSize());
System.out.println("当前页的记录数:" + pageInfo.getSize());
System.out.println("当前页的第一条记录:" + pageInfo.getStartRow());
System.out.println("当前页的最后一条记录:" + pageInfo.getEndRow());
```
注意:在使用PageHelper进行分页查询时,需要在MyBatis的配置文件中配置分页插件:
```
<!-- MyBatis配置文件 -->
<configuration>
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor"/>
</plugins>
</configuration>
```
mybatis plus分页 union
根据提供的引用内容,我了解到你想了解如何在MyBatis Plus中使用分页和联接查询。在MyBatis Plus中,可以使用`Page`对象进行分页查询,并且可以使用`Wrapper`对象进行多表联接查询。
以下是一个示例,演示了如何在MyBatis Plus中进行分页和联接查询:
```java
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class MyService {
@Autowired
private MyMapper myMapper;
public Page<Question> getAllQuestionWithStudentByPage(int pageNum, int pageSize) {
Page<Question> page = new Page<>(pageNum, pageSize);
QueryWrapper<Question> queryWrapper = new QueryWrapper<>();
queryWrapper.select("question.*", "student.name as studentName")
.eq("question.student_id", "student.id")
.orderByDesc("question.create_time");
return myMapper.selectPage(page, queryWrapper);
}
}
```
上述示例中,我们使用`Page`对象来指定分页的页码和每页的记录数。然后,我们使用`QueryWrapper`对象来构建多表联接查询的条件。在这个例子中,我们查询了`question`表和`student`表,并且通过`eq`方法指定了它们之间的关联条件。最后,我们使用`myMapper.selectPage`方法执行查询,并返回分页结果。
请注意,上述示例中的`Question`和`MyMapper`是示例代码中的类和接口,你需要根据你的实际情况进行替换。