功能: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"%>
登录界面
login_success.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
登录成功
欢迎你,${user.userRealName} ,你登入成功!!
进入主页面,请点击主页面。。。
login_failure
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
你的账号密码不匹配!
对不起,你填写的账号和密码不正确!请
重新登录!
register.jsp
<%@ page contentType="text/html" pageEncoding="UTF-8"%>
注册页面
欢迎注册我们的系统,请认真填写您的信息
register_success.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
注册成功!
恭喜,${ui.realname},你成功注册了我们的管理系统!点此
登录
register_failure
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
注册失败
对不起,数据库出错,没有正确保存,请重新注册。
请点击注册。。。
register_failure_user
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
注册失败
对不起,此用户名:${ui.realname}已被占用,请重新注册。
请点击注册。。。
pom.xml
4.0.0
org.springframework.boot
spring-boot-starter-parent
3.2.0
com.example
demo
0.0.1-SNAPSHOT
demo
Demo project for Spring Boot
17
org.springframework.boot
spring-boot-starter-web
org.mybatis.spring.boot
mybatis-spring-boot-starter
3.0.3
mysql
mysql-connector-java
8.0.20
org.apache.tomcat.embed
tomcat-embed-jasper
provided
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-devtools
true
org.springframework.boot
spring-boot-maven-plugin