Spring MVC+mybatis实现注册登录功能

网友投稿 254 2023-04-28


Spring MVC+mybatis实现注册登录功能

本文实例为大家分享了Spring MVC mybatis实现注册登录功能的具体代码,供大家参考,具体内容如下

前期准备:

如下图所示,准备好所需要的包

新建工程,导入所需要的包,在web.xml中配置好所需要的,如下

xmlns:xsi="http://w3.org/2001/XMLSchema-instance

http://springmodules.org/schema/cache/springmodules-cache.xsd

http://springmodules.org/schema/cache/springmodules-ehcache.xsd"

xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee

http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd

">

encoding

org.springframework.web.filter.CharacterEncodingFilter

encoding

UTF-8

encoding

*.action

spring

org.springframework.web.servlet.DispatcherServlet

1

contextConfigLocation

/WEB-INF/config/spring.xml

spring

*.action

index.jsp

xmlns:xsi="http://w3.org/2001/XMLSchema-instance

http://springmodules.org/schema/cache/springmodules-cache.xsd

http://springmodules.org/schema/cache/springmodules-ehcache.xsd"

xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee

http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd

">

encoding

org.springframework.web.filter.CharacterEncodingFilter

encoding

UTF-8

encoding

*.action

spring

org.springframework.web.servlet.DispatcherServlet

1

contextConfigLocation

/WEB-INF/config/spring.xml

spring

*.action

index.jsp

配置好如下文件spring.xml

xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:context="http://springframework.org/schema/context"

xmlns:aop="http://springframework.org/schema/aop" xmlns:util="http://springframework.org/schema/util"

xmlns:cache="http://springframework.org/schema/cache" xmlns:tx="http://springframework.org/schema/tx"

xmlns:mvc="http://springframework.org/schema/mvc"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans-3.2.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context-3.2.xbXXFtllyNjsd

http://springframework.org/schema/aop

http://springframework.org/schema/aop/spring-aop-3.2.xsd

http://springframework.org/schema/util

http://springframework.org/schema/util/spring-util-3.2.xsd

http://springframework.org/schema/cache

http://springframework.org/schema/cache/spring-cache-3.2.xsd

http://springframework.org/schema/tx

http://springframework.org/schema/tx/spring-tx-3.2.xsd

http://springframework.org/schema/mvc

http://springframework.org/schema/mvc/spring-mvc-3.2.xsd">

xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:context="http://springframework.org/schema/context"

xmlns:aop="http://springframework.org/schema/aop" xmlns:util="http://springframework.org/schema/util"

xmlns:cache="http://springframework.org/schema/cache" xmlns:tx="http://springframework.org/schema/tx"

xmlns:mvc="http://springframework.org/schema/mvc"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans-3.2.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context-3.2.xbXXFtllyNjsd

http://springframework.org/schema/aop

http://springframework.org/schema/aop/spring-aop-3.2.xsd

http://springframework.org/schema/util

http://springframework.org/schema/util/spring-util-3.2.xsd

http://springframework.org/schema/cache

http://springframework.org/schema/cache/spring-cache-3.2.xsd

http://springframework.org/schema/tx

http://springframework.org/schema/tx/spring-tx-3.2.xsd

http://springframework.org/schema/mvc

http://springframework.org/schema/mvc/spring-mvc-3.2.xsd">

配置好数据库信息

driver=com.mysql.jdbc.Driver

url=jdbc:mysql://localhost:3306/cheshangbao?useUnicode=true&characterEncoding=utf8&autoReconnect=true&useOldAliasMetadataBehavior=true

username=root

password=admin

另外,还有所需要的操作数据库的语句:

PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

insert into user_login (phone_mobile,login_password,register_time,user_code) values(#{phone_mobile},#{login_password},#{register_time},#{user_code})

select phone_mobile from user_login where phone_mobile=#{phone_mobile}

select phone_mobile,login_password from user_login where phone_mobile=#{phone_mobile} and login_password=#{login_password}

前端准备工作

我做的一个简单的注册登录的页面,在页面中对表单进行了第一层的判断,然后在后端又写了第二层验证。

登陆页面如下

手机号:

密 码:




以下是注册界面

手机号:

密码:

确认密码:

页面中对手机号进行了验证,而且能够与后台进行交互,判断数据库中是否已有此手机号,页面所需的js文件和css配置会在以下给出,

进入正题

数据库中包含用户手机号,用户密码,以及注册信息,和生成的唯一识别码

以下是用户的实体类

package com.register.vo;

import java.util.Date;

public class User {

private int id;

private String phone_mobile;//用户手机号

private String login_password;//用户登录密码

private Date register_time;//用户注册日期

private String user_code;//用户唯一识别ID

public User(int id, String phone_mobile, String login_password, Date register_time,

String user_code) {

super();

this.id = id;

this.phone_mobile = phone_mobile;

this.login_password = login_password;

this.register_time = register_time;

this.user_code = user_code;

}

public User(String phone_mobile, String login_password, Date register_time, String user_code) {

super();

this.phone_mobile = phone_mobile;

this.login_password = login_password;

this.register_time = register_time;

this.user_code = user_code;

}

public User() {

}

public int getId() {

return id;

}

//对用户数据进行封装

public void setId(int id) {

this.id = id;

}

public String getPhone_mobile() {

return phone_mobile;

}

public void setPhone_mobile(String phone_mobile) {

this.phone_mobile = phone_mobile;

}

public String getLogin_password() {

return login_password;

}

public void setLogin_password(String login_password) {

this.login_password = login_password;

}

public Date getRegister_time() {

return register_time;

}

public void setRegister_time(Date register_time) {

this.register_time = register_time;

}

public String getUser_code() {

return user_code;

}

public void setUser_code(String user_code) {

this.user_code = user_code;

}

}

下面这一步是比较重要的一步,Controller类, 如下所示

package com.register.controller;

import java.net.InetAddress;

import java.net.UnknownHostException;

import java.text.DateFormat;

import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.HashMap;

import java.util.Map;

import java.util.UUID;

import java.util.regex.Matcher;

import java.util.regex.Pattern;

import javax.script.ScriptEngine;

import javax.script.ScriptEngineManager;

import javax.script.ScriptException;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpSession;

import org.apache.ibatis.session.SqlSession;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.sprihttp://ngframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;

import com.register.service.UserService;

import com.register.vo.User;

//让控制器成为一个bean

@Controller

//这个控制器是接受user_reg页面传过来的参数去操作数据库

public class UserController {

@Autowired

private SqlSession sqlSession;

@Autowired

private UserService us;

@Autowired

private HttpServletRequest req;

@RequestMapping("/userReg.action")

//jsp页面通过userReg.action找到这个方法

public String userReg(User user){

Map map = new HashMap();

map.put("phone_mobile", user.getPhone_mobile());

map.put("login_password", user.getLogin_password());

//判断页面传回的数据要求

Pattern pattern = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[01236789]))\\d{8}$");

Matcher matcher = pattern.matcher(user.getPhone_mobile());

if(user.getPhone_mobile()==null || user.getLogin_password()==null || !matcher.matches()){

return "index.jsp";

}

//获取当前注册时间

Date date = new Date();

DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

map.put("register_time", df.format(date));

//生成唯一识别码

String s = UUID.randomUUID().toString();

String user_code = s.substring(0,8)+s.substring(9,13)+s.substring(14,18)+s.substring(19,23)+s.substring(24);

map.put("user_code", user_code);

//将数据添加到数据库中

int a = sqlSession.insert("com.register.dao.addUser",map);

req.setAttribute("phone_mobile", user.getPhone_mobile());

req.setAttribute("login_password", user.getLogin_password());

return "pages/register_success.jsp";

}

//处理用户名唯一性的判断

@RequestMapping("/userJudge.action")

@ResponseBody

public User userJudge(String phone_mobile) {

User u = sqlSession.selectOne("com.register.dao.judgeUser",phone_mobile);

System.out.println(u.getPhone_mobile());

return u;

}

//用户登录的判断

@RequestMapping("/userLogin.action")

public String userLogin(String phone_mobile,String login_password){

//对页面传回的值进行二次判断

Pattern pattern = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[01236789]))\\d{8}$");

Matcher matcher = pattern.matcher(phone_mobile);

if(phone_mobile==null || login_password==null || !matcher.matches()){

return "pages/user-login-no.jsp";

}

User u = us.userLogin(phone_mobile, login_password);

//查到用户了,执行登录成功的操作

if(u!=null){

req.getSession().setAttribute("u", u);

return "pages/user-login-ok.jsp";

}else{

return "pages/user-login-no.jsp";

}

}

//用户退出销毁session 跳转到登录页

@RequestMapping("/userExit.action")

public String userExit(HttpSession sessiohttp://n){

session.invalidate();

return "index.jsp";

}

}

UserService类:

package com.register.service;

import java.util.HashMap;

import java.util.Map;

import org.apache.ibatis.session.SqlSession;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import com.register.vo.User;

@Service

public class UserService {

@Autowired

private SqlSession sqlSession;

public User userLogin(String phone_mobile,String login_password){

Map map = new HashMap();

map.put("phone_mobile",phone_mobile);

map.put("login_password", login_password);

User u = sqlSession.selectOne("com.register.dao.userLogin",map);

return u;

}

}

注入测试类:

package com.register.util;

import org.springframework.stereotype.Controller;

@Controller

public class TestUtil {

public void a(){

System.out.println("注入成功!");

}

}

OK,到这里已经基本差不多了,但是这里边所给的代码并不完整,为了方便大家相互交流学习,这是源码以及数据库文件,

链接:注册登录功能,想要参考源码的可以自己下载。


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Underscore之Array_动力节点Java学院整理
下一篇:因Spring AOP导致@Autowired依赖注入失败的解决方法
相关文章

 发表评论

暂时没有评论,来抢沙发吧~