`

springMVC+mybatis分页

阅读更多

环境:jdk8 + springMVC + Spring5 + mybatis3.5.1 + jsp + bootstrap3

实现效果:




 

 

 代码实现:

服务端:

封装分页工具类:Page.java

 

import java.util.List;

/**
 * 分页工具类
 * @author xiao沄莲
 */
public class Page<T> {
    /**
     * 当前页
     */
    private int pageNo;
    /**
     * 每页显示量
     */
    private int pageSize;
    /**
     * 总页数
     */
    private int totalPage;
    /**
     * 当前页要显示的所有数据
     */
    private List<T> list;

    public Page() {
        //设置默认显示信息:默认显示第一页,每页默认显示10 条数据
        pageNo = 1;
        pageSize = 3;
    }

    public int getPageNo() {
        return pageNo;
    }

    public void setPageNo(int pageNo) {
        if (pageNo <= 0){
            pageNo = 1;
        }
        this.pageNo = pageNo;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        if (pageSize <= 0){
            pageSize = 3;
        }
        this.pageSize = pageSize;
    }

    public int getTotalPage() {
        return totalPage;
    }

    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }

    public List<T> getList() {
        return list;
    }

    public void setList(List<T> list) {
        this.list = list;
    }
}

  

 

控制层代码:

@Controller
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("list")
    public String list(Page<User> page,User user, Model model){
        page = userService.findAll(page,user);
        model.addAttribute("user",user);
        model.addAttribute("page",page);
        return "userList";
    }

}

 

 

业务层接口代码:

/**
 * 用户业务相关接口
 */
public interface UserService {

    /**
     * 查询所有用户
     * @return 结果
     */
    Page<User> findAll(Page<User> page, User user);

}

 

业务层实现类代码:

/**
 * 用户业务相关节的实现
 *
 * @author 雅绮
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;


    /**
     * 查询所有用户
     *
     * @return 结果
     */
    @Override
    public Page<User> findAll(Page<User> page,User user) {
        //先查询出总条数
        int count = userDao.count(user);
        //计算出总页数
        int totalPage = count / page.getPageSize();
        if (count % page.getPageSize()!=0){
            totalPage ++ ;
        }
        page.setTotalPage(totalPage);
        //查询出当前页显示的数据信息
        int startIndex = (page.getPageNo()-1)*page.getPageSize();
        List<User> userList = userDao.findAll(startIndex,page.getPageSize(),user);
        for (User obj : userList) {
            if (obj == null){
                continue;
            }
            if (obj.getStatus() == Constant.USER_STATUS_NORMAL){
                obj.setStatusStr("正常");
            }else if(obj.getStatus() == Constant.USER_STATUS_STOP){
                obj.setStatusStr("停用");
            }else if(obj.getStatus() == Constant.USER_STATUS_DELETE){
                obj.setStatusStr("删除");
            }else{
                obj.setStatusStr("未知");
            }
        }
        page.setList(userList);
        return page;
    }
}

 

持久层Mapper接口:

@Mapper
public interface UserDao {

    List<User> findAll(@Param("startIndex") int startIndex, @Param("pageSize") int pageSize,
                       @Param("user") User user);

    int count(@Param("user") User user);
}

 sql映射文件:

    <!--条件查询:带分页,当前页的信息-->
    <select id="findAll" resultMap="userResultMap">
        select u.*,r.id role_id,r.name
        <include refid="find_page_common"></include>
        limit #{startIndex},#{pageSize}
    </select>
    <!--条件查询:带分页,统计-->
    <select id="count" resultType="int">
        select count(*)
        <include refid="find_page_common"></include>
    </select>
    <!--条件查询:带分页,SQL重复提取-->
    <sql id="find_page_common">
        from user u
        left join role r ON r.id=u.role_id
        <where>
            <if test="user.realName!=null and user.realName!=''">
                and u.real_name like '%${user.realName}%'
            </if>
            <if test="user.userName!=null and user.userName!=''">
                and u.user_name like '%${user.userName}%'
            </if>
            <if test="user.password!=null and user.password!=''">
                and u.password = #{user.password}
            </if>
            <if test="user.sex!=null and user.sex!=-1">
                and u.sex = #{user.sex}
            </if>
            <if test="user.loginTimeBegin!=null and user.loginTimeBegin!=''">
                <![CDATA[and u.login_time >= #{user.loginTimeBegin}]]>
            </if>
            <if test="user.loginTimeEnd!=null and user.loginTimeEnd!=''">
                <![CDATA[and u.login_time <= #{user.loginTimeEnd}]]>
            </if>
            <if test="user.registerTimeBegin!=null and user.registerTimeBegin!=''">
                <![CDATA[and u.register_time >= #{user.registerTimeBegin}]]>
            </if>
            <if test="user.registerTimeEnd!=null and user.registerTimeEnd!=''">
                <![CDATA[and u.register_time <= #{user.registerTimeEnd}]]>
            </if>
            <if test="user.status!=null and user.status!=-1">
                <![CDATA[and u.del_flag = #{user.status}]]>
            </if>
            <if test="user.mobile!=null and user.mobile!=''">
                <![CDATA[and u.mobile = #{user.mobile}]]>
            </if>
            <if test="user!=null and user.role!=null and user.role.id!=null and user.role.id!=''">
                <![CDATA[and u.role_id = #{user.role.id}]]>
            </if>
        </where>
    </sql>

 

前端:

jsp页面:



 js代码:



 css代码:

/*分页样式*/
.page{
    text-align: center;
    margin-top: 10px;
}
.page>button{
    height: 30px;
    font-size: 14px;
    line-height: 0px;
    font-weight: bold;
    font-family: kaiti;
    background-color: #DDDDDD;
}
.page>input{
    background-color: #DDDDDD;
    width: 60px;
    height: 30px;
    border-radius: 6px;
    text-align: center;
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 大小: 28.3 KB
  • 大小: 23.4 KB
  • 大小: 28.7 KB
  • 大小: 29.3 KB
  • 大小: 358 KB
  • 大小: 133 KB
1
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics