SSM实现登录和分页查询

一. 登录

dao层

User doLogin(String username);

Service层

User doLogin(String username);

SeviceImpl

@Override
public User doLogin(String username) {
    return userDao.doLogin(username);
}

Controller层

//登录
@RequestMapping("/doLogin.do")
//应用于服务器的缓存 Session
public String doLogin(String username, String password, HttpSession session){
    User user=userService.doLogin(username);
    if(user != null)
    {
        if(user.getPassword().equals(password))
        {
            session.setAttribute("user",user);
            return "main";
        }
        else
        {
            session.setAttribute("message","密码错误,请重新输入!");
            return "../login";
        }
    }
    else
    {
        session.setAttribute("message","用户不存在,请重新输入!");
        return "../login";
    }
}

Mapper层

<select id="doLogin" parameterType="java.lang.String" resultType="com.zhongruan.bean.User">
    select * from tb_user where username=#{username}
</select>

二.分页查询

applicationContext.xml中在配置SqlSessionFactory中加入以下代码

<!--分页的配置信息 -->
<property name="plugins">
    <array>
        <bean class="com.github.pagehelper.PageInterceptor">
            <property name="properties">
                <props>
                    <prop key="helperDialect">mysql</prop>
                    <prop key="reasonable">true</prop>
                </props>
            </property>
        </bean>
    </array>
</property>

dao层
// 查询全部

List<User> findAllUser();

Service层
// 分页查询全部

List<User> findAllUser(int page,int size);

ServiceImpl

@Override
public List<User> findAllUser(int page,int size) {
    //告诉 开始分页
    PageHelper.startPage(page, size);

    return userDao.findAllUser();
}

Controller层

// 查询所有
@RequestMapping("findAllUser.do")
public ModelAndView findAllUser(@RequestParam(defaultValue = "1") int page,@RequestParam(defaultValue = "5") int size){
    // 调用 Service方法,去查询得到结果
    List<User> userList = userService.findAllUser(page,size);
    PageInfo pageInfo=new PageInfo(userList);
 //  为了进行返回值传递,需要把返回值封装到SpringMVC内置对象ModelAndView里
    ModelAndView mv = new ModelAndView();
    mv.addObject("pageInfo",pageInfo);
    mv.setViewName("user-list");
    return mv;
}

jsp页面中

<c:forEach var="user" items="${pageInfo.list}">
   <tr>
      <td><input name="ids" type="checkbox"></td>
      <td>${user.id}</td>
      <td>${user.username}</td>
      <td>${user.password}</td>
      <td class="text-center">
         <a href="${pageContext.request.contextPath}/user/findUpdUser.do?id=${user.id}" class="btn bg-olive btn-xs">更新</a>
         <a href="${pageContext.request.contextPath}/user/deleteUser.do?id=${user.id}" class="btn bg-olive btn-xs">删除</a>
         <a href="#" class="btn bg-olive btn-xs">添加角色</a>
      </td>
   </tr>
</c:forEach>

首页,上一页,尾页,下一页

</div>
<!-- /.box-body -->
   <div class="box-tools pull-right">
      <ul class="pagination">
         <li><a href="${pageContext.request.contextPath}/user/findAllUser.do?page=1&size=5" aria-label="Previous">首页</a></li>
         <li><a href="${pageContext.request.contextPath}/user/findAllUser.do?page=${pageInfo.pageNum-1}&size=5">上一页</a></li>

         <c:forEach begin="1" end="${pageInfo.pages}" var="pageNum">
            <li><a href="${pageContext.request.contextPath}/user/findAllUser.do?page=${pageNum}&size=5">${pageNum}</a></li>
         </c:forEach>

         <li><a href="${pageContext.request.contextPath}/user/findAllUser.do?page=${pageInfo.pageNum+1}&size=5">下一页</a></li>
         <li><a href="${pageContext.request.contextPath}/user/findAllUser.do?page=${pageInfo.pages}&size=5" aria-label="Next">尾页</a></li>
      </ul>
   </div>

</div>
<!-- /.box-footer-->

本文结束

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页