mbatis 用户登录,注册

package com.neu.service;

import java.util.List;

import com.neu.entity.Power;
import com.neu.entity.Userinfo;

public interface LoginService {
	Userinfo getLogin(String username,String password) throws Exception;
	List<String> getRolname() throws Exception;
	Power getById(String rolname) throws Exception;
	int getInsert(Userinfo userinfo) throws Exception;
}

package com.neu.service;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

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

import com.neu.entity.Power;
import com.neu.entity.PowerExample;
import com.neu.entity.Userinfo;
import com.neu.entity.UserinfoExample;
import com.neu.mapper.PowerMapper;
import com.neu.mapper.UserinfoMapper;

public class LoginServiceImpl implements LoginService {
	private UserinfoMapper userinfoMapper;
	private SqlSession session;
	private PowerMapper powerMapper;
	
	public void before() throws IOException {
		//读取配置文件,连接数据库
		InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");
		//创建一个SqlSessionFactory
		SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
		//得到与数据库进行连接的会话SqlSession
		session = factory.openSession();
		
		//得到DeptMapper接口的实现类
		userinfoMapper = session.getMapper(UserinfoMapper.class);
		powerMapper = session.getMapper(PowerMapper.class);
	}
	
	
	public void after() {
		//关闭
		session.close();
	}
	@Override
	public Userinfo getLogin(String username, String password) throws Exception {
		before();
		UserinfoExample example = new UserinfoExample();
		example.or().andUsernameEqualTo(username).andPasswordEqualTo(password);
		List<Userinfo> list = userinfoMapper.selectByExample(example);
		after();
		if(list.size()==0) {
			return null;
		}
		return list.get(0);
	}


	@Override
	public List<String> getRolname() throws IOException {
		before();
		PowerExample example = new PowerExample();

		List<Power> list = powerMapper.selectByExample(example);
		List<String> list2 = new ArrayList<>();
		after();
		for(Power power:list) {
			String rolname = power.getRolname();
			list2.add(rolname);
		}
		
		return list2;
	}


	@Override
	public Power getById(String rolname) throws Exception {
		before();
		PowerExample example = new PowerExample();
		example.or().andRolnameEqualTo(rolname);
		List<Power> list = powerMapper.selectByExample(example);
		after();
		
		return list.get(0);
	}


	@Override
	public int getInsert(Userinfo userinfo) throws Exception {
		before();
		UserinfoExample example = new UserinfoExample();
		int n = userinfoMapper.insertSelective(userinfo);
		session.commit();
		after();
		return n;
	}

}

package com.neu.servlet;

import java.io.IOException;
import java.util.List;

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

import com.neu.entity.Power;
import com.neu.entity.Userinfo;
import com.neu.service.LoginService;
import com.neu.service.LoginServiceImpl;

/**
 * Servlet implementation class LoginServlet
 */
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    private LoginService login = new LoginServiceImpl();
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String method = request.getParameter("method");
		if("login".equals(method)) {
			doLogin(request,response);
		}else if("loglist".equals(method)) {
			doLoglist(request,response);
		}else if("logliston".equals(method)) {
			doLogon(request,response);
		}
	}
	private void doLogon(HttpServletRequest request, HttpServletResponse response) {
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String email = request.getParameter("email");
		String rolname = request.getParameter("rolname");
		Power power=null;
		try {
			power = login.getById(rolname);
		} catch (Exception e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
		Userinfo userinfo = new Userinfo();
		userinfo.setUsername(username);
		userinfo.setPassword(password);
		userinfo.setEmail(email);
		userinfo.setPower(power);
		int n= 0;
		try {
			n = login.getInsert(userinfo);
		} catch (Exception e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
		if(n!=0) {
			doLogin(request,response);
		}else {
			doLoglist(request,response);
		}
		
	}
	private void doLoglist(HttpServletRequest request, HttpServletResponse response) {
		try {
			List<String> list = login.getRolname();
			request.setAttribute("list", list);
			request.getRequestDispatcher("/WEB-INF/loglist.jsp").forward(request, response);
		} catch (Exception e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
		
	}
	private void doLogin(HttpServletRequest request, HttpServletResponse response) {
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		
		try {
			Userinfo userinfo = login.getLogin(username, password);
			
			if(userinfo !=null) {
				request.setAttribute("userinfo", userinfo);
				request.getRequestDispatcher("/WEB-INF/login.jsp").forward(request, response);
			}else {
				request.getRequestDispatcher("/index.jsp").forward(request, response);
			}
			
		} catch (Exception e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
		
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="${pageContext.request.contextPath }/LoginServlet?method=login" method="post">
	用户名:<input type="text" name="username">
	密码:<input type="password" name="password">
	<input type="submit" value="登录">
	</form>
	<a href="${pageContext.request.contextPath }/LoginServlet?method=loglist">注册</a>
</body>
</html>

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	用户名:${ userinfo.username } 权限:${userinfo.power.rolname }
</body>
</html>

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>    
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form action="${pageContext.request.contextPath }/LoginServlet?method=logliston" method="post">
		<table>
			<tr>
				<td>用户名:</td><td><input type="text" name="username"></td>
			</tr>
			<tr>
				<td>密码:</td><td><input type="password" name="password"></td>
			</tr>
			<tr>
				<td>email:</td><td><input type="email" name="email"></td>
			</tr>
			<tr>
				<td>权限:</td>
				<td>
					<select name="rolname">
						<c:forEach items="${list }" var="rolname">
							<option value="${ rolname }">${ rolname }</option>
						</c:forEach>
					</select>
				</td>
			</tr>
			<tr align="center"><td colspan="2"><input type="submit" value="注册"> </td> </tr>
		</table>
	</form>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值