SpringMVC+Spring+Mybatis实现登录注册Demo「建议收藏」

SpringMVC+Spring+Mybatis实现登录注册Demo「建议收藏」使用环境:MyEclipse/Eclipse+Tomcat+MySql。使用技术:SpringMVC+Spring+Mybatis。实现效果登录页面:密码错误提示登录成功后注册注册成功目录结构一、配置文件beans.xml<?xmlversion=”1.0″encoding=”UTF-8″?><beansxmlns=”http://www.springframework.org/schema/beans”xmlns:x.

大家好,又见面了,我是你们的朋友全栈君。

使用环境:MyEclipse/Eclipse + Tomcat + MySql。

使用技术:SpringMVC + Spring + Mybatis。

实现效果

登录页面:
在这里插入图片描述
密码错误提示
在这里插入图片描述
登录成功后
在这里插入图片描述
注册
在这里插入图片描述
注册成功
在这里插入图片描述

目录结构

在这里插入图片描述

一、配置文件

beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 自动扫描 -->
<context:component-scan base-package="com.java.dao"></context:component-scan>  
<context:component-scan base-package="com.java.service"></context:component-scan>  
<context:component-scan base-package="com.java.serviceimpl"></context:component-scan>
<!-- 配置mysql连接 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<context:property-placeholder location="classpath:/jdbc.properties"/>
<!-- 配置mybatis的SQLSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!-- 自动扫描mappers.xml文件 -->
<property name="mapperLocations" value="classpath:com/java/mappers/*.xml"></property>
<!-- mybatis配置文件 -->
<property name="configLocation" value="classpath:mybatis-config.xml "></property>
</bean>
<!-- spring自动查找其下的类 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.java.dao"></property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>  
</beans>

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>
<!-- 对entity下的实体类起个别名,起到简化的作用 -->
<typeAliases>
<package name="com.java.entity"></package>
</typeAliases>
</configuration>

springmvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 使用注解的包,包括子集 --> 
<context:component-scan base-package="com.java.controller"/>
<!-- 视图解析器 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>

jdbc.properties

jdbc.driverClassName=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/db_student?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=UTC
jdbc.username=root
jdbc.password=root

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance http://www.springmodules.org/schema/cache/springmodules-cache.xsd http://www.springmodules.org/schema/cache/springmodules-ehcache.xsd" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>mybatis06-spring-springmvc</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- 对spring的支持 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:beans.xml</param-value>
</context-param>
<!-- 过滤器 -->
<filter> 
<filter-name>characterEncodingFilter</filter-name> 
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 
<init-param> 
<param-name>encoding</param-name> 
<param-value>UTF-8</param-value> 
</init-param> 
<init-param> 
<param-name>forceEncoding</param-name> 
<param-value>true</param-value> 
</init-param> 
</filter> 
<filter-mapping> 
<filter-name>characterEncodingFilter</filter-name> 
<url-pattern>/*</url-pattern> 
</filter-mapping> 
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 对springmvc的支持 -->
<servlet>
<servlet-name >springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>

二、Java代码

1、com.java.entity

User.java

package com.java.entity;
public class User { 

private Integer id;
private String userName;
private String passWord;
// 无参构造
public User() { 

super();
}
// 全参构造
public User(String userName, String passWord) { 

super();
this.userName = userName;
this.passWord = passWord;
}
//get、set方法
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;
}
// toString方法
@Override
public String toString() { 

return "User [id=" + id + ", userName=" + userName + ", passWord=" + passWord + "]";
}
}

2、com.java.dao

UserDao.java

package com.java.dao;
import com.java.entity.User;
public interface UserDao { 

public User login(User user);
public User add(User user);
}

3、com.java.mappers

user.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">
<mapper namespace="com.java.dao.UserDao">
<resultMap type="com.java.entity.User" id="UserResult">
<id property="id" column="id"></id>
<result property="userName" column="userName"></result>
<result property="passWord" column="passWord"></result>
</resultMap>
<select id="login" parameterType="com.java.entity.User" resultMap="UserResult">
select * from user where userName=#{userName} and passWord=#{passWord}
</select>
<select id="add" parameterType="com.java.entity.User" resultMap="UserResult">
insert into user (userName,passWord) values(#{userName},#{passWord})
</select>
</mapper>  

4、com.java.controller

UserController.java

package com.java.controller;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.java.entity.User;
import com.java.service.UserService;
@Controller
@RequestMapping("/user")
public class UserController { 

@Resource
private UserService userService;
@RequestMapping("/login")
public String login(User user,HttpServletRequest request) { 

User resultUser=userService.login(user);
System.out.println(resultUser);
if(resultUser==null) { 

request.setAttribute("user", user);
request.setAttribute("errorMsg", "用户名或密码错误!");
return "index";
}else { 

HttpSession session=request.getSession();
session.setAttribute("currentUser", resultUser);
return "welcome";
}
}
@RequestMapping("/add")
public String add(HttpServletRequest request,HttpServletResponse response){ 

response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
String username=request.getParameter("userName");
String password=request.getParameter("passWord");
User users=new User(username, password);
userService.add(users);
return "reback";
}
}

5、com.java.service

UserService.java

package com.java.service;
import com.java.entity.User;
public interface UserService { 

public User login(User user);
public User add(User user);
}

6、com.java.serviceimpl

ServiceImpl.java

package com.java.serviceimpl;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.java.dao.UserDao;
import com.java.entity.User;
import com.java.service.UserService;
@Service("userService")
public class ServiceImpl implements UserService{ 

@Resource
private UserDao userDao;
public User login(User user) { 

return userDao.login(user);
}
public User add(User user) { 

return userDao.add(user);
}
}

三、Jsp页面代码

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>用户登录页面</title>
</head>
<body>
<a href="add.jsp">注册</a>&nbsp;<br/>
<form action="${pageContext.request.contextPath}/user/login.do" method="post">
userName:<input type="text" name="userName" value="${userName}"/><br/>
passWord:<input type="password" name="passWord" value="${passWord}"/><br/>
<input type="submit" value="登录"/><br/>
<font color="red">${errorMsg}</font>
</form>
</body>
</html>

welcome.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登录成功</title>
</head>
<body>
欢迎   ${currentUser.userName} 来到我们的世界!
</body>
</html>

reback.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登录成功</title>
<script> function fun(){ 
 alert("确认返回登录页面"); /* location.href ="http://localhost:8080/mybatis06-spring-springmvc/index.jsp"; */ window.history.go(-2); } </script>
</head>
<body>
<input type="button" value="注册成功,返回登录页面" onclick="fun()"/>
</body>
</html>

add.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/user/add.do" method="post">
userName:<input type="text" name="userName"/>
passWord:<input type="password" name="passWord"><br/>
<input type="submit" value="注册" />
</form>
</body>
</html>
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/132534.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)
blank

相关推荐

  • DHT和一致性哈希算法总结

    DHT和一致性哈希算法总结 Hash算法比较重要的考量点有两个:1.单调性(新增或者减少映射节点时,尽量不影响原有映射关系)2.平衡性(尽量均匀分布) 分布式领域常见负载均衡算法:(1)取余法:%n如果有3个节点,Hash之后取模求余 Hash(x)%3,如果加一个节点,则Hash(x)%4。 这种方法带来的问题:1一个cache服务器mdown掉了(在实际应用中必…

  • dedecms后台系统基本参数标题

    dedecms后台系统基本参数标题

  • Linux下minicom操作

    Linux下minicom操作一、使用minicom发送文件1、首先安装工具:sudoapt-getinstalllrzsz2、打开minicom:sudominicom3、配置minicom:Ctrl+A,再按Z,再按O,出现configuration窗口(1)选择&lt;Filenamesandpaths&gt;设置发送和下载文件的路径并保存(2)选择&lt;Serialportsetup&gt;…

  • 向量内积/点积_两个向量的内积和外积

    向量内积/点积_两个向量的内积和外积向量内积/点积在向量空间Rn中,自然基下,向量x=(x1,…,xn)和y=(y1,…,yn)在向量空间\mathbb{R}^n中,自然基下,向量\boldsymbol{x}=(x_1,\ldots,x_n)和\boldsymbol{y}=(y_1,\ldots,y_n)在向量空间Rn中,自然基下,向量x=(x1​,…,xn​)和y=(y1​,…,yn​)的点积(dotproduct),或称内…

  • 呕心沥血整理的的嵌入式学习路线[通俗易懂]

    呕心沥血整理的的嵌入式学习路线[通俗易懂]曾经是某见的教学总监,我带出来的学生也有大几千了,基本都从事linux相关工作。网上看了很多的嵌入式学习路线,有的还凑活,有的纯粹是打广告卖板子,有的东拼西凑,简直是嵌入式劝退指南。大致给你梳理一下我从事的一个嵌入式的课程培训体系吧。基础1.linux基本操作:个人建议只要会一些基本的操作就行了,不需要太深入的学习每一个命令,用到的时候会去搜索就行了。需要掌握的知识如下:环境的搭建;Linux目录结构;文件类型;基本目录操作,文件操作,vim,ifconfig,ping,cd,cp,m

  • ~按位取反_按位取反什么意思

    ~按位取反_按位取反什么意思inta=0;intb=~a;System.out.println(b);  // -1计算方法:因为整形是32位有符号补码整数表示的。因此将整形转换为二进制后为:a 十进制0二进制00000000000000000000000000000000~a的二进制1111111111111111111111111

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号