protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.设置编码
request.setCharacterEncoding("utf-8");
//2.获取参数
Map map = request.getParameterMap();
//3.封装对象
User user = new User();
try {
BeanUtils.populate(user,map);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
//4.调用Service保存
UserService service = new UserServiceImpl();
service.addUser(user);
//5.跳转到userListServlet
response.sendRedirect(request.getContextPath()+"/findUserByPageServlet");
}
3:UserServiceImpl(addUser)
@Override
public void addUser(User user) {
dao.add(user);
}
4:UserDaoImpl(add)
@Override
public void add(User user) {
//1.定义sql
String sql = "insert into user values(null,?,?,?,?,?,?,null,null)";
//2.执行sql
template.update(sql, user.getName(), user.getGender(), user.getAge(), user.getAddress(), user.getQq(), user.getEmail());
}
四:删除单个用户信息(一):添加删除按钮
删除
(二):后端代码实现1:删除功能的思路分析
2:DelUserServlet(先获取id,通过id,删除用户信息)
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.获取id
String id = request.getParameter("id");
//2.调用service删除
UserService service = new UserServiceImpl();
service.deleteUser(id);
//3.跳转到查询所有Servlet
response.sendRedirect(request.getContextPath()+"/findUserByPageServlet");
}
3:UserServiceImpl(deleteUser)
@Override
public void deleteUser(String id) {
dao.delete(Integer.parseInt(id));
}
4:UserDaoImpl(delete)
@Override
public void delete(int id) {
//1.定义sql
String sql = "delete from user where id = ?";
//2.执行sql
template.update(sql, id);
}
五:修改用户信息(一):修改用户信息界面1:html界面
2:html代码
修改联系人
3:改进后的jsp修改界面 4:页面改进后的代码
添加可以自由选择性别和籍贯的判断
修改联系人
(二):后端代码实现1:修改功能的思路分析
2:FindUserServlet
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.获取id
String id = request.getParameter("id");
//2.调用Service查询
UserService service = new UserServiceImpl();
User user = service.findUserById(id);
//3.将user存入request
request.setAttribute("user",user);
//4.转发到update.jsp
request.getRequestDispatcher("/update.jsp").forward(request,response);
}
3:UpdateUserServlet
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.设置编码
request.setCharacterEncoding("utf-8");
//2.获取map
Map map = request.getParameterMap();
//3.封装对象
User user = new User();
try {
BeanUtils.populate(user,map);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
//4.调用Service修改
UserService service = new UserServiceImpl();
service.updateUser(user);
//5.跳转到查询所有Servlet
response.sendRedirect(request.getContextPath()+"/findUserByPageServlet");
}
4:UserServiceImpl(updateUser)
@Override
public void updateUser(User user) {
dao.update(user);
}
5:UserDaoImpl(update)
@Override
public void update(User user) {
String sql = "update user set name = ?,gender = ? ,age = ? , address = ? , qq = ?, email = ? where id = ?";
template.update(sql, user.getName(), user.getGender(), user.getAge(), user.getAddress(), user.getQq(), user.getEmail(), user.getId());
}
复杂功能一:删除选中
(一):删除选中界面1:在list页面添加删除选中的按钮,并且给删除选中添加单击事件
删除选中
window.onload = function(){
//给删除选中按钮添加单击事件
document.getElementById("delSelected").onclick = function(){
if(confirm("您确定要删除选中条目吗?")){
var flag = false;
//判断是否有选中条目
var cbs = document.getElementsByName("uid");
for (var i = 0; i < cbs.length; i++) {
if(cbs[i].checked){
//有一个条目选中了
flag = true;
break;
}
}
if(flag){//有条目被选中
//表单提交
document.getElementById("form").submit();
}
}
}
}
2:删除选中时可以添加全部选中
//1.获取第一个cb
document.getElementById("firstCb").onclick = function(){
//2.获取下边列表中所有的cb
var cbs = document.getElementsByName("uid");
//3.遍历
for (var i = 0; i < cbs.length; i++) {
//4.设置这些cbs[i]的checked状态 = firstCb.checked
cbs[i].checked = this.checked;
}
}
(二):后端代码实现1:删除选中的思路分析
2:DelSelectedServlet
public class DelSelectedServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.获取所有id
String[] ids = request.getParameterValues("uid");
//2.调用service删除
UserService service = new UserServiceImpl();
service.delSelectedUser(ids);
//3.跳转查询所有Servlet
response.sendRedirect(request.getContextPath()+"/findUserByPageServlet");
}
3:UserService(delSelectedUser)
//删除选中用户
@Override
public void delSelectedUser(String[] ids) {
if(ids != null && ids.length > 0){
//1.遍历数组
for (String id : ids) {
//2.调用dao删除
dao.delete(Integer.parseInt(id));
}
}
}
二:分页查询(一):分页查询页面
1:在list.jsp中添加组件分页栏
(二):后端代码实现1:分页查询实体类的思路分析
1:添加一个分页查询相关的类
public class PageBean {
private int totalCount; // 总记录数
private int totalPage ; // 总页码
private List list ; // 每页的数据
private int currentPage ; //当前页码
private int rows;//每页显示的记录数
//分页条件查询
@Override
public PageBean findUserByPage(String _currentPage, String _rows, Map condition) {
int currentPage = Integer.parseInt(_currentPage);
int rows = Integer.parseInt(_rows);
//第1页的上一页永远返回1
if(currentPage <=0) {
currentPage = 1;
}
//1.创建空的PageBean对象
PageBean pb = new PageBean();
//2.设置参数
pb.setCurrentPage(currentPage);
pb.setRows(rows);
//3.调用dao查询总记录数
int totalCount = dao.findTotalCount(condition);
pb.setTotalCount(totalCount);
//4.调用dao查询List集合
//计算开始的记录索引
int start = (currentPage - 1) * rows;
List list = dao.findByPage(start,rows,condition);
pb.setList(list);
//5.计算总页码
int totalPage = (totalCount % rows) == 0 ? totalCount/rows : (totalCount/rows) + 1;
pb.setTotalPage(totalPage);
return pb;
}
5:UserDaoImpl(findTotalCount)
//查询总记录数
@Override
public int findTotalCount() {
//1.定义模板初始化sql
String sql = "select count(*) from user";
return template.queryForObject(sql,Integer.class);
}
6:UserDaoImpl(findByPage)
//分页条件查询
@Override
public List findByPage(int start, int rows) {
String sql = "select * from user limit?,? ;
return template.query(sql,new BeanPropertyRowMapper,start,rows);
}
//查询总记录数
@Override
public int findTotalCount(Map condition) {
//1.定义模板初始化sql
String sql = "select count(*) from user where 1 = 1 ";
StringBuilder sb = new StringBuilder(sql);
//2.遍历map
Set keySet = condition.keySet();
//定义参数的集合
List
5:在分页查询的UserDaoImpl(findByPage)
基础上遍历复杂条件查询的集合并为查询到的值做分页处理
//分页条件查询
@Override
public List findByPage(int start, int rows, Map condition) {
String sql = "select * from user where 1 = 1 ";
StringBuilder sb = new StringBuilder(sql);
//2.遍历map
Set keySet = condition.keySet();
//定义参数的集合
List params = new ArrayList();
for (String key : keySet) {
//排除分页条件参数
if("currentPage".equals(key) || "rows".equals(key)){
continue;
}
//获取value
String value = condition.get(key)[0];
//判断value是否有值
if(value != null && !"".equals(value)){
//有值
sb.append(" and "+key+" like ? ");
params.add("%"+value+"%");//?条件的值//模糊查询
}
}
//添加分页查询
sb.append(" limit ?,? ");
//添加分页查询参数值
params.add(start);
params.add(rows);
sql = sb.toString();
System.out.println(sql);
System.out.println(params);
return template.query(sql,new BeanPropertyRowMapper(User.class),params.toArray());
}
评论列表(0条)