登入系统终极版
程序员文章站
2022-07-03 17:31:33
...
今天老师把登入系统讲完了,优化了分页在首页和尾页的基础上有了123的页数和上一页下一页
<div>
<a href="/findByPage?currentPage=1&size=5">首页</a>
<c:if test="${pageInfo.currentPage==1}">
<a href="/findByPage?currentPage=1&size=5">上一页</a>
</c:if>
<c:if test="${pageInfo.currentPage>1}">
<a href="/findByPage?currentPage=${pageInfo.currentPage-1}&size=5">上一页</a>
</c:if>
<c:forEach begin="1" end="${pageInfo.totalPage}" var="i">
<a href="/findByPage?currentPage=${i}&size=5">${i}</a>
</c:forEach>
<c:if test="${pageInfo.currentPage==pageInfo.totalPage}">
<a href="/findByPage?currentPage=${pageInfo.totalPage}&size=5">下一页</a>
</c:if>
<c:if test="${pageInfo.currentPage<pageInfo.totalPage}">
<a href="/findByPage?currentPage=${pageInfo.currentPage+1}&size=5">下一页</a>
</c:if>
<a href="/findByPage?currentPage=${pageInfo.totalPage}&size=5">尾页</a>
</div>
然后老师对整个系统进行了优化,变得更加简洁明了
@Override
public List<User> findByPage(String username, int start, int size) {
List<User> users=new ArrayList<>();
Connection connection=null;
PreparedStatement statement=null;
ResultSet resultSet=null;
String sql=null;
try {
connection = DBUtil.getConnection();
if (username == null) {
sql = "select * from tb_user limit ?,?";
statement = connection.prepareStatement(sql);
statement.setInt(1, start);
statement.setInt(2, size);
} else {
sql = "select * from tb_user where username like ? limit ?,?";
statement = connection.prepareStatement(sql);
statement.setString(1, "%" + username + "%");
statement.setInt(2, start);
statement.setInt(3, size);
}
resultSet = statement.executeQuery();
while (resultSet.next()){
User user=new User();
user.setId(resultSet.getInt(1));
user.setUsername(resultSet.getString(2));
user.setPassword(resultSet.getString(3));
users.add(user);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DBUtil.closeALL(resultSet,statement,connection);
}
return users;
}
package com.zhongruan.service.impl;
import com.zhongruan.dao.IUserDao;
import com.zhongruan.dao.impl.UserDaolmpl;
import com.zhongruan.model.User;
import com.zhongruan.pojo.PageInfo;
import com.zhongruan.service.IUserService;
import javax.jws.soap.SOAPBinding;
import java.sql.SQLException;
import java.util.List;
public class UserServiceImpl implements IUserService {
IUserDao userDao=new UserDaolmpl();
@Override
public Boolean login(String username, String password) {
User user = userDao.finduserByusername(username);
if(user!=null&& user.getPassword().equals(password)){
return true;
}else {
return false;
}
}
@Override
public List<User> findAll() {
List<User> users = userDao.findAll();
return users;
}
@Override
public void delete(int id) {
userDao.delete(id);
}
@Override
public void add(String username, String password) {
userDao.add(username,password);
}
@Override
public User findUserById(int id) {
return userDao.findUserById(id);
}
@Override
public void update(int id, String username, String password) {
userDao.update(id,username,password);
}
@Override
public PageInfo findByPage(String searchname,int currentPage, int size) {
PageInfo pageInfo=new PageInfo();
pageInfo.setCurrentPage(currentPage);
pageInfo.setSize(size);
//1 0 2 5 3 10 4 15........
int start=(currentPage-1)*size;
List<User> users = userDao.findByPage(searchname,start, size);
pageInfo.setList(users);
int count = userDao.selectCount();
pageInfo.setTotalCount(count);
Double c=Double.valueOf(count);
Double ceil = Math.ceil(c / size);
pageInfo.setTotalPage(ceil.intValue());
return pageInfo;
}
}
最终效果: