SSM实现登录注册的小案例(手把手喂饭)
Posted 长理小生
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SSM实现登录注册的小案例(手把手喂饭)相关的知识,希望对你有一定的参考价值。
SSM实现登录注册的小案例
温馨提示
为了您有更好的阅读体验,原文链接如下,长理小生:https://lixingweiblog.github.io/Pages
SpringMVC+Spring+Mybatis
涉及知识
- java的注解、反射、代理
- 数据库的设计、增删改查、索引
- Spring的IoC注入原理
- SpringMVC与web交互
- Mybatis代理Dao层
所需环境
- jdk、maven、SQLServer
一、数据库设计(SQL Server)
1.表的结构
表customers:
- customer_id int 主键
- customer_account varchar(20) 用户名
- customer_passwd char(64) 密码哈希
- message_id int 用户信息外键
表message:
- message_id int 主键
- message_email varchar(40) 邮箱
- message_phone varchar(20) 电话
USE SpringSigninReg;
CREATE TABLE message
(
message_id INT NOT NULL PRIMARY KEY IDENTITY,
message_email VARCHAR(40),
message_phone VARCHAR(20)
);
CREATE TABLE customers
(
customer_id INT NOT NULL PRIMARY KEY IDENTITY,
customer_account VARCHAR(20) NOT NULL,
customer_passwd CHAR(64) NOT NULL,
message_id INT FOREIGN KEY REFERENCES message(message_id)
)
INSERT message(message_email, message_phone)
VALUES ('test@foxmail.com','15115111511');
INSERT customers(customer_account,customer_passwd,message_id)
VALUES ('test','80dea37cccca5fb56b9b78dbe2733fb19e8ee2aa7bb03d4ac1e361fe3fad7b51','1');
2.查询优化:
需要频繁的根据账号customer_account
查询用户,所以需要给它建立索引
CREATE INDEX idx_cuacc
ON customers(customer_account);
3.密码安全:
将密码经过sha256运算后存入,规避明文存储密码的风险
二、构建项目
1.新建maven项目
file->new->project->Maven->org.a…-webapp
Name为项目名,GroupId一般取做公司名,其余不必更改
采用默认,直接finish
创建好的目录如下:
2.导入依赖
将pom.xml
的相关依赖添加好,可以参考如下:
<?xml version="1.0" encoding="UTF-8"?>
<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.0</modelVersion>
<groupId>com.kingdom</groupId>
<artifactId>SsmCase</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>SsmCase Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<spring.version>5.0.2.RELEASE</spring.version>
<slf4j.version>1.6.6</slf4j.version>
<log4j.version>1.2.12</log4j.version>
<mybatis.version>3.4.5</mybatis.version>
</properties>
<dependencies>
<!-- spring -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.6.8</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>$spring.version</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>$spring.version</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>$spring.version</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>$spring.version</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>$spring.version</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>$spring.version</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>$spring.version</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>8.2.2.jre8</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- log start -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>$log4j.version</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>$slf4j.version</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>$slf4j.version</version>
</dependency>
<!-- log end -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>$mybatis.version</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<finalName>SsmCase</finalName>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- tomcat插件 -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<port>8081</port>
<path>/</path>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
3.创建基本目录及文件
在main目录下面创建java和resources文件夹,并设置相应文件
右击main目录,new directory,会提示创建两个Maven的源目录,如果没有则自己建立,记得将新建目录mark为Source Root
和Resource Root
创建类和接口
- 存放javabean的
domain
包下的用户类:com.kingdom.domain.Customers
- 数据访问层
dao
包下的用户 dao 接口:com.kingdom.dao.CustomersDao
- 业务层
service
包下的CustomersService
接口:com.kingdom.service.CustomersService
- 业务层
service
包下的service
实现类,继承service
接口:com.kingdom.service.impl.CustomersServiceImpl
- 控制层
controller
包下的用户控制层CustomersController
类:com.kingdom.controller.CustomersController
- 工具包
util
下用于安全加密的类:com.kingdom.util.Security
com.kingdom.domain.Customers
package com.kingdom.domain;
/**
* @ClassName: Customers
* @Author: LXW
* @Date: 2021/7/20
*/
public class Customers
private Integer customer_id;
private String customer_account;
private String customer_passwd;
private Integer message_id;
public Integer getCustomer_id()
return customer_id;
public void setCustomer_id(Integer customer_id)
this.customer_id = customer_id;
public String getCustomer_account()
return customer_account;
public void setCustomer_account(String customer_account)
this.customer_account = customer_account;
public String getCustomer_passwd()
return customer_passwd;
public void setCustomer_passwd(String customer_passwd)
this.customer_passwd = customer_passwd;
public Integer getMessage_id()
return message_id;
public void setMessage_id(Integer message_id)
this.message_id = message_id;
@Override
public String toString()
return "Customers" +
"customer_id=" + customer_id +
", customer_account='" + customer_account + '\\'' +
", customer_passwd='" + customer_passwd + '\\'' +
", message_id=" + message_id +
'';
com.kingdom.dao.CustomersDao
package com.kingdom.dao;
/**
* @ClassName: CustomersDao
* @Author: LXW
* @Date: 2021/7/20
*/
public class CustomersDao
com.kingdom.service.CustomersService
package com.kingdom.service;
/**
* @ClassName: CustomersService
* @Author: LXW
* @Date: 2021/7/20
*/
public interface CustomersService
public String testString();
com.kingdom.service.impl.CustomersServiceImpl
package com.kingdom.service.impl;
import com.kingdom.service.CustomersService;
import org.springframework.stereotype.Service;
/**
* @ClassName: CustomersServiceImpl
* @Author: LXW
* @Date: 2021/7/20
*/
@Service("customersService")
public class CustomersServiceImpl implements CustomersService
@Override
public String testString()
return "Service层";
<以上是关于SSM实现登录注册的小案例(手把手喂饭)的主要内容,如果未能解决你的问题,请参考以下文章