功能:1、实现用户的登录
2、实现用户的注册以及重名的判断
LoginControl:
package com.example.demo.controls;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import com.example.demo.service.*;
@RestController
public class LoginControl {
@Autowired
private UserInfServiceInterface userInfService;
@RequestMapping("/login")
public ModelAndView login(String userid, String userpwd) {
if (userInfService.login(userid, userpwd) == 1)
return new ModelAndView("login_success.jsp");
else
return new ModelAndView("login_failure.jsp");
}
}
RegisterControl:
package com.example.demo.controls;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import com.example.demo.service.*;
import com.example.demo.model.UserInf;
@RestController
public class RegisterControl {
@Autowired
public UserInfServiceInterface userInfService;
@RequestMapping("/register")
public ModelAndView register(UserInf ui) {
if (userInfService.register(ui) == 1)
return new ModelAndView("register_success.jsp");
else if (userInfService.register(ui) == 0)
return new ModelAndView("register_failure_user.jsp");
else
return new ModelAndView("register_failure.jsp");
}
}
UserInfDaoInterface:
package com.example.demo.dao;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Component;
import com.example.demo.model.*;
@Mapper
public interface UserInfDaoInterface {
@Select("select count(*) from userinf where userid=#{userid} and userpwd=#{userpwd}")
public int selectByIdPwd(@Param("userid")String userid,@Param("userpwd")String userpwd);
@Select("select count(*) from userinf where realname=#{realname}")
public int selectByRealname(@Param("realname")String realname);
@Insert("insert into userinf values(#{userid},#{userpwd},#{realname})")
public int insert(UserInf ui);
}
UserInf:
package com.example.demo.model;
public class UserInf {
private String userid;
private String userpwd;
private String realname;
public String getUserid() {
return userid;
}
public void setUserid(String userid) {
this.userid = userid;
}
public String getUserpwd() {
return userpwd;
}
public void setUserpwd(String userpwd) {
this.userpwd = userpwd;
}
public String getRealname() {
return realname;
}
public void setRealname(String realname) {
this.realname = realname;
}
}
UserInfService:
package com.example.demo.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.example.demo.dao.UserInfDaoInterface;
import com.example.demo.model.UserInf;
@Service
@Transactional
public class UserInfService implements UserInfServiceInterface {
@Autowired
private UserInfDaoInterface userInfDao;
@Override
public int login(String userid, String userpwd) {
return userInfDao.selectByIdPwd(userid, userpwd);
}
@Override
public int register(UserInf ui) {
// TODO Auto-generated method stub
// TODO Auto-generated method stub
if(userInfDao.selectByRealname(ui.getRealname())==1)
return 0;
else
{
if(userInfDao.insert(ui)==1)
return 1;
else
return 2;
}
}
}
UserInfServiceInterface:
package com.example.demo.service;
import com.example.demo.model.UserInf;
public interface UserInfServiceInterface {
public int login(String userid,String userpwd);
public int register(UserInf ui);
}
DemoApplication:
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
application.properties:
server.port=8082
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/ssm?characterEncoding=utf8&useSSL=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root
login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<html>
<head>
<title>登录界面</title>
</head>
<body>
<form method="post" action="login">
<table>
<tr>
<th colspan="2">用户登录</th>
</tr>
<tr>
<td align="right">用户名:</td>
<td><input type="text" name="userid" /></td>
</tr>
<tr>
<td align="right">密码:</td>
<td><input type="password" name="userpwd" /></td>
</tr>
<tr>
<td align="left"><input type="submit" value="登录" /></td>
<td>未注册者,请先注册,单击 <a href="register.jsp">注册</a></td>
</tr>
</table>
</form>
<br>
</body>
</html>
login_success.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>登录成功</title>
</head>
<body>
欢迎你,${user.userRealName} ,你登入成功!!<br>
进入主页面,请点击<a href="index.jsp">主页面</a>。。。
</body>
</html>
login_failure
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>你的账号密码不匹配!</title>
</head>
<body>
<h2 align="center"><font color="red">
对不起,你填写的账号和密码不正确!请</font>
<a href="login.jsp">重新登录</a>!
</h2>
</body>
</html>
register.jsp
<%@ page contentType="text/html" 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 type="text/javascript">
function isValidate(form) {
var username=document.getElementById("username").value;
var userpass=document.getElementById("userpassword").value;
var userpass1=document.getElementById("userpass1").value;
if (userpass != userpass1) {
alert("两次密码输入不一致,请重新输入!");
return false;
}else if (userpass.length<=0 ||username.length<=0 ) {
alert("用户名 以及密码不能为空,请重新输入!");
return false;
}else{
return true;
}
}
</script>
</head>
<body>
<h3 align="left">欢迎注册我们的系统,请认真填写您的信息</h3>
<form action="register" method="post" onsubmit="return isValidate()">
<table>
<tr>
<td align="right">账户名:</td>
<td><input type="text" name="userid" id="username"></td>
</tr>
<tr>
<td align="right">为您的账户设置密码:</td>
<td><input type="password" name="userpwd" id="userpassword"></td>
</tr>
<tr>
<td align="right">再次确认您的密码:</td>
<td><input type="password" id="userpass1"></td>
<td></td>
</tr>
<tr>
<td align="right">真实姓名:</td>
<td><input type="text" name="realname" id="userrealname"></td>
<td></td>
</tr>
<tr>
<td align="right"><input type="submit" value="提交"></td>
<td colspan="2"><input type="reset" value="重新填写"></td>
</tr>
</table>
</form>
</body>
</html>
register_success.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>注册成功!</title>
</head>
<body >
<h3>恭喜,${ui.realname},你成功注册了我们的管理系统!点此
<a href="login.jsp">登录</a>
</h3>
</body>
</html>
register_failure
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>注册失败</title>
</head>
<body>
<h3>对不起,数据库出错,没有正确保存,请重新注册。</h3> <br>
<h2>请点击<a href="register.jsp">注册</a>。。。</h2>
</body>
</html>
register_failure_user
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>注册失败</title>
</head>
<body>
<h3>对不起,此用户名:${ui.realname}已被占用,请重新注册。</h3> <br>
<h2>请点击<a href="register.jsp">注册</a>。。。</h2>
</body>
</html>
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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>3.0.3</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.20</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>