一.实体层
User.java
Pager.java
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Pager {
private Object data;//用于响应的数据
private int pageTotal;//总条数
}
二.创建UserMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="demo.dao.UserMapper">
</mapper>
三.通过ResultMap建立Use实体和t_user表的映射关系
<!--建立映射,将数据库中表t_user和java实体类user进行关联 -->
<resultMap id="userResult" type="User">
<result column="id" property="id"></result>
<result column="user_name" property="userName"></result>
<result column="passwd" property ="passwd"></result>
<result column="create_date" property="createDate"></result>
</resultMap>
四.在UserMapper中添加查询所有用户的方法
UserMapper.java
<sql id="s1">
select id,user_name,passwd,create_date from t_user
</sql>
<select id="selectAll" resultMap="userResult">
<include refid="s1"></include> limit #{pageIndex},#{pageSize}
</select>
<select id="selectCount" resultType="int">
select COUNT(id) from t_user
</select>
</mapper>
五.在IUserService中添加业务方法
IUService接口
UserServiceImpl实现类
六.在UserCOntroller中测试
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private IUserService userService;
@RequestMapping("/list")
@CrossOrigin(origins ="*")//允许所有跨源请求
public Pager select(int pageIndex, int pageSize){
return userService.queryUserList(pageIndex, pageSize);
}
}