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;
}
}
package com.bjnm.mapper;
import com.bjnm.model.User;
public interface IUserMapper {
User login(String lname,String pass);
}
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;
}
}
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;
}
}
}
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("用户名或密码错误");
}
}
}
至此,整个后台代码完成,下面是部分前端代码: