![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/e7cc37730cb91a4150fd92c7754a4cd4.png#pic_center)
上一文章地址:https://blog.csdn.net/weixin_44369335/article/details/93618439
上一个文章的项目并没有实现注册登录
1. 注册验证是否存在相同用户名
2. 登录验证用户名密码是否存在,在这里完善一下
注册时验证是否存在此用户,存在返回注册页,不存在返回登录页
登录时验证是否存在此用户,存在返回登录成功,不存在返回登录页
项目目录
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/9bde92ef0cac56f87c2ef9bec948caf6.png)
上一个文章没有用service,这里补上,还有一些改动的地方
UserDao.java
package com.demo.dao;
import org.apache.ibatis.annotations.Param;
import com.demo.entity.User;
public interface UserDao{
User logincheck(@Param("username") String username,@Param("password") String password);
void register(@Param("username") String username,@Param("password") String password);
}
UserDao.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.demo.dao.UserDao">
<select id="logincheck" parameterType="com.demo.entity.User" resultType="com.demo.entity.User">
select *
from user
<where>
<if test="username != null and username != ''">
and username = #{username}
</if>
<if test="password != null and password != ''">
AND password = #{password}
</if>
</where>
</select>
<insert id="register" parameterType="com.demo.entity.User">
insert into
user(username,password)
values(#{username},#{password})
</insert>
</mapper>
UserService.java
package com.demo.service;
import com.demo.entity.User;
public interface UserService{
User logincheck(String username,String password);
void register(String username,String password);
}
UserServiceImpl.java
package com.demo.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.demo.dao.UserDao;
import com.demo.entity.User;
@Service
public class UserServiceImpl implements UserService{
@Autowired
private UserDao userDao;
public User logincheck(String username,String password) {
return userDao.logincheck(username,password);
}
public void register(String username,String password) {
userDao.register(username,password);
}
}
index.jsp
<%@ page contentType="text/html;charset=UTF-8" %>
<html>
<head>
<title>登陆</title>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<!-- Tell the browser to be responsive to screen width -->
<meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
<!-- Font Awesome -->
<style>
*{margin:0;padding:0;}
p{
margin-top:20px;
}
#btn{
padding:4px 10px;
}
</style>
</head>
<body style="background:rgba(112,112,112,.4);width:100%;height:100%;">
<div style="position:absolute;width:300px;height:150px;text-align:center;background:rgba(0,144,255,.4);top:50%;left:50%;margin-top:-75px;margin-left:-100px;">
<form action="login">
<p>
<label for="username">用户名:</label>
<input type="text" name="username" id="username"/>
</p>
<p>
<label for="pwd">密 码:</label>
<input type="password" name="password" id="pwd"/>
</p>
<p>
<input type="submit" value="登录"/>
<a href="toRegister"><input type="button" value="注册"/></a>
</p>
</form>
</div>
</body>
</html>
reigster.jsp
<%@ page contentType="text/html;charset=UTF-8" %>
<html>
<head>
<title>登陆</title>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<!-- Tell the browser to be responsive to screen width -->
<meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
<!-- Font Awesome -->
<style>
*{margin:0;padding:0;}
p{
margin-top:20px;
}
#btn{
padding:4px 10px;
}
</style>
</head>
<body style="background:rgba(112,112,112,.4);width:100%;height:100%;">
<div style="position:absolute;width:300px;height:150px;text-align:center;background:rgba(0,144,255,.4);top:50%;left:50%;margin-top:-75px;margin-left:-100px;">
<form action="register">
<p>
<label for="username">用户名:</label>
<input type="text" name="username" id="username"/>
</p>
<p>
<label for="pwd">密 码:</label>
<input type="password" name="password" id="pwd"/>
</p>
<p>
<input type="submit" value="提交"/>
</p>
</form>
</div>
</body>
</html>
UserController.java
package com.demo.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.demo.entity.User;
import com.demo.service.UserService;
@Controller
public class UserController{
@Autowired
private UserService userService;
@RequestMapping(value = "login")
public String form(@RequestParam("username") String username,
@RequestParam("password") String password,
Model model) {
User user = new User();
user.setName(username);
user.setPassword(password);
User isLogin = userService.logincheck(username,password);
if (isLogin!=null){
return "/login";
}else{
return "../../index";
}
}
@RequestMapping(value = "register")
public String register(@RequestParam("username") String username,
@RequestParam("password") String password,
Model model) {
User user = new User();
user.setName(username);
user.setPassword(password);
User isRegister = userService.logincheck(username,null);
if (isRegister == null){
userService.register(username,password);
return "../../index";
}else{
return "register";
}
}
@RequestMapping(value = "toRegister")
public String register() {
return "/register";
}
}