所有坐标
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
</plugin>
</plugins>
</build>
<dependencies> <!-- mysql--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.34</version> </dependency> <!--导入servlet插件--> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.6</version> </dependency> <!--mybatis--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.5</version> </dependency>
</dependencies>
mybatis-config.xml文件
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <!--<properties resource="dbconfig.properties"/>--> <!--创建别名--> <typeAliases> <package name="com.avb.pojo"/> </typeAliases> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql:///db1?useSSL=false&useServerPrepStmts=true"/>
//这里修改成自己需要用的数据库 <property name="username" value="root"/> <property name="password" value="root"/> </dataSource> </environment> </environments> <mappers> <!--<mapper resource="com/avb/Mapper/UserMapper.xml"/>--> <!--mapper代理方式--> <package name="com.avb.Mapper"/> </mappers> </configuration>
在Mapper文件夹下创建接口文件在resource文件夹下创建映射文件(两者必须相同路径)
映射文件
<?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"> <!--namespace:名称空间。写接口给的全类名,相当于告诉MyBatis这个配置文件是实现哪个接口的。--> <mapper namespace="com.avb.Mapper.UserMapper"> <!--resultType=""指定查询数据封装结果的时候使用自定义封装规则--> <!--<!–type指定为哪个javaBean自定义封装规则,id是唯一标识–> <resultMap id="mycat" type="smq.javabean.Cat"> <!–id指定主键列的对应规则,column指定哪一列是主键列,property指定cat的哪个属性封装id这个列数据–> <id column="id" property="id"/> <!–普通列–> <result column="cName" property="name"/> <result column="cgender" property="gender"/> <result column="cAge" property="age"/> </resultMap>--> </mapper>
在接口文件中创建sql语句,由于这个sql语句比较简单可以直接用注解的方式创建。
package com.avb.Mapper; import com.avb.pojo.User; import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Select; public interface UserMapper { @Select("select * from user where username = #{username} and password = #{password}") User select(@Param("username") String username, @Param("password") String password); }
登录界面html文件
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <div id="loginDiv">
<form action="loginServlet" method="post" id="form">
<p>username:<input id="username" name="username" type="text"></p>
<p>password:<input id="password" name="password" type="password"></p>
<div id="subDiv">
<input type="submit" class="button" value="login up">
<input type="reset" class="button" value="reset"> <a href="regiseter.html">没有账号?点击注册</a>
</div>
</form>
</div>
</body>
</html>
servlet文件
package com.avb.web; import com.avb.Mapper.UserMapper; import com.avb.pojo.User; 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.*; 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 { private String message; public void init() { } @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { //1、接收用户名和密码 String username = request.getParameter("username"); String password = request.getParameter("password"); //2、调用MyBatis完成查询 //2.1获取SqlSessionFactory对象 String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); //2.2获取SqlSession对象 SqlSession sqlSession = sqlSessionFactory.openSession(); //2.3获取Mapper UserMapper userMapper = sqlSession.getMapper(UserMapper.class); //2.4调用方法 User user = userMapper.select(username,password); //2.5释放资源 sqlSession.close(); //获取字符输出流,并设置content type response.setContentType("text/html;charset=utf-8"); PrintWriter writer = response.getWriter(); //3.判断user释放为null if(user != null){ writer.write("登录成功"); } else{ writer.write("登录失败"); } } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException { this.doGet(request, response); } }