<?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"> <mappernamespace="controller"> <selectid="listUser"resultType="User"> select * from user </select> <selectid="selectUser"resultType="User"> select * from user where username=#{username} and password=#{password} </select> <insertid="addUser"parameterType="User" > insert into user ( username ) values (#{username}) </insert> <deleteid="deleteUser"parameterType="User" > delete from user where id= #{id} </delete> <selectid="getUser"parameterType="String"resultType="User"> select * from user where username = #{username} </select> <selectid="getPassword"parameterType="String"resultType="User"> select password from user where username = #{username} </select> <updateid="updateUser"parameterType="User" > update User set name=#{name} where id=#{id} </update> </mapper>
package controller; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; publicclassLoginServletextendsHttpServlet { /** * Constructor of the object. */ publicLoginServlet() { super(); } /** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ publicvoiddoGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Stringusername= request.getParameter("username").trim(); Stringpassword= request.getParameter("password").trim(); Stringresource="mybatis-config.xml"; InputStreaminputStream= Resources.getResourceAsStream(resource); SqlSessionFactorysqlSessionFactory=newSqlSessionFactoryBuilder() .build(inputStream); SqlSession session=sqlSessionFactory.openSession(); Map<String,Object> params = newHashMap<>(); params.put("username", username); params.put("password", password); List<User> us = session.selectList("getPassword",username); Useruser= us.get(0); if(user.getPassword().equals(password)){ request.getRequestDispatcher("/index.jsp").forward(request,response); } } /** * The doPost method of the servlet. <br> * * This method is called when a form has its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ publicvoiddoPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriterout= response.getWriter(); out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">"); out.println("<HTML>"); out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>"); out.println(" <BODY>"); out.print(" This is "); out.print(this.getClass()); out.println(", using the POST method"); out.println(" </BODY>"); out.println("</HTML>"); out.flush(); out.close(); } }