JavaWeb开发——Mybatis+Tomcat(三)

JavaWeb三层结构

1.Mybatis+Mapper——配置文件
2.{dao实现类—dao接口类}——{service实现类—service接口类}——servlet
3.jsp(一个servlet类对应一个jsp页面)

以学生登录为例,以token存储用户登录状态,也可以直接判断密码进行登录
1)配置文件

Mybatis框架实现MySQL数据库的连接(一)

2)
dao接口类
public interface StudentsDao {
	boolean studentLogin(String stuId, String stuPassword) throws IOException, SQLException;
}
dao实现类
public class StudentsDaoImpl implements StudentsDao{
	@Override
	public boolean studentLogin(String stuId, String stuPassword) throws IOException, SQLException {
		// TODO Auto-generated method stub
		SqlSessionFactoryBuilder sfb = new SqlSessionFactoryBuilder();
		InputStream ins = Resources.getResourceAsStream("students.xml");		
		SqlSessionFactory ssf = sfb.build(ins);
		SqlSession session = ssf.openSession();
		
		Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("stuId", stuId);
        paramMap.put("stuPassword", stuPassword);
        Map<String, Object> studentMap = session.selectOne("dao.students.StudentsDaoImpl.studentLogin", paramMap);
        if(studentMap.isEmpty()) {
        	return false;
        }
        else {
        	return true;
        }
	}
}
service接口类
public interface StudentsService {
	boolean studentLogin(String stuId, String stuPassword, HttpServletRequest request) throws IOException, SQLException;
}
service实现类
public class StudentsServiceImpl implements StudentsService{
	private StudentsDao students = new StudentsDaoImpl();

	@Override
	public boolean studentLogin(String stuId, String stuPassword, HttpServletRequest request)
			throws IOException, SQLException {
		// TODO Auto-generated method stub
		boolean flag = students.studentLogin(stuId, stuPassword);
	  	  
        if (flag) {
        	boolean stoken = students.checkStuId(stuId);	//判断学生是否已登录
        	if (stoken) {
				students.updateStoken(stuId);	//已登录则更新token值
			}
        	else {
            	students.addStoken(stuId);	//未登录则生成token值
			}
        }
        else {
        	flag = false;
        }
        return flag;
	}
}
servlet类:登录成功后,将(学生ID,token值)以json格式打包返回给前端
@SuppressWarnings("serial")
@WebServlet("/StudentLoginServlet")
public class StudentLoginServlet extends HttpServlet {
	private StudentsService students = new StudentsServiceImpl();
	
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
	            throws ServletException, IOException {
	        try {
	            String stuId = request.getParameter("stuId");
	            String stuPassword = request.getParameter("stuPassword");
	            boolean flag = students.studentLogin(stuId, stuPassword, request);
	            if (flag) {
	            	JSONObject jsonObject = JSONObject.fromObject(students.getStoken(stuId, request));
		            response.getWriter().print(jsonObject.toString());
	            } 
	            else {
	            	response.getWriter().write("400");
	            }
	        } catch (SQLException e) {
	            e.printStackTrace();
	        }
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
}
3)jsp页面:此处用postman(模拟前端,测试后台,强烈安利)进行测试,测试前先运行Tomcat(参考动态JavaWeb项目——hello world(二))

JavaWeb开发——Mybatis+Tomcat(三)_第1张图片

你可能感兴趣的:(mybatis,servlet,tomcat)