• MyBatis(登录)


    超市账单管理系统登录

    目录:

     

     大配置文件

    <?xml version="1.0" encoding="UTF-8"?>
    <!-- xml文件的头文件,起到对文件的约束作用(例如:必须存在哪些节点) -->
    <!DOCTYPE configuration
            PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-config.dtd">
    <configuration><!--核心配置-->
        <!--引入jdbc配置,数据库核心的四要素-->
        <properties resource="jdbc.properties"/>
        <!--集成log4j配置-->
        <settings>
            <setting name="logImpl" value="LOG4J"/>
        </settings>
        <!--配置别名-->
        <typeAliases>
            <!--方式一:单个别名-->
            <!--<typeAlias type="com.wdksoft.entity.user" alias="user"></typeAlias>-->
            <!--方式二:全局别名-->
            <package name="com.wdksuper.entity"></package><!--这个包下的所有实体类的别名都是简写的类名-->
            <!--com.wdksoft.entity.user 别名为 user-->
        </typeAliases>
        <environments default="development">
            <environment id="development">
                <!--设置事务管理 采用JDBC事务 后续会将事务管理转移到Spring-->
                <transactionManager type="JDBC" />
                <!-- 配置数据库连接信息 MyBatis自带的数据源 其它取值为UNPOOLED和JNDI -->
                <dataSource type="POOLED">
                    <property name="driver" value="${jdbc.driver}" />
                    <property name="url" value="${jdbc.url}" />
                    <property name="username" value="${jdbc.username}" />
                    <property name="password" value="${jdbc.password}" />
                </dataSource>
            </environment>
        </environments>
        <mappers><!--管理我们配置的映射文件-->
            <mapper resource="com/wdksuper/dao/smbms_userMapper.xml"></mapper>
        </mappers>
    </configuration>

    jdbc.properties

    jdbc.driver=com.mysql.jdbc.Driver
    jdbc.url=jdbc:mysql://localhost:3306/smbms?useUniCode=true&characterEncoding=utf-8
    jdbc.username=root
    jdbc.password=123

    log4j.properties

    ### direct log messages to stdout 记录日志信息到控制台###
    log4j.appender.stdout=org.apache.log4j.ConsoleAppender
    log4j.appender.stdout.Target=System.out
    log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
    log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
    
    ### direct messages to file log.txt 记录日志信息到硬盘上的文件中 ###
    log4j.appender.file=org.apache.log4j.FileAppender
    log4j.appender.file.File=d:/log.txt
    log4j.appender.file.layout=org.apache.log4j.PatternLayout
    log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
    ### set log levels - for more verbose logging change 'info' to 'debug' ###
    log4j.rootLogger=debug, stdout,file

    DAO

    smbms_userMapper.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">
    <!--namespace需要指向接口全路径-->
    <mapper namespace="com.wdksuper.dao.userDao">
        <!--id代表当前命名空间下(接口下)的唯一方法名   resultType代表返回值类型-->
        <select id="selectpwd" resultType="smbms_user">
           select * from smbms_user where userCode=#{userCode}
        </select>
    </mapper>

    userDao

    package com.wdksuper.dao;
    
    import com.wdksuper.entity.smbms_user;
    
    public interface userDao {
        //登录(根据用户名查密码)
        public smbms_user selectpwd(String userCode);
    }

    entity

    smbms_user

    package com.wdksuper.entity;
    
    public class smbms_user {
        private Integer id;
        private String userCode;
        private String userName;
        private String userPassword;
        private Integer gender;
    
        public Integer getId() {
            return id;
        }
    
        public void setId(Integer id) {
            this.id = id;
        }
    
        public String getUserCode() {
            return userCode;
        }
    
        public void setUserCode(String userCode) {
            this.userCode = userCode;
        }
    
        public String getUserName() {
            return userName;
        }
    
        public void setUserName(String userName) {
            this.userName = userName;
        }
    
        public String getUserPassword() {
            return userPassword;
        }
    
        public void setUserPassword(String userPassword) {
            this.userPassword = userPassword;
        }
    
        public Integer getGender() {
            return gender;
        }
    
        public void setGender(Integer gender) {
            this.gender = gender;
        }
    }

    Service

    userservice

    package com.wdksuper.service;
    
    import com.wdksuper.entity.smbms_user;
    
    public interface userservice {
        //登录(根据用户名查密码)
        public smbms_user selectpwd(String userCode,String userPassword);
    }

    impl

    import com.wdksuper.dao.userDao;
    import com.wdksuper.entity.smbms_user;
    import com.wdksuper.service.userservice;
    import com.wdksuper.util.MyBatisUtil;
    import org.apache.ibatis.session.SqlSession;
    
    public class userserviceimpl implements userservice {
    
        SqlSession session= MyBatisUtil.getSession();
    
        @Override
        public smbms_user selectpwd(String userCode, String userPassword) {
            smbms_user selectpwd = session.getMapper(userDao.class).selectpwd(userCode);
            if(selectpwd!=null&&selectpwd.getUserPassword().equals(userPassword)){
                return selectpwd;
            }else{
                return null;
            }
        }
    }

    Servlet

    userServlet

    package com.wdksuper.servlet;
    
    import com.wdksuper.entity.smbms_user;
    import com.wdksuper.service.impl.userserviceimpl;
    import com.wdksuper.service.userservice;
    
    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("/userServlet")
    public class userServlet extends HttpServlet {
    
        userservice userss=new userserviceimpl();
    
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            String name=request.getParameter("username");
            String pwd=request.getParameter("password");
            smbms_user selectpwd = userss.selectpwd(name, pwd);
            if(selectpwd!=null){
                request.getRequestDispatcher("welcome.jsp").forward(request,response);
            }else{
                response.sendRedirect("login.jsp");
            }
        }
    
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            doPost(request,response);
        }
    }

    util

    MyBatisUtil

    package com.wdksuper.util;
    
            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;
    
    /**
     * 获取SqlSession对象的
     */
    public class MyBatisUtil {
        //01.创建需要单例的静态对象
        private  static SqlSessionFactory sessionFactory;
    
        //02.私有化构造
        private MyBatisUtil(){}
    
        //03.提供对外访问的接口
        public static synchronized SqlSession getSession() {
            try {
                //把xml文件读取到内存中 并且返回一个输入流对象
                InputStream stream = Resources.getResourceAsStream("mybatis-config.xml");
                if (sessionFactory == null) {
                    //创建SqlSessionFactory
                    sessionFactory = new SqlSessionFactoryBuilder().build(stream);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            return sessionFactory.openSession();
        }
    
    }

     Login.jsp

    <%--
    Created by IntelliJ IDEA.
    User: PC
    Date: 2019/9/28
    Time: 12:22
    To change this template use File | Settings | File Templates.
    --%>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <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="userServlet" method="get" >
                    <div class="inputbox">
                        <label for="user">用户名:</label>
                        <input id="user" type="text" name="username" 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>
  • 相关阅读:
    汇编学习笔记(一)
    外部中断的资料
    喇叭的落幕
    红外模块
    SQL2005连接不上解决
    DataGrid中动态添加列,使用CheckBox选择行
    List和ObservableCollection的相互转化
    使用C#发送邮件
    C#委托与事件 简明
    Linq GroupBy 求和
  • 原文地址:https://www.cnblogs.com/whtt/p/11604435.html
Copyright © 2020-2023  润新知