目录
7.mybatis-config.xml mtbatis连接数据库xml文件
11.web.xml web配置文件(这个在配置webapp项目的时候自动配置)
14.对Servlet SqlSessionFactory的代码优化
1.首先创建一个web的项目
2.配置maven环境,mybatis配置文件
3. 写好都相应的文件,就可以开始了

- package com.itheima.mapper;
-
- import com.itheima.pojo.User;
- import org.apache.ibatis.annotations.Insert;
- import org.apache.ibatis.annotations.Param;
- import org.apache.ibatis.annotations.Select;
-
- public interface UserMapper {
- // 查询用户信息
-
- /**
- * @Param("username")内的参数是与大括号里面的参数做一个映射
- *
- * @param username
- * @param password
- * @return
- */
- @Select("select * from tb_user where username = #{username} and password = #{password}")
- User select(@Param("username") String username,@Param("password")String password);
-
- //获取数据库相同信息
- @Select("select * from tb_user where username = #{username}")
- User selectByUsername(String username);
-
- //向数据库插入用户信息
- void insert(User user);
- }
- package com.itheima.pojo;
-
- public class User {
-
- private Integer id;
- private String username;
- private String password;
-
- public Integer getId() {
- return id;
- }
- public void setId(Integer id) {
- this.id = id;
- }
- public String getUsername() {
- return username;
- }
- public void setUsername(String username) {
- this.username = username;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- @Override
- public String toString() {
- return "User{" +
- "id=" + id +
- ", username='" + username + '\'' +
- ", password='" + password + '\'' +
- '}';
- }
- }
- package com.itheima.web;
-
- import com.itheima.mapper.UserMapper;
- import com.itheima.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 javax.servlet.*;
- import javax.servlet.http.*;
- import javax.servlet.annotation.*;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.PrintWriter;
-
- @WebServlet("/loginServlet")
- public class loginServlet extends HttpServlet {
- @Override
- protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- //1.接受用户名和密码
- String username = request.getParameter("username");
- String password = request.getParameter("password");
-
- //2.调用mybatis方法进行调用
- //2.1获取sqlSessionFactory对象
- String resource = "mybatis-config.xml";//现在在resource根目录下
- InputStream inputStream = Resources.getResourceAsStream(resource);
- SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
- //2.2获取sqlSession对象
- SqlSession sqlSession = sqlSessionFactory.openSession(true);//自动提交事务
- //2.3获取mapper对象
- UserMapper mapper = sqlSession.getMapper(UserMapper.class);
- //2.4调用mapper方法
- User select = mapper.select(username, password);
- //2.5释放资源
- sqlSession.close();
-
- //获取response响应数据的字符输出流,并设置相应的格式
- response.setContentType("text/html;charset=utf-8");
- PrintWriter writer = response.getWriter();
- //3.判断select是否为null
- if (select!=null){
- //登录成功
- writer.write("");
- }else{
- //登录失败
- writer.write("");
- }
- }
-
- @Override
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- this.doGet(request, response);
- }
- }
- package com.itheima.web;
-
- import com.itheima.mapper.UserMapper;
- import com.itheima.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 javax.servlet.*;
- import javax.servlet.http.*;
- import javax.servlet.annotation.*;
- import java.io.IOException;
- import java.io.InputStream;
-
-
- @WebServlet("/registerServlet")
- public class registerServlet extends HttpServlet {
- @Override
- protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- //1.接受用户名和密码
- String username = request.getParameter("username");
- String password = request.getParameter("password");
- //1.2封装参数 --利用实体类对参数进行封装,就简单快捷了许多
- User user = new User();
- user.setUsername(username);
- user.setPassword(password);
-
- //2.调用mybatis方法进行调用
- //2.1获取sqlSessionFactory对象
- String resource = "mybatis-config.xml";//现在在resource根目录下
- InputStream inputStream = Resources.getResourceAsStream(resource);
- SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
- //2.2获取sqlSession对象
- SqlSession sqlSession = sqlSessionFactory.openSession(true);//自动提交事务
- //2.3获取mapper对象
- UserMapper mapper = sqlSession.getMapper(UserMapper.class);
- //3.检测数据库内有么有相同的值
- //获取response响应数据的字符输出流,并设置相应的格式
- response.setContentType("text/html;charset=utf-8");
- User selectByUsername = mapper.selectByUsername(username);
- if (selectByUsername == null){
- //判断字符串是否为空,抢了前端的活
- if (username.length()!=0 && password .length()!=0){
- mapper.insert(user);
- //释放资源
- sqlSession.close();
- response.getWriter().write("");
- }else{
- response.getWriter().write("");
- }
- }else {
- response.getWriter().write("");
- }
-
-
- }
-
- @Override
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- this.doGet(request, response);
- }
- }
- mapper
- PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
- "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
- <mapper namespace="com.itheima.mapper.UserMapper">
-
-
-
- <insert id="insert" useGeneratedKeys="true" keyProperty="id">
- insert into tb_user(username,password)values (#{username},#{password});
- insert>
- mapper>
- configuration
- PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
- "http://mybatis.org/dtd/mybatis-3-config.dtd">
- <configuration>
-
- <typeAliases>
- <package name="com.itheima.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>
-
- <package name="com.itheima.mapper"/>
- mappers>
- configuration>
- * {
- margin: 0;
- padding: 0;
- }
-
- html {
- height: 100%;
- width: 100%;
- overflow: hidden;
- margin: 0;
- padding: 0;
- background: url(../imgs/Desert.jpg) no-repeat 0px 0px;
- background-repeat: no-repeat;
- background-size: 100% 100%;
- -moz-background-size: 100% 100%;
- }
-
- body {
- display: flex;
- align-items: center;
- justify-content: center;
- height: 100%;
- }
-
- #loginDiv {
- width: 37%;
- display: flex;
- justify-content: center;
- align-items: center;
- height: 300px;
- background-color: rgba(75, 81, 95, 0.3);
- box-shadow: 7px 7px 17px rgba(52, 56, 66, 0.5);
- border-radius: 5px;
- }
-
- #name_trip {
- margin-left: 50px;
- color: red;
- }
-
- p {
- margin-top: 30px;
- margin-left: 20px;
- color: azure;
- }
-
- input {
- margin-left: 15px;
- border-radius: 5px;
- border-style: hidden;
- height: 30px;
- width: 140px;
- background-color: rgba(216, 191, 216, 0.5);
- outline: none;
- color: #f0edf3;
- padding-left: 10px;
- }
- #username{
- width: 200px;
- }
- #password{
- width: 202px;
- }
- .button {
- border-color: cornsilk;
- background-color: rgba(100, 149, 237, .7);
- color: aliceblue;
- border-style: hidden;
- border-radius: 5px;
- width: 100px;
- height: 31px;
- font-size: 16px;
- }
-
- #subDiv {
- text-align: center;
- margin-top: 30px;
- }
- #loginMsg{
- text-align: center;color: aliceblue;
- }
- * {
- margin: 0;
- padding: 0;
- list-style-type: none;
- }
- .reg-content{
- padding: 30px;
- margin: 3px;
- }
- a, img {
- border: 0;
- }
-
- body {
- background-image: url("../imgs/reg_bg_min.jpg") ;
- text-align: center;
- }
-
- table {
- border-collapse: collapse;
- border-spacing: 0;
- }
-
- td, th {
- padding: 0;
- height: 90px;
-
- }
- .inputs{
- vertical-align: top;
- }
-
- .clear {
- clear: both;
- }
-
- .clear:before, .clear:after {
- content: "";
- display: table;
- }
-
- .clear:after {
- clear: both;
- }
-
- .form-div {
- background-color: rgba(255, 255, 255, 0.27);
- border-radius: 10px;
- border: 1px solid #aaa;
- width: 424px;
- margin-top: 150px;
- margin-left:1050px;
- padding: 30px 0 20px 0px;
- font-size: 16px;
- box-shadow: inset 0px 0px 10px rgba(255, 255, 255, 0.5), 0px 0px 15px rgba(75, 75, 75, 0.3);
- text-align: left;
- }
-
- .form-div input[type="text"], .form-div input[type="password"], .form-div input[type="email"] {
- width: 268px;
- margin: 10px;
- line-height: 20px;
- font-size: 16px;
- }
-
- .form-div input[type="checkbox"] {
- margin: 20px 0 20px 10px;
- }
-
- .form-div input[type="button"], .form-div input[type="submit"] {
- margin: 10px 20px 0 0;
- }
-
- .form-div table {
- margin: 0 auto;
- text-align: right;
- color: rgba(64, 64, 64, 1.00);
- }
-
- .form-div table img {
- vertical-align: middle;
- margin: 0 0 5px 0;
- }
-
- .footer {
- color: rgba(64, 64, 64, 1.00);
- font-size: 12px;
- margin-top: 30px;
- }
-
- .form-div .buttons {
- float: right;
- }
-
- input[type="text"], input[type="password"], input[type="email"] {
- border-radius: 8px;
- box-shadow: inset 0 2px 5px #eee;
- padding: 10px;
- border: 1px solid #D4D4D4;
- color: #333333;
- margin-top: 5px;
- }
-
- input[type="text"]:focus, input[type="password"]:focus, input[type="email"]:focus {
- border: 1px solid #50afeb;
- outline: none;
- }
-
- input[type="button"], input[type="submit"] {
- padding: 7px 15px;
- background-color: #3c6db0;
- text-align: center;
- border-radius: 5px;
- overflow: hidden;
- min-width: 80px;
- border: none;
- color: #FFF;
- box-shadow: 1px 1px 1px rgba(75, 75, 75, 0.3);
- }
-
- input[type="button"]:hover, input[type="submit"]:hover {
- background-color: #5a88c8;
- }
-
- input[type="button"]:active, input[type="submit"]:active {
- background-color: #5a88c8;
- }
- .err_msg{
- color: red;
- padding-right: 170px;
- }
- #password_err,#tel_err{
- padding-right: 195px;
- }
-
- #reg_btn{
- margin-right:50px; width: 285px; height: 45px; margin-top:20px;
- }
(desert.jpg)

(reg_bg_min.jpg)

- <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
- version="4.0">
- web-app>
- html>
- <html lang="en">
-
- <head>
- <meta charset="UTF-8">
- <title>logintitle>
- <link href="css/login.css" rel="stylesheet">
- head>
-
- <body>
- <div id="loginDiv">
- <form action="/maven-mubatis-servlet-final/loginServlet" id="form" method="post">
- <h1 id="loginMsg">登录案例h1>
- <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="register.html">没有账号?点击注册a>
- div>
- form>
- div>
-
- body>
- html>
- html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>欢迎注册title>
- <link href="css/register.css" rel="stylesheet">
- head>
- <body>
-
- <div class="form-div">
- <div class="reg-content">
- <h1>欢迎注册h1>
- <span>已有帐号?span> <a href="login.html">登录a>
- div>
- <form id="reg-form" action="/maven-mubatis-servlet-final/registerServlet" method="post">
-
- <table>
-
- <tr>
- <td>用户名td>
- <td class="inputs">
- <input name="username" type="text" id="username">
- <br>
- <span id="username_err" class="err_msg" style="display: none">用户名不太受欢迎span>
- td>
-
- tr>
-
- <tr>
- <td>密码td>
- <td class="inputs">
- <input name="password" type="password" id="password">
- <br>
- <span id="password_err" class="err_msg" style="display: none">密码格式有误span>
- td>
- tr>
-
-
- table>
-
- <div class="buttons">
- <input value="注 册" type="submit" id="reg_btn">
- div>
- <br class="clear">
- form>
-
- div>
- body>
- html>
- <project xmlns="http://maven.apache.org/POM/4.0.0"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0modelVersion>
-
- <groupId>org.examplegroupId>
- <artifactId>maven-mubatis-servlet-finalartifactId>
- <version>1.0-SNAPSHOTversion>
- <packaging>warpackaging>
-
-
-
-
- <properties>
- <maven.compiler.source>18maven.compiler.source>
- <maven.compiler.target>18maven.compiler.target>
- properties>
- <build>
- <plugins>
-
- <plugin>
- <groupId>org.apache.tomcat.mavengroupId>
- <artifactId>tomcat7-maven-pluginartifactId>
- <version>2.2version>
- plugin>
- <plugin>
- <groupId>org.apache.maven.pluginsgroupId>
- <artifactId>maven-compiler-pluginartifactId>
- <configuration>
- <source>17source>
- <target>17target>
- configuration>
- plugin>
- plugins>
- build>
-
- <dependencies>
- <dependency>
- <groupId>javax.servletgroupId>
- <artifactId>javax.servlet-apiartifactId>
- <version>3.1.0version>
- <scope>providedscope>
- dependency>
-
- <dependency>
- <groupId>commons-iogroupId>
- <artifactId>commons-ioartifactId>
- <version>2.6version>
- dependency>
-
- <dependency>
- <groupId>org.mybatisgroupId>
- <artifactId>mybatisartifactId>
- <version>3.5.5version>
- dependency>
-
- <dependency>
- <groupId>mysqlgroupId>
- <artifactId>mysql-connector-javaartifactId>
- <version>8.0.29version>
- dependency>
-
- <dependency>
- <groupId>junitgroupId>
- <artifactId>junitartifactId>
- <version>4.13.2version>
- <scope>Testscope>
- dependency>
-
- <dependency>
- <groupId>org.slf4jgroupId>
- <artifactId>slf4j-apiartifactId>
- <version>1.7.36version>
- dependency>
-
- <dependency>
- <groupId>ch.qos.logbackgroupId>
- <artifactId>logback-classicartifactId>
- <version>1.2.3version>
- dependency>
-
- <dependency>
- <groupId>ch.qos.logbackgroupId>
- <artifactId>logback-coreartifactId>
- <version>1.2.3version>
- dependency>
- dependencies>
- project>
- -- 创建用户表
- CREATE TABLE tb_user(
- id int primary key auto_increment,
- username varchar(20) unique,
- password varchar(32)
- );
-
- -- 添加数据
- INSERT INTO tb_user(username,password) values('zhangsan','123'),('lisi','234');
-
- SELECT * FROM tb_user;
-
-