IDEA中使用MyBatis进行登录
今天学习的是使用MyBatis来进行简单的登录,对于本博客内容表示看不懂的大家可以访问https://www.cnblogs.com/yjc1605961523/p/11599294.html
上面的地址也是我之前写的关于MyBatis比较基础的知识点
我也是刚开始学习MyBatis 能写的东西都写在博客里面了
项目结构大致如图
第一步先准备相应的工具类
1.1获取SqlSession核心对象的核心类
package com.yjc.util; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import java.io.InputStream; public class SqlSessionUtils { private static SqlSessionFactory factory; private SqlSessionUtils(){} //生产SqlSession public static synchronized SqlSession getSession(){ //1:获取核心配置文件的字节流 InputStream is=SqlSessionUtils.class.getResourceAsStream("/mybatis-config.xml"); //2:将字节流作为参数 构建Session工厂 factory=new SqlSessionFactoryBuilder().build(is); //3:调用工厂对象 生产session return factory.openSession(); } }
1.2用户实体类smbms_user(与数据库中表的字段名类型相同)
package com.yjc.entity; import java.util.Date; /** * smbms_user 实体类 * @author liangzz * @date2019-09-28 03:18 */ public class SmbmsUser{ /**主键ID**/ private Integer id; /**用户编码**/ private String userCode; /**用户名称**/ private String userName; /**用户密码**/ private String userPassword; /**性别(1:女、 2:男)**/ private Integer gender; /**出生日期**/ private Date birthday; /**手机**/ private String phone; /**地址**/ private String address; /**用户角色(取自角色表-角色id)**/ private Integer userRole; /**创建者(userId)**/ private Integer createdBy; /**创建时间**/ private Date creationDate; /**更新者(userId)**/ private Integer modifyBy; /**更新时间**/ private Date modifyDate; public void setId(Integer id){ this.id=id; } public Integer getId(){ return id; } public void setUserCode(String userCode){ this.userCode=userCode; } public String getUserCode(){ return userCode; } public void setUserName(String userName){ this.userName=userName; } public String getUserName(){ return userName; } public void setUserPassword(String userPassword){ this.userPassword=userPassword; } public String getUserPassword(){ return userPassword; } public void setGender(Integer gender){ this.gender=gender; } public Integer getGender(){ return gender; } public void setBirthday(Date birthday){ this.birthday=birthday; } public Date getBirthday(){ return birthday; } public void setPhone(String phone){ this.phone=phone; } public String getPhone(){ return phone; } public void setAddress(String address){ this.address=address; } public String getAddress(){ return address; } public void setUserRole(Integer userRole){ this.userRole=userRole; } public Integer getUserRole(){ return userRole; } public void setCreatedBy(Integer createdBy){ this.createdBy=createdBy; } public Integer getCreatedBy(){ return createdBy; } public void setCreationDate(Date creationDate){ this.creationDate=creationDate; } public Date getCreationDate(){ return creationDate; } public void setModifyBy(Integer modifyBy){ this.modifyBy=modifyBy; } public Integer getModifyBy(){ return modifyBy; } public void setModifyDate(Date modifyDate){ this.modifyDate=modifyDate; } public Date getModifyDate(){ return modifyDate; } }
1.3Dao层接口ILoginDao
package com.yjc.dao; import com.yjc.entity.SmbmsUser; import org.apache.ibatis.annotations.Select; public interface ILoginDao {
//此处使用注解方式进行查询 xml方式见https://www.cnblogs.com/yjc1605961523/p/11599294.html @Select("SELECT * FROM smbms_user where userCode=#{userCode} and userPassword=#{userPassword}") SmbmsUser loginCheck(SmbmsUser smbmsUser); //登录方法 }
和Dao层接口名称同名的XML配置文件
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.yjc.dao.ILoginDao"> //指向Dao层接口 由于使用的注解方式,xml里不做其他实现,xml其实也可以省略,修改核心配置文件,在此不多说了 </mapper>
1.4Service层接口
package com.yjc.service; import com.yjc.entity.SmbmsUser; public interface ILoginService { SmbmsUser loginCheck(SmbmsUser smbmsUser); //登录方法 }
1.5Service层实现类
package com.yjc.service.Impl; import com.yjc.dao.ILoginDao; import com.yjc.entity.SmbmsUser; import com.yjc.service.ILoginService; import com.yjc.util.SqlSessionUtils; import org.apache.ibatis.session.SqlSession; public class LoginServiceImpl implements ILoginService { SqlSession sqlSession= SqlSessionUtils.getSession(); //调用工具栏获取SqlSession对象 ILoginDao mapper = sqlSession.getMapper(ILoginDao.class); //映射Dao层接口 @Override public SmbmsUser loginCheck(SmbmsUser smbmsUser) { return mapper.loginCheck(smbmsUser); //调用Dao层想对应的方法进行实现 } }
1.6Servlet
package com.yjc.servlet; import com.yjc.entity.SmbmsUser; import com.yjc.service.ILoginService; import com.yjc.service.Impl.LoginServiceImpl; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @WebServlet("/LoginServlet") public class LoginServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request,response); } ILoginService service=new LoginServiceImpl(); protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String usercode = request.getParameter("usercode"); //获取表单提交的数据 String password = request.getParameter("password"); usercode= new String(usercode.getBytes("ISO-8859-1"),"UTF-8"); password= new String(password.getBytes("ISO-8859-1"),"UTF-8"); SmbmsUser smbmsUser=new SmbmsUser(); smbmsUser.setUserCode(usercode); smbmsUser.setUserPassword(password); SmbmsUser smbmsUser2 = service.loginCheck(smbmsUser); //调用service层的方法进行实现 if (smbmsUser2!=null){ request.setAttribute("smbmsUser",smbmsUser2); //返回值不为空代表登录成功 进行转发否则重定向登录页面进行重新登录 request.getRequestDispatcher("welcome.jsp").forward(request,response); }else{ response.sendRedirect("login.jsp"); } } }
1.7JSP页面
<%-- Created by IntelliJ IDEA. User: Administrator Date: 2019/9/28 Time: 13:34 To change this template use File | Settings | File Templates. --%> <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <html> <head lang="en"> <meta charset="UTF-8"> <title>系统登录 - 超市账单管理系统</title> <link rel="stylesheet" href="css/style.css"/> </head> <body class="login_bg"> <section class="loginBox"> <header class="loginHeader"> <h1>超市账单管理系统</h1> </header> <section class="loginCont"> <form class="loginForm" action="LoginServlet"> //表单提交地址为Servlet <div class="inputbox"> <label for="user">用户名:</label> <input id="user" type="text" name="usercode" placeholder="请输入用户名" required/> </div> <div class="inputbox"> <label for="mima">密码:</label> <input id="mima" type="password" name="password" placeholder="请输入密码" required/> </div> <div class="subBtn"> <input type="submit" value="登录" /> <input type="reset" value="重置"/> </div> </form> </section> </section> </body> </html>
JSP效果展示