【Mybatis】mybatis登录实例
pom.xml
<dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>javax.servlet.jsp-api</artifactId> <version>2.3.1</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.2</version> </dependency> <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.47</version> </dependency>
登录的JSP页面:
<%-- Created by IntelliJ IDEA. User: Super Date: 2019/8/11 Time: 21:07 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>主页-登录页面</title> </head> <body> <form action="login" method="post"> 姓名:<input type="text" name="username" id="username"><br> 密码:<input type="password" name="userpass" id="userpass"><br> <input type="submit" value="登录"> </form> </body> </html>
web.xml中的servlet配置:
<servlet> <servlet-name>Login</servlet-name> <servlet-class>com.hj.servlet.LoginServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>Login</servlet-name> <url-pattern>/login</url-pattern> </servlet-mapping>
LoginServlet类:
package com.hj.servlet; import com.hj.bean.Users; import com.hj.dao.UserDao; 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(name = "LoginServlet") public class LoginServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Users user = new Users(); user.setUsername(request.getParameter("username")); user.setUserpass(request.getParameter("userpass")); UserDao userDao = new UserDao(); Users loginUser = userDao.loginCheck(user); if (loginUser != null) { request.getRequestDispatcher("success.jsp").forward(request, response); }else { request.getRequestDispatcher("fail.jsp").forward(request, response); } } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request,response); } }
User类:
package com.hj.bean; public class Users { private int id; private String username; private String userpass; private String nickname; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getUserpass() { return userpass; } public void setUserpass(String userpass) { this.userpass = userpass; } public String getNickname() { return nickname; } public void setNickname(String nickname) { this.nickname = nickname; } }
UserDao
package com.hj.dao; import com.hj.bean.Users; import com.hj.utils.SqlSessionFactoryUtils; import org.apache.ibatis.session.SqlSession; import java.util.List; public class UserDao { private SqlSession sqlSession; private List<Users> list; private Users users; private SqlSession getSession(){ sqlSession = SqlSessionFactoryUtils.getSqlSessionFactory().openSession(); return sqlSession; } public Users loginCheck(Users user){ try { System.out.println("登录查询中..."); this.users = getSession().selectOne("loginCheck", user); } catch (Exception e) { e.printStackTrace(); } finally { sqlSession.close(); } return this.users; } }
-------------------------工具类和Listener---------------------
工具类,用于获取SqlSession,启动和关闭SqlSessionFactory:
package com.hj.utils; 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 java.io.IOException; import java.io.InputStream; public class SqlSessionFactoryUtils { private static String RESOURCE = "mybatis.xml"; private static SqlSessionFactory sqlSessionFactory; private static ThreadLocal<SqlSession> threadLocal = new ThreadLocal<SqlSession>(); /** * 初始化时候新建工厂 */ public static void initSqlSessionFactory(){ try { InputStream is = Resources.getResourceAsStream(RESOURCE); sqlSessionFactory = new SqlSessionFactoryBuilder().build(is); } catch (IOException e) { e.printStackTrace(); } } /** * 获取SqlSessionFactory的方法 * @return */ public static SqlSessionFactory getSqlSessionFactory(){ return sqlSessionFactory; } /** * 关闭session */ public static void close(){ SqlSession session = threadLocal.get(); if (session != null) { threadLocal.set(null); } } }
Listener:
package com.hj.listener; import com.hj.utils.SqlSessionFactoryUtils; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import javax.servlet.annotation.WebListener; @WebListener public class InitSqlSessionListener implements ServletContextListener { public void contextInitialized(ServletContextEvent servletContextEvent) { System.out.println("容器加载中,初始化SqlSessionFactory..."); SqlSessionFactoryUtils.initSqlSessionFactory(); } public void contextDestroyed(ServletContextEvent servletContextEvent) { System.out.println("容器销毁中,关闭SqlSession对象..."); SqlSessionFactoryUtils.close(); } }