web 层,接收前台信息,然后调用 service 层,返回结果集
package com.xxxx.note.web;
import com.xxxx.note.po.User;
import com.xxxx.note.service.UserService;
import com.xxxx.note.vo.ResultInfo;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
@WebServlet("/user")
public class UserServlet extends HttpServlet {
private UserService userService = new UserService();
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String actionName = request.getParameter("actionName");
if ("login".equals(actionName)) {
userLogin(request, response);
}
}
private void userLogin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String userName = request.getParameter("userName");
String userPwd = request.getParameter("userPwd");
ResultInfo<User> resultInfo = userService.userLogin(userName, userPwd);
if (resultInfo.getCode() == 1) {
request.getSession().setAttribute("user", resultInfo.getResult());
String rem = request.getParameter("rem");
if ("1".equals(rem)) {
Cookie cookie = new Cookie("user", userName + "-" + userPwd);
cookie.setMaxAge(60 * 60);
response.addCookie(cookie);
} else {
Cookie cookie = new Cookie("user", null);
cookie.setMaxAge(0);
response.addCookie(cookie);
}
response.sendRedirect("index.jsp");
} else {
request.setAttribute("resultInfo", resultInfo);
request.getRequestDispatcher("login.jsp").forward(request, response);
}
}
}
service 层:进行逻辑判断,返回结果集
package com.xxxx.note.service;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.DigestUtil;
import com.xxxx.note.dao.UserDao;
import com.xxxx.note.po.User;
import com.xxxx.note.vo.ResultInfo;
public class UserService {
private UserDao userDao = new UserDao();
public ResultInfo<User> userLogin(String userName, String userPwd) {
ResultInfo<User> resultInfo = new ResultInfo<>();
User u = new User();
u.setUname(userName);
u.setUpwd(userPwd);
resultInfo.setResult(u);
if (StrUtil.isBlank(userName) || StrUtil.isBlank(userPwd)) {
resultInfo.setCode(0);
resultInfo.setMsg("用户的名称或密码是否为空");
return resultInfo;
}
User user = userDao.queryUserByName(userName);
if (user == null) {
resultInfo.setCode(0);
resultInfo.setMsg("该用户不存在");
return resultInfo;
}
userPwd = DigestUtil.md5Hex(userPwd);
if (!userPwd.equals(user.getUpwd())) {
resultInfo.setCode(0);
resultInfo.setMsg("用户密码不正确");
return resultInfo;
}
resultInfo.setCode(1);
resultInfo.setResult(user);
return resultInfo;
}
}
dao 层:与数据库做交互 创建连接 定义 sql 语句 预编译 设置参数 返回结果集 关闭连接 返回 User 对象
package com.xxxx.note.dao;
import com.xxxx.note.po.User;
import com.xxxx.note.util.DBUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class UserDao {
public User queryUserByName(String userName) {
User user = null;
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
connection = DBUtil.getConnection();
String sql = "select * from tb_user where uname = ?";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, userName);
resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
user = new User();
user.setUserId(resultSet.getInt("userId"));
user.setUname(userName);
user.setHead(resultSet.getString("head"));
user.setNick(resultSet.getString("nick"));
user.setMood(resultSet.getString("mood"));
user.setUpwd(resultSet.getString("upwd"));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DBUtil.close(resultSet, preparedStatement, connection);
}
return user;
}
}
po 层:User 对象
package com.xxxx.note.po;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class User {
private Integer userId;
private String uname;
private String upwd;
private String nick;
private String head;
private String mood;
}
util 层:工具类,创建连接,关闭连接
package com.xxxx.note.util;
import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;
public class DBUtil {
private static Properties properties = new Properties();
static {
try {
InputStream in = DBUtil.class.getClassLoader().getResourceAsStream("db.properties");
properties.load(in);
Class.forName(properties.getProperty("jdbcName"));
} catch (Exception e) {
e.printStackTrace();
}
}
public static Connection getConnection() throws SQLException {
Connection connection;
String dbUrl = properties.getProperty("dbUrl");
String dbName = properties.getProperty("dbName");
String dbPwd = properties.getProperty("dbPwd");
connection = DriverManager.getConnection(dbUrl, dbName, dbPwd);
return connection;
}
public static void close(ResultSet resultSet, PreparedStatement preparedStatement, Connection connection) {
try {
if (resultSet != null) {
resultSet.close();
}
if (preparedStatement != null) {
preparedStatement.close();
}
if (connection != null) {
connection.close();
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
vo 层: 结果集,有响应码,提示信息和返回对象
package com.xxxx.note.vo;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class ResultInfo<T> {
private Integer code;
private String msg;
private T result;
}
|