1.项目代码分割
一共分为 3个主要模块
页面分发模块,服务模块,数据库操作模块
注意:关于用户操作,用户的数据要封装在用户对象中,进行传输
1.登陆模块
验证码,账户,密码等验证
验证码生成存于Session中
1.页码需要提交,验证码,账户,密码
2.页面分发模块,获取验证码,账户,密码,进行非空校验
3.获取验证码与session中的匹配
4.封装User对象
//req.getParameterMap();方法可以将传递过来的数据封装为map集合
Map<String, String[]> map = req.getParameterMap();
User user = new User();
try {
//populate将map集合中的键值对封装到User对象中
BeanUtils.populate(user,map);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
4.调用服务,将User传递到服务层,服务服务层根据查询数据库的结果返回boolean类型的值
5.根据返回结果进行页面跳转
req.getRequestDispatcher("/index.jsp").forward(req,resp);
或者
req.getRequestDispatcher("/login.jsp").forward(req,resp);
注意如果有错误,需要返回提示信息
2.显示模块、PageBean、分页
2.1 PageBean
功能:对于分页需要的数据进行封装
- private int totalcount;//总条数
- private int totalPage;//总页码
- private List list;//每页显示的列表
- private int currentPage;//当前页码
- private int rows;//每页显示的条数
- private Map map;//查询条件集合
‘’’’’’’’’’’’’’’’’
其中:
总条数
等于:调用dao中的getTotalcount方法
需要有,查询条件集合(map),查询什么的总条数
步骤:
1.定义sql模板
2.遍历map集合,根据集合进行sql语句组装
3.组装好的sql进行查询
public int getTotalcount(Map<String, String[]> map) {
String sql = "select count(*) from user where 1 = 1 ";
StringBuffer a = new StringBuffer();
a.append(sql);
List<Object> arr = new ArrayList<Object>();
Set<String> key = map.keySet();
for (String s : key) {
if(s.equals("currentPage")||s.equals("rows")){//map中存在错误字段
continue;
}
String value = map.get(s)[0];
if(value!=null&&!"".equals(value)){
a.append(" and "+s+" like ? ");
arr.add("%"+value+"%");
}
}
sql = a.toString();
return template.queryForObject(sql, Integer.class,arr.toArray());
}
总页码
等于,总条数%每页显示的条数==0?总条数/每页显示的条数 :总条数/每页显示的条数+1
每页显示的列表
需要三个参数,查询条件集合、当前页码、每页显示的条数
使用关键字where 与 limit
@Override
public List<User> getUserList(int stat, int row, Map<String, String[]> map) {
String sql = "select * from user where 1 = 1";
StringBuffer a = new StringBuffer();
a.append(sql);
List<Object> arr = new ArrayList<Object>();
Set<String> key = map.keySet();
for (String s : key) {
if(s.equals("currentPage")||s.equals("rows")){
continue;
}
String value = map.get(s)[0];
if(value!=null&&!"".equals(value)){
a.append(" and "+s+" like ? ");
arr.add("%"+value+"%");
}
}
a.append(" limit ?,? ");
arr.add(stat);
arr.add(row);
sql = a.toString();
List<User> list = template.query(sql, new BeanPropertyRowMapper<User>(User.class),arr.toArray());
return list;
}
当前页码、每页显示的条数、查询条件集合
需要由页面提供,如果页码不提供,需要对其进行非空判断,如果为null
需要赋予默认值
如:当前页码默认为1,显示条数默认为10,查询条件集合默认为null
注意查询条件遍历前需要进行非空校验。如果为null,默认查询所有
3.添加、删除、修改功能
简单的操作数据库,查询完要重定向到,查询首页的方法中
resp.sendRedirect(req.getContextPath()+"/findPageBeanServlet");
303

被折叠的 条评论
为什么被折叠?



