JavaWeb03 员工管理系统之登陆页面

JavaWeb03 员工管理系统之登陆页面

整个项目的结构如下:
JavaWeb03 员工管理系统之登陆页面_第1张图片

  • 首先在com.bjnm.util包内对mybatis的一些内容进行封装,获取SqlSession对象;
package com.bjnm.util;

import java.io.IOException;
import java.io.InputStream;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class MyBatisUtil {
	
	public static SqlSessionFactory factory;
	
	static {
		try {
			InputStream in=Resources.getResourceAsStream("mybatis-config.xml");
			factory=new SqlSessionFactoryBuilder().build(in);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public static SqlSession createSession() {
		return factory.openSession();
	}

}
  • 接着创建实体类
package com.bjnm.model;

public class User {
	private Integer t_id;
	private String loginname;
	private String username;
	private String pass;
	private Integer tel;
	private Integer sal;
	private Integer state;
	private Integer d_id;
	public Integer getT_id() {
		return t_id;
	}
	public void setT_id(Integer t_id) {
		this.t_id = t_id;
	}
	public String getLoginname() {
		return loginname;
	}
	public void setLoginname(String loginname) {
		this.loginname = loginname;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPass() {
		return pass;
	}
	public void setPass(String pass) {
		this.pass = pass;
	}
	public Integer getTel() {
		return tel;
	}
	public void setTel(Integer tel) {
		this.tel = tel;
	}
	public Integer getSal() {
		return sal;
	}
	public void setSal(Integer sal) {
		this.sal = sal;
	}
	public Integer getState() {
		return state;
	}
	public void setState(Integer state) {
		this.state = state;
	}
	public Integer getD_id() {
		return d_id;
	}
	public void setD_id(Integer d_id) {
		this.d_id = d_id;
	}
	

}

  • 然后在接口中添加登陆方法和mapper.xml文件的配置
package com.bjnm.mapper;

import com.bjnm.model.User;

public interface IUserMapper {
	
	User login(String lname,String pass);

}




	


  • UserDao实现登陆方法
package com.bjnm.dao;

import org.apache.ibatis.session.SqlSession;

import com.bjnm.mapper.IUserMapper;
import com.bjnm.model.User;
import com.bjnm.util.MyBatisUtil;

public class UserDao {
	
	public User login(String lname,String pass) {
		SqlSession session=MyBatisUtil.createSession();
		IUserMapper user=session.getMapper(IUserMapper.class);
		User u=user.login(lname, pass);
		session.commit();
		session.close();
		return u;
	}
	

}
  • UserService进行逻辑处理
package com.bjnm.service;

import com.bjnm.dao.UserDao;
import com.bjnm.model.User;

public class UserService {
	
	public User login(String lname,String pass) {
		UserDao dao=new UserDao();
		User u=dao.login(lname, pass);
		//返回值不为空证明 登陆成功 返回u
		if(u!=null){
			return u;
			//失败
		}else{
			return null;
		}
	}

}
  • UserServlet实现登陆
package com.bjnm.servlet;

import java.io.IOException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bjnm.model.User;
import com.bjnm.service.UserService;

public class UserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UserServlet() {
        super();
        // TODO Auto-generated constructor stub
    }
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//获取登陆的登录名和密码
		String lname=request.getParameter("loginname");
		String pass=request.getParameter("pass");
		//调用service中登陆
		UserService service=new UserService();
		User u=service.login(lname, pass);
		if(u!=null){
			System.out.println("欢迎 "+u.getUsername()+"登陆本系统");
			Integer c = Integer.parseInt(request.getServletContext().getAttribute("count").toString());
			c++;
			request.getServletContext().setAttribute("count", c);
		}else{
			System.out.println("用户名或密码错误");
		}
	}

}

至此,整个后台代码完成,下面是部分前端代码:

               
登录信息

后台通过获取用户输入的loginnamepass与数据库内容进行比对,如果一致则登陆成功,否则就登陆失败

你可能感兴趣的:(JavaWeb)

账号名:
密码: