项目样式:
SQL:
CREATE TABLE `t_user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(32) NOT NULL,
`password` varchar(32) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `username` (`username`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8
项目结构:
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>2.7.13</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.bai</groupId>
<artifactId>login</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>login</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</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>2.3.1</version>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter-test</artifactId>
<version>2.3.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.properties:
# 数据库连接信息
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/companydb?serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=123456
# mapper映射
mybatis.type-aliases-package=com.bai.login.pojo
mybatis.mapper-locations=classpath:mapper/*.xml
login.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>login</title>
</head>
<body>
<form method="post" action="/user/login">
用户名:<input name="username" type="text"><br>
密码:<input name="password" type="password"><br>
<input type="submit" value="登录验证">
<input type="button" onclick="javascript:window.location.href='regist.html'" value="去注册页面">
</form>
</body>
</html>
regist.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="/user/regist" method="post">
用户名:<input type="text" name="username"><br>
密码:<input type="password" name="password"><br>
<input type="submit" value="注册验证"><br>
<input type="button" onclick="javascript:window.location.href='login.html'" value="返回登录界面">
</form>
</body>
</html>
User.java:
package com.bai.login.pojo;
public class User {
private Integer id;
private String username;
private String password;
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;
}
}
UserMapper.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.bai.login.mapper.UserMapper">
<select id="findAll" resultType="User">
select * from t_user
</select>
<select id="findByName" resultType="User">
select * from t_user where username = #{username}
</select>
<select id="findPswByName" resultType="String">
select password from t_user where username = #{username}
</select>
<insert id="save">
insert into t_user(username,password) value (#{username},#{password})
</insert>
</mapper>
UserMapper.java:
package com.bai.login.mapper;
import com.bai.login.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface UserMapper {
List<User> findAll();
User findByName(String name);
String findPswByName(String UserName);
void save(User user);
}
UserService.java:
package com.bai.login.service;
import com.bai.login.mapper.UserMapper;
import com.bai.login.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
public String login(User user){
try {
User userExistN=userMapper.findByName(user.getUsername());
if (userExistN!=null){
String userExistP=userMapper.findPswByName(user.getUsername());
if (userExistP.equals(user.getPassword())){
return user.getUsername()+"用户登录成功,欢迎您!";
}else {
return "登录失败,密码错误";
}
}else {
return "登录失败,账户不存在";
}
} catch (Exception e) {
e.printStackTrace();
return e.getMessage();
}
}
public String regist(User user){
try {
User userExist=userMapper.findByName(user.getUsername());
if (user.getUsername().equals("")){
return "账户名不能为空";
}else if (user.getPassword().equals("")){
return "密码不能为空";
}else if (userExist!=null) {
return "账户已经存在";
}else {
userMapper.save(user);
return "注册成功";
}
} catch (Exception e) {
e.printStackTrace();
return e.getMessage();
}
}
public List<User> findAll(){
List<User> list=userMapper.findAll();
return list;
}
}
UserController.java:
package com.bai.login.controller;
import com.bai.login.pojo.User;
import com.bai.login.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/login")
public String login(User user){
return userService.login(user);
}
@RequestMapping("/regist")
public String regist(User user){
return userService.regist(user);
}
/**
* 解决查询数据库中文出现乱码问题
* @return
*/
@RequestMapping(value = "/alluser",method = RequestMethod.GET,produces = "application/json;charset=UTF-8" )
public List<User> findAll(){
return userService.findAll();
}
}
LoginApplication.java:
package com.bai.login;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class LoginApplication {
public static void main(String[] args) {
SpringApplication.run(LoginApplication.class, args);
}
}
访问地址:http://localhost:8080/login.html