今天是在昨天的项目中添加登入,添加,删除,修改的简单操作。
简单功能
1. 列表查询
2. 登录
3. 添加
4. 删除
5. 修改
列表查询的功能昨天的代码已经简单实现,今天实现下面的几个功能。后面还有比较复杂的功能:删除选中,分页查询,复杂条件查询明天实现。
UserDao.java
/**
* 用于登录
* @param user
* @return
*/
User findByUserNameAndPassword(User user);
UserDaoImpl.java
@Override
public User findByUserNameAndPassword(User user) {
String sql = "select * from user where username=? and password=?";
User u=null;
try {
u= template.queryForObject(sql, new BeanPropertyRowMapper<>(User.class), user.getUsername(), user.getPassword());
}catch(Exception ex){
}
return u;
}
虽然我也不知道为什么要定义接口,老师说以后业务如果复杂,接口会带来很多方便。这里不明觉厉吧,跟着写。
UserService.java
/**
* 登录
* @param user
* @return
*/
User login(User user);
UserServiceImpl.java
@Override
public User login(User user) {
return userDao.findByUserNameAndPassword(user);
}
@WebServlet(value = "/UserLoginServlet")
public class UserLoginServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//处理请求乱码
request.setCharacterEncoding("utf-8");
//接收验证码
String verifycode = request.getParameter("verifycode");
//获取服务器端产生的验证码
HttpSession session = request.getSession();
String server_verifycode=(String)session.getAttribute("CHECKCODE_SERVER");
//获取完服务器验证码后,立即从服务器端删除
session.removeAttribute("CHECKCODE_SERVER");
//比较验证码
if(verifycode!=null&&verifycode.equalsIgnoreCase(server_verifycode)){
//如果验证码正确
//接收前台传过来的用户名和密码
Map<String, String[]> parameterMap = request.getParameterMap();
//将用户名和密码封装到User对象中
User user = new User();
try {
BeanUtils.populate(user,parameterMap);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
//调用业务层进行验证
UserService userService = new UserServiceImpl();
User u=userService.login(user);
//判断是否登录成功
if(u==null){
//登录失败
request.setAttribute("errormsg","用户名或密码出错");
request.getRequestDispatcher("/login.jsp").forward(request,response);
}else{
//登录成功
session.setAttribute("user",u);
//重定向到index.jsp
response.sendRedirect(request.getContextPath()+"/index.jsp");
}
}else{
//如果验证码出错
request.setAttribute("errormsg","验证码出错");
request.getRequestDispatcher("/login.jsp").forward(request,response);
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
管理员登录
管理员登录
${errormsg}
UserDao.java
/**
* 添加用户
* @param user
*/
void add(User user);
UserDaoImpl.java
@Override
public void add(User user) {
String sql ="insert into user(name,gender,age,address,qq,email) values(?,?,?,?,?,?)";
template.update(sql,user.getName(),user.getGender(),user.getAge(),user.getAddress(),user.getQq(),user.getEmail());
}
UserService.java
/**
* 添加用户
*/
void add(User user);
UserServiceImpl.java
@Override
public void add(User user) {
userDao.add(user);
}
添加AddUserServlet处理添加请求
AddUserServlet.java
@WebServlet(value = "/AddUserServlet")
public class AddUserServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//乱码处理
request.setCharacterEncoding("utf-8");
//接收数据并封装到User对象中
Map<String, String[]> parameterMap = request.getParameterMap();
User user= new User();
try {
BeanUtils.populate(user,parameterMap);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
//调用业务层保存用户信息
UserService userService = new UserServiceImpl();
userService.add(user);
//
//request.getRequestDispatcher("/list.jsp").forward(request,response);
//跳转到UserListServlet重新查询数据库并跳转到list.jsp显示
response.sendRedirect(request.getContextPath()+"/UserListServlet");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
2、add.jsp
UserDao.java
/**
* 根据id删除指定用户
* @param id
*/
void delete(int id);
UserDaoImpl.java
@Override
public void delete(int id) {
String sql = "delete from user where id=?";
template.update(sql,id);
}
UserService.java
/**
* 删除用户
*/
void delete(String id);
UserServiceImpl.java
@Override
public void delete(String id) {
int int_i = Integer.parseInt(id);
userDao.delete(int_i);
}
添加DelUserServlet用于处理删除请求
@WebServlet(value = "/DelUserServlet")
public class DelUserServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//接收id
String id = request.getParameter("id");
//调用业务层进行删除
UserService userService = new UserServiceImpl();
userService.delete(id);
//跳转到UserListServlet重新查询
response.sendRedirect(request.getContextPath()+"/UserListServlet");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
list.jsp
定义了一个方法,在删除之前要执行确定操作。
刚开始写这个有点蒙,多写几个功能之后发现套路差不多,学习java,感觉要不停的敲,在敲代码中领悟,量的积累达到质的飞越!加油!