一个十分简单的MyBatis与struts2结合实现的登录与注册

在这次示例中,并没有加入spring,主要是我懒,,但是结合spring也十分的容易,MyBatis框架是一个非常灵活的java持久化框架,相对于hibernate来说,更加的灵活,并且个人觉得MyBatis比hibernate学习起来更加容易,但是无论是hibernate还是MyBatis都有自己的优点与缺点。
本次示例的测试环境
idea + maven + mysql

一,在idea中创建maven的webapp模板项目如图

一个十分简单的MyBatis与struts2结合实现的登录与注册_第1张图片

中途需要填写Groupld与Artifactld,创建好的项目结构如下图,如果创建好的项目木有途中某些目录,自行创建就好

一个十分简单的MyBatis与struts2结合实现的登录与注册_第2张图片

二,想pom.xml文件中添加依赖

    
      mysql
      mysql-connector-java
      6.0.6
    
    
      org.apache.struts
      struts2-core
      2.1.8
    
    
      org.mybatis
      mybatis
      3.4.4
    

完整的依赖图

一个十分简单的MyBatis与struts2结合实现的登录与注册_第3张图片

三,创建User类以及对应的映射文件

User.java, 就是一个十分普通的javabean,里面没有特定的api

package per.summer.login.domain;

/**
 * @Author: summer
 * @Mail: [email protected]
 * @Date: 2017 17-4-26 下午7:25
 * Project: struts-mybatis-login
 * Package: per.summer.login.domain
 * Desc: User实体类
 */
public class User {
    private int id;
    private String username;
    private String password;

    public User() {
    }

    public User(int id, String username, String ppassword) {
        this.id = id;
        this.username = username;
        this.password = ppassword;
    }

    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 getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

userMapper.xml文件,
resources文件中的mapper文件夹需要自己创建




    
    

    
    INSERT INTO user_inf VALUES(#{id}, #{username}, #{password})
    

四,创建MyBatis的相关配置文件及SqlSessionFactoryUtil类

jdbc.properties,在这里,我将连接数据库必要的信息单独拿出来了,更加方便吧

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybatis
username=root
password=manager

mybatis-config.xml




    
    
    
    
    
    
    
    
        
        
            
            
            
            
        
    
    
    
    
    

SqlSessionFactoryUtil.java,这里使用了单例模式来创建SqlSessionFactory,并且有SqlSessionFactory创建SqlSession实例, 毕竟,我们只需要SqlSessionFactory被创建一次

    package per.summer.login.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.Reader;

    /**
     * @Author: summer
     * @Mail: [email protected]
     * @Date: 2017 17-4-26 下午7:50
     * Project: struts-mybatis-login
     * Package: per.summer.login.util
     * Desc: SqlSessionFactory工具类
     */
    public class SqlSessionFactoryUtil {
        private static SqlSessionFactory sqlSessionFactory = null;

        private static final Class CLASS_LOCK = SqlSessionFactoryUtil.class;

        /**
         * 私有化构造器,实现单例模式
         */
        private SqlSessionFactoryUtil() {}

        /**
         * @Author: summer
         * @Date: 17-4-26 下午7:54
         * @Param: null
         * @Return: sqlSessionFactory
         * @Desc: 返回SqlSessionFactory实例
         */
        private static SqlSessionFactory initSqlSessionFactory() {
            Reader reader = null;

            try {
                reader = Resources.getResourceAsReader("mybatis-config.xml");
            } catch (IOException e) {
                e.printStackTrace();
            }

            synchronized (CLASS_LOCK) {
                if (sqlSessionFactory == null) {
                    sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
                }
            }
            return sqlSessionFactory;
        }

        /**
         * @Author: summer
         * @Date: 17-4-26 下午7:55
         * @Param: null
         * @Return: sqlSession
         * @Desc: 创建SqlSession实例并返回
         */
        public static SqlSession openSqlSession() {
            if (sqlSessionFactory == null) {
                initSqlSessionFactory();
            }

            return sqlSessionFactory.openSession();
        }
    }

MyBatis的东西,基本完了。下面是struts2的

Action

LoginAction.java

package per.summer.login.action;

import com.opensymphony.xwork2.ActionSupport;
import org.apache.ibatis.session.SqlSession;
import per.summer.login.domain.User;
import per.summer.login.mapper.UserMapper;
import per.summer.login.util.SqlSessionFactoryUtil;

import java.util.Set;

/**
 * @Author: summer
 * @Mail: [email protected]
 * @Date: 2017 17-4-26 下午7:48
 * Project: struts-mybatis-login
 * Package: per.summer.login.action
 * Desc:
 */
public class LoginAction extends ActionSupport{
    private String username;
    private String password;

    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 execute() throws Exception {
        Set users = null;
        SqlSession sqlSession = null;
        try {
            sqlSession = SqlSessionFactoryUtil.openSqlSession();
            UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
            users = userMapper.getUser(username, password);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (sqlSession != null) {
                sqlSession.close();
            }
        }
        if (users.size() == 0) {
            return ERROR;
        } else {
            return SUCCESS;
        }
    }
}

RegisterAction.java,这个注册,我没有考虑什么账号已经存在。不能在注册之类的问题,毕竟,只打算做一个最简单的,想实现这些的,自己去试试,不难

package per.summer.login.action;

import com.opensymphony.xwork2.ActionSupport;
import org.apache.ibatis.session.SqlSession;
import per.summer.login.domain.User;
import per.summer.login.mapper.UserMapper;
import per.summer.login.util.SqlSessionFactoryUtil;

/**
 * @Author: summer
 * @Mail: [email protected]
 * @Date: 2017 17-4-26 下午8:19
 * Project: struts-mybatis-login
 * Package: per.summer.login.action
 * Desc:
 */
public class RegisterAction extends ActionSupport {
    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 execute() throws Exception {
        SqlSession sqlSession = null;
    
        try {
            sqlSession = SqlSessionFactoryUtil.openSqlSession();
            User user = new User(1, username, password);
            UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
            userMapper.addUser(user);
            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
            sqlSession.rollback();
        } finally {
            sqlSession.close();
        }
        return SUCCESS;
    }
}

struts.xml






    
    
        WEB-INF/jsp/welcome.jsp
        WEB-INF/jsp/error.jsp
    

    
        WEB-INF/jsp/login.jsp
    

    
        WEB-INF/jsp/success.jsp
    

    
        WEB-INF/jsp/register.jsp
    
    

哦!最后还有web.xml



    
    struts2
    org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
    
    
    struts2
    /*
    

至于相关的jsp页面,我就是给出login.jsp吧!

<%@ taglib prefix="s" uri="/struts-tags" %>
<%--
  Created by IntelliJ IDEA.
  User: summer
  Date: 17-2-20
  Time: 下午12:59
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    Login Page



    
    
    


测试结果

一个十分简单的MyBatis与struts2结合实现的登录与注册_第4张图片

关于想学习MyBatis的朋友们,光是这个简单的示例是肯定不行的,还需要去好好的看资料与书籍。至于源码,在这里 https://drive.google.com/file/d/0B_SeU2BJWlSJWHlPb0l3SFdfOWc/view?usp=sharing
有什么不懂的,可以留言,也可以邮箱,欢迎一起进步

你可能感兴趣的:(一个十分简单的MyBatis与struts2结合实现的登录与注册)